amazon web services - delete an empty S3 bucket using `deleteBucket` method using Java -
i trying check if bucket exists, , delete if does, if not throw error.
using amazonclientbuilder class, using doesbucketexist method check if bucket there, , using deletebucket method delete bucket if exists. if doesn't exist state so.
note: have made sure have full access operations on bucket.
but when run code, output show bucket name, not if deleted or not. verify if bucket deleted, doing system.out.println, shows bucket deleted on 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," deleted."); } } catch(amazons3exception e) { system.out.printf(bucketname," not exist."); system.exit(1); } } } output:
mycalibucket1
can me?
your code must throwing exception other amazons3exception if not reaching 2nd , 3rd print statements.
looking @ api javadoc, http://docs.aws.amazon.com/awsjavasdk/latest/javadoc/com/amazonaws/services/s3/amazons3.html#deletebucket-java.lang.string-
it looks deletebucket method calling may throw 2 different runtime exceptions neither of derive amazons3exception. try catching com.amazonaws.amazonclientexception looks common both. see tells , maybe able figure out causing problem.
docs note bucket must empty deleted.
Comments
Post a Comment