AWS Code Sample
Catalog demonstrates how to list, create, and delete a bucket in Amazon S3.

/** * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ package; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import; import; import; import; import; import java.util.List; public class App { private static AmazonS3 s3; public static void main( String[] args ) { if (args.length < 2) { System.out.format("Usage: <the bucket name> <the AWS Region to use>\n" + "Example: my-test-bucket us-east-2\n"); return; } String bucket_name = args[0]; String region = args[1]; s3 = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(region) .build(); // List current buckets. ListMyBuckets(); // Create the bucket. if (s3.doesBucketExistV2(bucket_name)) { System.out.format("\nCannot create the bucket. \n" + "A bucket named '%s' already exists.", bucket_name); return; } else { try { System.out.format("\nCreating a new bucket named '%s'...\n\n", bucket_name); s3.createBucket(new CreateBucketRequest(bucket_name, region)); } catch (AmazonS3Exception e) { System.err.println(e.getErrorMessage()); } } // Confirm that the bucket was created. ListMyBuckets(); // Delete the bucket. try { System.out.format("\nDeleting the bucket named '%s'...\n\n", bucket_name); s3.deleteBucket(bucket_name); } catch (AmazonS3Exception e) { System.err.println(e.getErrorMessage()); } // Confirm that the bucket was deleted. ListMyBuckets(); } private static void ListMyBuckets() { List<Bucket> buckets = s3.listBuckets(); System.out.println("My buckets now are:"); for (Bucket b : buckets) { System.out.println(b.getName()); } } }

Sample Details

Service: s3

Last tested: 2018-05-29

Author: AWS

Type: full-example

On this page: