coding made simple with some delicious cake

Java – Create a bucket in S3

In this post we will provide a code sample of how to create a bucket in S3 (amazon web services). In order to create a bucket you need to ensure that you have setup the appropriate security access, you can find more information regarding this in the reference section.

Setup:

You will need to add the following dependency to your project.

1

2

3

4

5

<dependency>

<groupId>com.amazonaws</groupId>

<artifactId>aws-java-sdk</artifactId>

<version>1.11.283</version>

</dependency>

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 the reference section below for more details

Delete a bucket in S3:

The following code example show how to delete a bucket using the amazon java sdk.

Java

1

2

3

4

5

6

7

8

9

10

11

@Test

publicvoidcreatingABucket(){

try{

AmazonS3 s3Client=AmazonS3ClientBuilder.defaultClient();

Bucket bucket=s3Client.createBucket("your-bucket");

System.out.println("bucket = "+bucket);

}catch(SdkClientExceptione){

e.printStackTrace();

fail();

}

}

Running the above code will create a bucket in S3. You will need to have setup your development environment correctly to ensure you have enough access to create a bucket on S3. Please check the references for more details about this.

In conclusion, I hope you found this post useful and helps you understand how to work with AWS S3.