Java – List files in a AWS S3 Bucket

In this post we will provide examples on how to list files in a AWS S3 bucket using Java. Amazon provides a cloud storage service called S3, it allows you to store and retrieve data at any time from anywhere on the web. It is highly scaleable, fast and reliable. For more information on Amazon S3 please refer to their documentation.


You will need to add the following amazon-sdk dependancy to your project

You will also need to setup your AWS credentials a simple approach is to export the following environment variables:

  • AWS_REGION=<your-aws-region>
  • AWS_ACCESS_KEY_ID=<your-access-key>
  • AWS_SECRET_ACCESS_KEY=<your-secret-access-key>

For alternative approaches please check reference 1 below for more details.

List files in a AWS S3 bucket:

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

Running the above code prints the files contained in the bucket. NoteĀ if you have many files within a bucket then, all of the files may not be returned. To ensure you have listed all the files you should check the isTruncated() method. This will let you know if there are more files to be read, below is an example ensuring all files are listed.

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

Similar posts:

  1. Java 8 streams map example
  2. Java 8 Streams flatmap example
  3. Java List files in a directory


  1. AWS credentials setup
  2. What is Amazon S3

Leave a Reply