Search in sources :

Example 1 with CreateBucketConfiguration

use of software.amazon.awssdk.services.s3.model.CreateBucketConfiguration in project aws-doc-sdk-examples by awsdocs.

the class S3ObjectOperations method createBucket.

// Create a bucket by using a S3Waiter object
public static void createBucket(S3Client s3Client, String bucketName, Region region) {
    S3Waiter s3Waiter = s3Client.waiter();
    try {
        CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).createBucketConfiguration(CreateBucketConfiguration.builder().locationConstraint(region.id()).build()).build();
        s3Client.createBucket(bucketRequest);
        HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
        // Wait until the bucket is created and print out the response
        WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
        waiterResponse.matched().response().ifPresent(System.out::println);
        System.out.println(bucketName + " is ready");
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : HeadBucketRequest(software.amazon.awssdk.services.s3.model.HeadBucketRequest) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest) HeadBucketResponse(software.amazon.awssdk.services.s3.model.HeadBucketResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Waiter(software.amazon.awssdk.services.s3.waiters.S3Waiter)

Example 2 with CreateBucketConfiguration

use of software.amazon.awssdk.services.s3.model.CreateBucketConfiguration in project data-transfer-project by google.

the class BackblazeDataTransferClient method getOrCreateBucket.

private String getOrCreateBucket(S3Client s3Client, ListBucketsResponse listBucketsResponse, String region, String exportService) throws IOException {
    String fullPrefix = String.format(DATA_TRANSFER_BUCKET_PREFIX_FORMAT_STRING, exportService.toLowerCase());
    try {
        for (Bucket bucket : listBucketsResponse.buckets()) {
            if (bucket.name().startsWith(fullPrefix)) {
                return bucket.name();
            }
        }
        for (int i = 0; i < MAX_BUCKET_CREATION_ATTEMPTS; i++) {
            String bucketName = String.format("%s-%s", fullPrefix, RandomStringUtils.randomNumeric(8).toLowerCase());
            try {
                CreateBucketConfiguration createBucketConfiguration = CreateBucketConfiguration.builder().locationConstraint(region).build();
                CreateBucketRequest createBucketRequest = CreateBucketRequest.builder().bucket(bucketName).createBucketConfiguration(createBucketConfiguration).build();
                s3Client.createBucket(createBucketRequest);
                return bucketName;
            } catch (BucketAlreadyExistsException | BucketAlreadyOwnedByYouException e) {
                monitor.info(() -> "Bucket name already exists");
            }
        }
        throw new IOException(String.format("Failed to create a uniquely named bucket after %d attempts", MAX_BUCKET_CREATION_ATTEMPTS));
    } catch (AwsServiceException | SdkClientException e) {
        throw new IOException("Error while creating bucket", e);
    }
}
Also used : CreateBucketConfiguration(software.amazon.awssdk.services.s3.model.CreateBucketConfiguration) BucketAlreadyOwnedByYouException(software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException) BucketAlreadyExistsException(software.amazon.awssdk.services.s3.model.BucketAlreadyExistsException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) Bucket(software.amazon.awssdk.services.s3.model.Bucket) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) IOException(java.io.IOException)

Aggregations

CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)2 IOException (java.io.IOException)1 AwsServiceException (software.amazon.awssdk.awscore.exception.AwsServiceException)1 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)1 Bucket (software.amazon.awssdk.services.s3.model.Bucket)1 BucketAlreadyExistsException (software.amazon.awssdk.services.s3.model.BucketAlreadyExistsException)1 BucketAlreadyOwnedByYouException (software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException)1 CreateBucketConfiguration (software.amazon.awssdk.services.s3.model.CreateBucketConfiguration)1 HeadBucketRequest (software.amazon.awssdk.services.s3.model.HeadBucketRequest)1 HeadBucketResponse (software.amazon.awssdk.services.s3.model.HeadBucketResponse)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1 S3Waiter (software.amazon.awssdk.services.s3.waiters.S3Waiter)1