How to delete bucket in S3 using Java – AWS S3 Delete Bucket
5 years ago Lalit Bhagtani 0
In this tutorial, we will learn about how to delete a bucket 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 :-
- Region :- It is a region where S3 table will be stored.
- ACCESS_KEY :- It is a access key for using S3. You can generate this key, using aws management console.
- 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();
Delete Bucket
Amazon S3 buckets can only be deleted when they are empty, So in order to delete a bucket, we first should delete all objects and their version stored inside a bucket. Steps to create and send delete bucket request to Amazon S3 are as follows:-
- Get list of objects stored inside a given bucket by executing the listObjects method on AmazonS3 object.
- Recursively delete all the objects stored inside a given bucket.
- Get list of versions of objects stored inside a given bucket by executing the listVersions method on AmazonS3 object.
- Recursively delete all the version of objects stored inside a given bucket.
- Invoke the deleteBucket method on AmazonS3 object by passing a given bucket name as an argument.
Example
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.ListVersionsRequest; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.S3ObjectSummary; import com.amazonaws.services.s3.model.S3VersionSummary; import com.amazonaws.services.s3.model.VersionListing; public class DeleteBucket { 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 { /* Get list of objects in a given bucket */ ObjectListing objects = s3.listObjects("lb-aws-learning"); /* Recursively delete all the objects inside given bucket */ if(objects != null && objects.getObjectSummaries() != null) { while (true) { for(S3ObjectSummary summary : objects.getObjectSummaries()) { s3.deleteObject("lb-aws-learning", summary.getKey()); } if (objects.isTruncated()) { objects = s3.listNextBatchOfObjects(objects); } else { break; } } } /* Get list of versions in a given bucket */ VersionListing versions = s3.listVersions(new ListVersionsRequest().withBucketName("lb-aws-learning")); /* Recursively delete all the versions inside given bucket */ if(versions != null && versions.getVersionSummaries() != null) { while (true) { for(S3VersionSummary summary : versions.getVersionSummaries()) { s3.deleteObject("lb-aws-learning", summary.getKey()); } if (versions.isTruncated()) { versions = s3.listNextBatchOfVersions(versions); } else { break; } } } /* Send Delete Bucket Request */ s3.deleteBucket("lb-aws-learning"); } catch (AmazonServiceException e) { System.out.println(e.getErrorMessage()); } finally { if(s3 != null) { s3.shutdown(); } } } }
Similar Post
- How to upload an object to S3 bucket using Java
- How to get a list of buckets stored in S3 using Java
- How to use aws s3 sync command
- How to copy object from one S3 bucket to another using Java
References :-
That’s all for how to delete a bucket in S3 using java language. If you liked it, please share your thoughts in comments section and share it with others too.