Menu
AWS SDK for Java
Developer Guide

Creating, Listing, and Deleting Amazon S3 Buckets

Every object (file) in Amazon S3 must reside within a bucket, which represents a collection (container) of objects. Each bucket is known by a key (name), which must be unique. For detailed information about buckets and their configuration, see Working with Amazon S3 Buckets in the Amazon S3 Developer Guide.

Note

These code snippets assume that you understand the material in Using the AWS SDK for Java and have configured default AWS credentials using the information in Set up AWS Credentials and Region for Development.

Create a Bucket

Use the AmazonS3 client's createBucket method. The new Bucket is returned. The createBucket method will raise an exception if the bucket already exists.

Note

To check whether a bucket already exists before attempting to create one with the same name, call the doesBucketExist method. It will return true if the bucket exists, and false otherwise.

Imports

Copy
import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.AmazonS3Exception; import com.amazonaws.services.s3.model.Bucket; import java.util.List;

Code

Copy
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); Bucket b = null; if (s3.doesBucketExist(bucket_name)) { System.out.format("Bucket %s already exists.\n", bucket_name); b = getBucket(bucket_name); } else { try { b = s3.createBucket(bucket_name); } catch (AmazonS3Exception e) { System.err.println(e.getErrorMessage()); } } return b;

See the complete example.

List Buckets

Use the AmazonS3 client's listBucket method. If successful, a list of Bucket is returned.

Imports

Copy
import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.Bucket; import java.util.List;

Code

Copy
inal AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); List<Bucket> buckets = s3.listBuckets(); System.out.println("Your Amazon S3 buckets are:"); for (Bucket b : buckets) { System.out.println("* " + b.getName()); }

See the complete example.

Delete a Bucket

Before you can delete an Amazon S3 bucket, you must ensure that the bucket is empty or an error will result. If you have a versioned bucket, you must also delete any versioned objects associated with the bucket.

Note

The complete example includes each of these steps in order, providing a complete solution for deleting an Amazon S3 bucket and its contents.

Remove Objects from an Unversioned Bucket Before Deleting It

Use the AmazonS3 client's listObjects method to retrieve the list of objects and deleteObject to delete each one.

Imports

Copy
import com.amazonaws.AmazonServiceException; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.S3ObjectSummary; import java.util.Iterator;

Code

Copy
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { System.out.println(" - removing objects from bucket"); ObjectListing object_listing = s3.listObjects(bucket_name); while (true) { for (Iterator<?> iterator = object_listing.getObjectSummaries().iterator(); iterator.hasNext();) { S3ObjectSummary summary = (S3ObjectSummary)iterator.next(); s3.deleteObject(bucket_name, summary.getKey()); } // more object_listing to retrieve? if (object_listing.isTruncated()) { object_listing = s3.listNextBatchOfObjects(object_listing); } else { break; } }; } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

See the complete example.

Remove Objects from a Versioned Bucket Before Deleting It

If you're using a versioned bucket, you also need to remove any stored versions of the objects in the bucket before the bucket can be deleted.

Using a pattern similar to the one used when removing objects within a bucket, remove versioned objects by using the AmazonS3 client's listVersions method to list any versioned objects, and then deleteVersion to delete each one.

Imports

Copy
import com.amazonaws.AmazonServiceException; 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; import java.util.Iterator;

Code

Copy
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { System.out.println(" - removing objects from bucket"); ObjectListing object_listing = s3.listObjects(bucket_name); while (true) { for (Iterator<?> iterator = object_listing.getObjectSummaries().iterator(); iterator.hasNext();) { S3ObjectSummary summary = (S3ObjectSummary)iterator.next(); s3.deleteObject(bucket_name, summary.getKey()); } // more object_listing to retrieve? if (object_listing.isTruncated()) { object_listing = s3.listNextBatchOfObjects(object_listing); } else { break; } }; System.out.println(" - removing versions from bucket"); VersionListing version_listing = s3.listVersions( new ListVersionsRequest().withBucketName(bucket_name)); while (true) { for (Iterator<?> iterator = version_listing.getVersionSummaries().iterator(); iterator.hasNext();) { S3VersionSummary vs = (S3VersionSummary)iterator.next(); s3.deleteVersion( bucket_name, vs.getKey(), vs.getVersionId()); } if (version_listing.isTruncated()) { version_listing = s3.listNextBatchOfVersions( version_listing); } else { break; } } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

See the complete example.

Delete an Empty Bucket

Once you remove the objects from a bucket (including any versioned objects), you can delete the bucket itself by using the AmazonS3 client's deleteBucket method.

Imports

Copy
import com.amazonaws.AmazonServiceException; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder;

Code

Copy
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { s3.deleteBucket(bucket_name); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

See the complete example.