Search in sources :

Example 11 with CreateBucketRequest

use of com.amazonaws.services.s3.model.CreateBucketRequest in project flink by apache.

the class AWSServicesTestUtils method createBucket.

public static void createBucket(S3AsyncClient s3Client, String bucketName) throws ExecutionException, InterruptedException {
    CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();
    s3Client.createBucket(bucketRequest);
    HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
    try (final S3AsyncWaiter waiter = s3Client.waiter()) {
        waiter.waitUntilBucketExists(bucketRequestWait).get();
    }
}
Also used : S3AsyncWaiter(software.amazon.awssdk.services.s3.waiters.S3AsyncWaiter) HeadBucketRequest(software.amazon.awssdk.services.s3.model.HeadBucketRequest) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest)

Example 12 with CreateBucketRequest

use of com.amazonaws.services.s3.model.CreateBucketRequest 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 (com.amazonaws.services.s3.model.CreateBucketRequest)5 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)5 HeadBucketRequest (software.amazon.awssdk.services.s3.model.HeadBucketRequest)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 HeadBucketResponse (software.amazon.awssdk.services.s3.model.HeadBucketResponse)3 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)3 S3Waiter (software.amazon.awssdk.services.s3.waiters.S3Waiter)3 SdkClientException (com.amazonaws.SdkClientException)2 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)2 Regions (com.amazonaws.regions.Regions)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 IOException (java.io.IOException)2 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)1 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 Bucket (com.amazonaws.services.s3.model.Bucket)1 BucketVersioningConfiguration (com.amazonaws.services.s3.model.BucketVersioningConfiguration)1 GetBucketLocationRequest (com.amazonaws.services.s3.model.GetBucketLocationRequest)1 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)1 Owner (com.amazonaws.services.s3.model.Owner)1