How to get a list of buckets stored in S3 using Java – AWS S3 List Buckets

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get a list of buckets stored in S3 using java language.

Project Setup

Create a simple maven project in your favorite IDE and add below mentioned dependency in your pom.xml file.

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-s3</artifactId>
  <version>1.11.533</version>
</dependency>

For latest version of aws library, check this page.

S3 Connection

Create an object of AmazonS3 ( com.amazonaws.services.s3.AmazonS3 ) class for sending a client request to S3. To get instance of this class, we will use AmazonS3ClientBuilder builder class. It requires three important parameters :- 

  1. Region :- It is a region where S3 table will be stored.
  2. ACCESS_KEY :- It is a access key for using S3. You can generate this key, using aws management console.
  3. SECRET_KEY :- It is a secret key of above mentioned access key.

Here is a code example :-

AmazonS3 s3 = AmazonS3ClientBuilder.standard()
				.withRegion(Regions.AP_SOUTH_1)
				.withCredentials(new AWSStaticCredentialsProvider
                                                    (new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))
				.build();

List Buckets

An AmazonS3.listBuckets method returns a list of all S3 buckets that the authenticated sender of the request owns. Users can only list buckets that it had created.

Example

import java.util.List;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;

public class ListBuckets {

	public static void main(String[] args) {
		
		/* Create S3 Client Object */
		AmazonS3 s3 = AmazonS3ClientBuilder
				.standard()
				.withRegion(Regions.AP_SOUTH_1)
				.withCredentials(new AWSStaticCredentialsProvider(
                                     new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))
				.build();
		
		try {			
			/* Send List Bucket Request */
			List&lt;Bucket&gt; buckets = s3.listBuckets();
			
			/* Printing Bucket Names */
			if (buckets != null) {
				buckets.stream().forEach(e -&gt; System.out.println(e.getName()));
			} else {
				System.out.println("No bucket available");
			}
            
        } catch (AmazonServiceException e) {
        	
            System.out.println(e.getErrorMessage());
            
        } finally {
        	
        	if(s3 != null) {
        		s3.shutdown();
        	}        	
        }
	}
}

Similar Post

  1. How to upload an object to S3 bucket using Java
  2. How to get a list of objects stored in S3 using Java
  3. How to delete bucket in S3 using Java
  4. How to create bucket in S3 using Java

References :-

  1. ListBucketsRequest Java Docs
  2. Bucket Java Docs

That’s all for how to get a list of buckets stored in S3 using java language. If you liked it, please share your thoughts in comments section and share it with others too.