I am trying to check if a bucket exists, and delete it if it does, if not throw some error.
Using the AmazonClientBuilder
class, I am using the doesBucketExist
method to check if a bucket is there, and using the deleteBucket
method to delete that bucket if it exists. If it doesn't exist state it so.
NOTE: I have made sure I have full access to do all operations on the bucket.
But when I run the code, the output show only the bucket name, but not if it is deleted or not. To verify if bucket is deleted, I am doing a System.out.println
, so that it shows that the bucket is deleted on the screen.
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
public class DeleteBucket
{
private static String bucketName = "mycalibucket1";
public static void main(String[] args)
{
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(Regions.US_WEST_2)
.build();
try
{
if(s3Client.doesBucketExist(bucketName))
{
System.out.printf("\n",bucketName," exists");
s3Client.deleteBucket(bucketName);
System.out.println("----------------------------------------------");
System.out.printf("\n",bucketName," is deleted.");
}
}
catch(AmazonS3Exception e)
{
System.out.printf(bucketName," does not exist.");
System.exit(1);
}
}
}
OUTPUT:
mycalibucket1
Can somebody help me?