use of software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException 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);
}
}
Aggregations