AWS Java SDK S3 Create Bucket Examples
- 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 creating buckets on Amazon S3 server programmatically. In details, I will share some code examples about:
- create a bucket with default permission
- create a bucket with read permission for public users
- wait until a bucket is created
Note: you must have AWS SDK for Java installed beforehand. If not, refer to this guide.
Bucket Naming Rules:
In Amazon S3, a bucket name is globally unique. That means when creating a new bucket, you must give a name that is unique across AWS accounts and regions. In addition, a bucket name:
- must be between 3 and 63 characters long
- can consist only of lowercase letters, numbers, dots (.) and hyphens (-)
- must begin and end with a letter or number
- must not be formatted as an IP address
1. Create a Bucket with default permission
The following code example creates a new bucket in the AWS default region:
package net.codejava.aws; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; public class CreateBucketExample1 { public static void main(String[] args) { String bucketName = "codejava-private"; S3Client client = S3Client.builder().build(); CreateBucketRequest request = CreateBucketRequest.builder().bucket(bucketName).build(); client.createBucket(request); } }
By default, the bucket owner has full permission, whereas public users do not have any permissions. That means the bucket is not publicly accessible by default.
2. Create a Bucket with Public Read permission
The following code example shows how to create a bucket with Read permission for public users, using the acl() method and a predefined (canned) ACL (Access Control List):
String bucketName = "codejava-public"; S3Client client = S3Client.builder().build(); CreateBucketRequest request = CreateBucketRequest.builder() .bucket(bucketName) .acl(BucketCannedACL.PUBLIC_READ).build(); client.createBucket(request);
The bucket owner always has full permission, and the bucket is publicly accessible to everyone.
For exception handling, you may need to catch BucketAlreadyOwnedByYouException or BucketAlreadyExistsException, which is self-explanatory.
3. Wait Until a Bucket exists
Amazon S3 creates buckets almost instantly. If you want to run some custom code that depends on the existence of new buckets, you can use a waiter. Here’s an example:
package net.codejava.aws; 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.waiters.S3Waiter; public class CreateBucketExample3 { public static void main(String[] args) { String bucketName = "shopme-files"; S3Client client = S3Client.builder().build(); S3Waiter waiter = client.waiter(); CreateBucketRequest request = CreateBucketRequest.builder().bucket(bucketName).build(); client.createBucket(request); HeadBucketRequest requestWait = HeadBucketRequest.builder().bucket(bucketName).build(); waiter.waitUntilBucketExists(requestWait); System.out.println("Bucket " + bucketName + " is ready."); // run code that depends on the newly created bucket } }
You see, the method waitUntiBucketExists() of S3Waiter class will return if the new bucket actually created, so you can be sure that the next code will run properly.
Those are some code examples about creating buckets on Amazon S3 using AWS SDK for Java. If you want to see the coding in action, watch the following video:
References:
Related AWS Java SDK Tutorials:
- How to Generate AWS Access Key ID and Secret Access Key
- How to setup AWS SDK for Java for Amazon S3 Development
- AWS Java SDK S3 List Buckets Example
- AWS Java SDK S3 List Objects Examples
- AWS Java SDK S3 Create Folder Examples
- Upload File to S3 using AWS Jav SDK - Java Console Program
- Upload File to S3 using AWS Java SDK - Java Servlet JSP Web App
- Spring Boot File Upload to Amazon S3 Example
- AWS Java SDK Download File from S3 Example
- AWS Java SDK S3 Delete Objects Examples
- AWS Java SDK S3 Delete Buckets Examples