o7planning

Java Create, list and delete S3 Bucket

  1. Library
  2. List Buckets
  3. Create S3 Bucket
  4. Delete S3 Bucket
MyUtils.java
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

public class MyUtils {  

	public static S3Client createS3Client(Region region) {
		AwsCredentials credentials = AwsBasicCredentials.create("accessKeyId", "secretAccessKey");
		AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(credentials);

		S3Client client = S3Client.builder() //
				.credentialsProvider(credentialsProvider) //
				.region(region) //
				.build();

		return client;
	} 
}
To create an S3Client object you need to create an AwsCredentialsProvider object, this object provides credentials that allow you to interact with AWS. See the article below to create an AwsCredentialsProvider suitable for your purposes.

1. Library

<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/s3 -->
<dependency>
	<groupId>software.amazon.awssdk</groupId>
	<artifactId>s3</artifactId>
	<version>2.21.10</version>
</dependency>

<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/s3-transfer-manager -->
<dependency>
	<groupId>software.amazon.awssdk</groupId>
	<artifactId>s3-transfer-manager</artifactId>
	<version>2.21.10</version>
</dependency>

<!-- https://mvnrepository.com/artifact/software.amazon.awssdk.crt/aws-crt -->
<dependency>
	<groupId>software.amazon.awssdk.crt</groupId>
	<artifactId>aws-crt</artifactId>
	<version>0.28.0</version>
</dependency>

<!-- Log Library -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>2.0.9</version>
</dependency>

<!-- Log Library -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-simple</artifactId>
	<version>2.0.9</version>
</dependency>

2. List Buckets

For example, use S3Client to list your Bucket(s) in a Region.
ListBucketExample.java
package org.o7planning.java_14257_awssdk_s3;

import org.o7planning.awssdks3.utils.MyUtils;

import software.amazon.awssdk.regions.Region;
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 ListBucketExample {
	private static Region myRegion = Region.EU_CENTRAL_1;

	private static void listBuckets(S3Client s3Client) {
		// List buckets
		ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build();
		ListBucketsResponse listBucketsResponse = s3Client.listBuckets(listBucketsRequest);
		listBucketsResponse.buckets().stream().forEach(x -> System.out.println(x.name()));
	}

	public static void main(String[] args) {
		S3Client s3Client = MyUtils.createS3Client(myRegion);

		listBuckets(s3Client);
	}
}

3. Create S3 Bucket

Note, the name of a Bucket is unique for all users, so you should name the Bucket as suggested by a domain name of your website. Such as:
  • test1.yourdomain.com
  • test1-yourdomain-com
  • static.yourdomain.com
Example, use S3Client to create a Bucket
CreateBucketExample1.java
package org.o7planning.java_14257_awssdk_s3;

import org.o7planning.awssdks3.utils.MyUtils;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;

public class CreateBucketExample1 {
	private static Region myRegion = Region.EU_CENTRAL_1;
	private static String bucketName = "test1.o7planning.org";

	// Create a bucket
	public static void createBucket(S3Client s3Client, String bucketName) {

		CreateBucketRequest createBucketRequest = CreateBucketRequest.builder() //
				.bucket(bucketName) //
				.build();

		s3Client.createBucket(createBucketRequest);
		System.out.println(bucketName + " is ready");
	}

	public static void main(String[] args) {
		S3Client s3Client = MyUtils.createS3Client(myRegion);

		createBucket(s3Client, bucketName);
	}
}
In the above example we have created a completely private Bucket, this means that all objects in the Bucket will not be publicly accessible. So depending on your intended use, you need to add a few small settings.
  • Java Awssdk config Bucket
S3Waiter Example:
Nếu bạn đang tạo một S3 Bucket trên Thread-1, và Thread-2 đang chờ đợi Bucket này được tạo ra để thực hiện tiếp một hành động nào đó. Hãy sử dụng S3Waiter trong Thread-2.
CreateBucketExample2.java
package org.o7planning.java_14257_awssdk_s3;

import org.o7planning.awssdks3.utils.MyUtils;

import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
import software.amazon.awssdk.services.s3.model.HeadBucketResponse;
import software.amazon.awssdk.services.s3.waiters.S3Waiter;

public class CreateBucketExample2 {
	private static Region myRegion = Region.EU_CENTRAL_1;
	private static String bucketName = "test5.o7planning.org";

	public static void main(String[] args) {
		new PubThread().start();

		new SubThread().start();
	}

	static class PubThread extends Thread {

		@Override
		public void run() {
			S3Client s3Client = MyUtils.createS3Client(myRegion);
			System.out.println("PubThread: Creating " + bucketName + " ...");

			CreateBucketRequest bucketRequest = CreateBucketRequest.builder() //
					.bucket(bucketName).build();

			s3Client.createBucket(bucketRequest);
			System.out.println("PubThread: " + bucketName + " is ready!");
		}
	}

	static class SubThread extends Thread {

		@Override
		public void run() {
			S3Client s3Client = MyUtils.createS3Client(myRegion);
			System.out.println("SubThread: Wait for bucket " + bucketName);

			//
			// Create S3Waiter
			//
			S3Waiter s3Waiter = s3Client.waiter();

			HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder() //
					.bucket(bucketName) //
					.build();

			//
			// Wait until the bucket is created and print out the response.
			//
			WaiterResponse<HeadBucketResponse> waiterResponse //
					= s3Waiter.waitUntilBucketExists(bucketRequestWait);
			waiterResponse.matched().response().ifPresent(System.out::println);
			System.out.println("SubThread: " + bucketName + " is ready");
		}
	}
}
Output:
PubThread: Creating test5.o7planning.org ...
SubThread: Wait for bucket test5.o7planning.org
PubThread: test5.o7planning.org is ready!
HeadBucketResponse()
SubThread: test5.o7planning.org is ready

4. Delete S3 Bucket

Before deleting a Bucket you need to make sure it is an empty Bucket otherwise the service will return an error. This means you need to delete all objects on the Bucket as well as versions of each object if any.
See also how to delete objects on Bucket, this article also includes an example of deleting all objects.
  • Java Awssdk S3 delete object
The code below will delete an S3 Bucket assuming it is an empty bucket.
DeleteBucketExample.java
package org.o7planning.java_14257_awssdk_s3;

import org.o7planning.awssdks3.utils.MyUtils;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;

public class DeleteBucketExample {
	private static Region myRegion = Region.EU_CENTRAL_1;
	private static String bucketName = "test1.o7planning.org";

	private static void deleteBucket(S3Client s3Client, String bucketName) {
		DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder() //
				.bucket(bucketName) //
				.build();

		s3Client.deleteBucket(deleteBucketRequest);
		s3Client.close();
		System.out.println("Done!");
	}

	public static void main(String[] args) {
		S3Client s3Client = MyUtils.createS3Client(myRegion);

		deleteBucket(s3Client, bucketName);
	}
}