Created
September 2, 2015 07:32
-
-
Save honux77/1a477cde2c3c27582afe to your computer and use it in GitHub Desktop.
delete s3 bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Delete the specified bucket. You will use the deleteBucket() method of the client object to | |
| * delete the bucket, but first you will need to delete the bucket contents. To delete the contents, | |
| * you will need to list the objects and delete them individually (DeleteObject() method) or as a | |
| * batch (DeleteObjects() method). | |
| * | |
| * The purpose of this task is to gain experience writing applications that remove unused AWS resources | |
| * in an automated manner. | |
| * | |
| * @param s3Client The S3 client object. | |
| * @param bucketName The name of the bucket to delete. | |
| */ | |
| @Override | |
| public void deleteBucket(AmazonS3 s3Client, String bucketName) { | |
| try { | |
| s3Client.deleteBucket(bucketName); | |
| return; | |
| } catch (AmazonS3Exception e) { | |
| if (!e.getErrorCode().equals("BucketNotEmpty")) | |
| throw e; | |
| else | |
| System.err.println("Bucket is not empty"); | |
| } | |
| List<S3ObjectSummary> s3list = s3Client.listObjects(bucketName).getObjectSummaries(); | |
| for(S3ObjectSummary s: s3list) { | |
| System.out.println("deleting " + s.getKey()); | |
| s3Client.deleteObject(bucketName, s.getKey()); | |
| } | |
| System.err.println("try deleting bucket again"); | |
| s3Client.deleteBucket(bucketName); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment