In this AWS Java SDK article, I’d like share some Java code examples about file download from a bucket on Amazon S3 programmatically. You can use the code example to add S3 file download function to your application.

I suppose that you already have AWS SDK for Java set up on your computer. If not, follow this guide: How to setup AWS SDK for Java for Amazon S3 Development.

Below is the code of a Java console program that downloads a file from a bucket on S3, and then saves the file on disk:

package net.codejava.aws;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;

public class S3DownloadFileExample {
	
	public static void main(String[] args) throws IOException {
		String bucket = "your-bucket-name";
		String key = "Your file.png";
		
		S3Client client = S3Client.builder().build();
		
		GetObjectRequest request = GetObjectRequest.builder()
						.bucket(bucket)
						.key(key)
						.build();
		
		ResponseInputStream<GetObjectResponse> response = client.getObject(request);
				
		BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(key));
		
		byte[] buffer = new byte[4096];
		int bytesRead = -1;
		
		while ((bytesRead = response.read(buffer)) !=  -1) {
			outputStream.write(buffer, 0, bytesRead);
		}
							
		response.close();
		outputStream.close();
	}
}
To run this program, you must specify exactly the bucket name on your AWS account, the object key of file, and the AWS credentials you’re using has at least read permission on the file.

In this example, the object key looks like it is directly underneath the bucket. And the file will be saved in the current directory (same as application’s directory). The file name is as same as the object key.

If you use a key that looks like a directory path, consider the following example:

String bucket = "your-bucket-name";
String key = "Dir1/subdir1/Your file.doc";

S3Client client = S3Client.builder().build();

GetObjectRequest request = GetObjectRequest.builder()
					.bucket(bucket)
					.key(key)
					.build();

ResponseInputStream<GetObjectResponse> response = client.getObject(request);

String fileName = new File(key).getName();
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(fileName));

byte[] buffer = new byte[4096];
int bytesRead = -1;

while ((bytesRead = response.read(buffer)) !=  -1) {
	outputStream.write(buffer, 0, bytesRead);
}
					
response.close();
outputStream.close();
For handling exception, consider to catch NoSuchKeyException, InvalidObjectStateException, AwsServiceException, SdkClientException, and S3Exception which can be thrown by the S3Client.getObject() method.

That’s a couple of examples for downloading a file from a bucket on Amazon S3. I hope you find it useful. To see the coding in action, watch the following video:



 

Related AWS Java SDK Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.