AWS SDK for Java
Developer Guide

Creating, Listing, and Deleting Amazon S3 Buckets

Important

This is a preview release and is not recommended for production environments.

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

Note

Best Practice

We recommend that you enable the AbortIncompleteMultipartUpload lifecycle rule on your Amazon S3 buckets.

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 in the Amazon S3 User Guide.

Note

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

Create a Bucket

Build a CreateBucketRequest and provide a bucket name. Pass it to the S3Client's createBucket method. Use the S3Client to do additional operations such as listing or deleting buckets as shown in later examples.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; import software.amazon.awssdk.services.s3.model.CreateBucketRequest;

Code

Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder().region(region).build(); String bucket = "bucket" + System.currentTimeMillis(); CreateBucketRequest createBucketRequest = CreateBucketRequest .builder() .bucket(bucket) .createBucketConfiguration(CreateBucketConfiguration.builder() .locationConstraint(region.value()) .build()) .build(); s3.createBucket(createBucketRequest);

See the complete example on GitHub.

List the Buckets

Build a ListBucketRequest. Use the S3Client's listBuckets method to retrieve the list of buckets. If the request succeeds a ListBucketsResponse is returned. Use this response object to retrieve the list of buckets.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.model.ListBucketsRequest; import software.amazon.awssdk.services.s3.model.ListBucketsResponse;

Code

Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder().region(region).build(); // List buckets ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build(); ListBucketsResponse listBucketsResponse = s3.listBuckets(listBucketsRequest); listBucketsResponse.buckets().stream().forEach(x -> System.out.println(x.name()));

See the complete example on GitHub.

Delete a Bucket

Before you can delete an Amazon S3 bucket, you must ensure that the bucket is empty or the service will return an error. If you have a versioned bucket, you must also delete any versioned objects that are in the bucket.

Delete Objects in a Bucket

Build a ListObjectsV2Request and use the S3Client's listObjects method to retrieve the list of objects in the bucket. Then use the deleteObject method on each object to delete it.

Imports

import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;

Code

ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket2).build(); ListObjectsV2Response listObjectsV2Response; do { listObjectsV2Response = s3.listObjectsV2(listObjectsV2Request); for (S3Object s3Object : listObjectsV2Response.contents()) { s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket2).key(s3Object.key()).build()); } listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket2) .continuationToken(listObjectsV2Response.nextContinuationToken()) .build(); } while (listObjectsV2Response.isTruncated());

See the complete example on GitHub.

Delete an Empty Bucket

Build a DeleteBucketRequest with a bucket name and pass it to the S3Client's deleteBucket method.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;

Code

Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder().region(region).build(); DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build(); s3.deleteBucket(deleteBucketRequest);

See the complete example on GitHub.