Get file in a AWS S3 bucket:

The example below assumes you have a bucket already created with files on S3.

1

2

3

4

5

6

7

8

@Test

publicvoidgetFileFromS3Bucket()throwsIOException{

AmazonS3 s3Client=AmazonS3ClientBuilder.defaultClient();

S3Object object=s3Client.getObject("bucket-name","file-key.JPG");

File file=newFile("downloaded-file.jpg");

FileUtils.copyInputStreamToFile(object.getObjectContent(),file);

}

Running the above code will download a copy the content from the file on s3 into the new ‘downloaded-file.jpg’ file. Note you will need to have access to download files from the bucket. For more information relating to AWS access controls please refer to the AWS documentation

In conclusion I hope you found this post useful and it helps you understand how to get files within an S3 bucket.