There are more AWS SDK examples available in the AWS Doc SDK Examples
Use CreateBucket
with an AWS SDK
The following code example shows how to use CreateBucket
.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Create an S3 directory bucket.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.BucketInfo; import software.amazon.awssdk.services.s3.model.BucketType; import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.CreateBucketResponse; import software.amazon.awssdk.services.s3.model.DataRedundancy; import software.amazon.awssdk.services.s3.model.LocationInfo; import software.amazon.awssdk.services.s3.model.LocationType; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; /** * Creates a new S3 directory bucket in a specified Zone (For example, a * specified Availability Zone in this code example). * * @param s3Client The S3 client used to create the bucket * @param bucketName The name of the bucket to be created * @param zone The region where the bucket will be created * @throws S3Exception if there's an error creating the bucket */ public static void createDirectoryBucket(S3Client s3Client, String bucketName, String zone) throws S3Exception { logger.info("Creating bucket: {}", bucketName); CreateBucketConfiguration bucketConfiguration = CreateBucketConfiguration.builder() .location(LocationInfo.builder() .type(LocationType.AVAILABILITY_ZONE) .name(zone).build()) .bucket(BucketInfo.builder() .type(BucketType.DIRECTORY) .dataRedundancy(DataRedundancy.SINGLE_AVAILABILITY_ZONE) .build()) .build(); try { CreateBucketRequest bucketRequest = CreateBucketRequest.builder() .bucket(bucketName) .createBucketConfiguration(bucketConfiguration).build(); CreateBucketResponse response = s3Client.createBucket(bucketRequest); logger.info("Bucket created successfully with location: {}", response.location()); } catch (S3Exception e) { logger.error("Error creating bucket: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
-
For API details, see CreateBucket in AWS SDK for Java 2.x API Reference.
-
CopyObject
CreateMultipartUpload