This example attempts to create a bucket called 'hello-world' and, as the bucket hello-world has already been created by someone else in S3's global namespace, throws the following exception. Change 'hello-world' to something else to avoid the exception by creating a uniquely named bucket. The new bucket so created can be deleted using the AWS console
Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again. (Service: Amazon S3; Status Code: 409; Error Code: BucketAlreadyExists; Request ID: ...
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CreateBucketRequest;
import com.amazonaws.services.s3.model.Bucket;
/** S3 "hello world" example. */
public class S3Hello {
/** Name of hello-world bucket -- must be globally unique. The
* bucket namespace is shared by all users of the system.
*/
static final String BUCKET_NAME = "hello-world";
/** Creates bucket
* @param args Command line arguments
*/
public static void main(final String[] args) {
AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
CreateBucketRequest request
= new CreateBucketRequest(BUCKET_NAME);
Bucket bucket = s3.createBucket(request);
System.out.println("S3 Hello World completed.");
}
}
This example requires the following dependencies:
credentials
set up in .aws under your home directory. https://aws.amazon.com/developers/getting-started/java/