- Details
- Written by Nam Ha Minh
- Last Updated on 03 January 2022   |   Print Email
In this AWS Java SDK tutorial, you will learn how to write Java code for listing buckets in a specific region on Amazon S3 server. In S3’s terms, a bucket is a container for objects stored on Amazon’s cloud storage service. You can have many buckets in your AWS account which belongs to a specific geographical region.
Notes: You may need to setup
AWS SDK for Java for Amazon S3 beforehand, in order to follow this guide.
1. List Buckets in the default AWS region
The following program prints name and creation date of all buckets in the default region specified by the AWS_REGION environment variable (or in AWS config file):
package net.codejava.aws;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListBucketsRequest;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
public class ListBucketsExample1 {
public static void main(String[] args) {
S3Client client = S3Client.builder().build();
ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build();
ListBucketsResponse listBuckets = client.listBuckets(listBucketsRequest);
listBuckets.buckets().stream().forEach(
x -> System.out.println(x.name() + " - " + x.creationDate()));
}
}
You see, the code example is quite simple and straightforward. You need to build a
S3Client object and a
ListBucketsRequest object. And invoke the
S3Client.listBuckets() method that returns a
ListBucketsResponse object.And call
ListBucketsResponse.buckets() method that returns a
List<Bucket>, and in the example I use Java Stream API to iterate through each bucket in the list, and print out name and creation date.You would see the output of the above example program as follows:
computer.nam-public-images - 2021-08-23T09:19:20Z
nam-code-backup - 2021-05-16T03:26:13Z
nam-public-images - 2021-05-16T03:47:00Z
2. List Buckets in a specific AWS region
The following code example illustrates how to list all buckets in a specific AWS region, e.g. US_EAST_2:
package net.codejava.aws;
import java.util.List;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.Bucket;
import software.amazon.awssdk.services.s3.model.ListBucketsRequest;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
public class ListBucketsExample2 {
public static void main(String[] args) {
Region region = Region.US_EAST_2;
S3Client client = S3Client.builder().region(region).build();
ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build();
ListBucketsResponse listBuckets = client.listBuckets(listBucketsRequest);
List<Bucket> buckets = listBuckets.buckets();
for (Bucket bucket : buckets) {
System.out.println(bucket.name() + " - " + bucket.creationDate());
}
}
}
The code is self-explanatory. Note that I use a for each loop to print name and creation date of each bucket in the list.
Notes: If you want to handle exceptions, you can catch
AwsServiceException,
SdkClientException and
S3Exception which may be thrown by the
listBuckets() method of the
S3Client interface.Those are a couple of code examples that list buckets on Amazon S3 server. I hope you find this tutorial helpful.
Related AWS Java SDK Tutorials:
About the Author:
Nam Ha Minh 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.