The Amazon SDK for Java 1.x has entered maintenance mode as of July 31, 2024,
and will reach end-of-support
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
Note
Best Practice
We recommend that you enable the AbortIncompleteMultipartUpload
This rule directs Amazon S3 to abort multipart uploads that don’t complete within a specified number of days after being initiated. When the set time limit is exceeded, Amazon S3 aborts the upload and then deletes the incomplete upload data.
For more information, see Lifecycle Configuration for a Bucket with Versioning
Note
These code examples assume that you understand the material in Using the Amazon SDK for Java and have configured default Amazon credentials using the information in Set up Amazon 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
import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.AmazonS3Exception; import com.amazonaws.services.s3.model.Bucket; import java.util.List;
Code
if (s3.doesBucketExistV2(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
import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.Bucket; import java.util.List;
Code
List<Bucket> buckets = s3.listBuckets(); System.out.println("Your {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
Note
The complete example
Topics
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
import com.amazonaws.AmazonServiceException; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import java.util.Iterator;
Code
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; } }
See the complete example
Remove Objects from a Versioned Bucket Before Deleting It
If you’re using a versioned bucket
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
import com.amazonaws.AmazonServiceException; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import java.util.Iterator;
Code
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; } }
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
import com.amazonaws.AmazonServiceException; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import java.util.Iterator;
Code
System.out.println(" OK, bucket ready to delete!"); s3.deleteBucket(bucket_name);
See the complete example