software.amazon.awssdk.services.s3.model.CreateBucketConfiguration Java Examples

The following examples show how to use software.amazon.awssdk.services.s3.model.CreateBucketConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CreateBucketInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void modifyRequest_UpdatesLocationConstraint_When_NullLocationConstraint() {
    CreateBucketRequest request = CreateBucketRequest.builder()
                                                     .bucket("test-bucket")
                                                     .createBucketConfiguration(CreateBucketConfiguration.builder()
                                                                                                         .build())
                                                     .build();

    Context.ModifyRequest context = () -> request;

    ExecutionAttributes attributes = new ExecutionAttributes()
            .putAttribute(AwsExecutionAttribute.AWS_REGION, Region.US_WEST_2);

    SdkRequest modifiedRequest = new CreateBucketInterceptor().modifyRequest(context, attributes);
    String locationConstraint = ((CreateBucketRequest) modifiedRequest).createBucketConfiguration().locationConstraintAsString();

    assertThat(locationConstraint).isEqualToIgnoringCase("us-west-2");
}
 
Example #2
Source File: CreateBucketInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * For us-east-1 there must not be a location constraint (or containing CreateBucketConfiguration) sent.
 */
@Test
public void modifyRequest_UsEast1_UsesNullBucketConfiguration() {
    CreateBucketRequest request = CreateBucketRequest.builder()
                                                     .bucket("test-bucket")
                                                     .createBucketConfiguration(CreateBucketConfiguration.builder()
                                                                                                         .build())
                                                     .build();

    Context.ModifyRequest context = () -> request;

    ExecutionAttributes attributes = new ExecutionAttributes()
            .putAttribute(AwsExecutionAttribute.AWS_REGION, Region.US_EAST_1);

    SdkRequest modifiedRequest = new CreateBucketInterceptor().modifyRequest(context, attributes);
    assertThat(((CreateBucketRequest) modifiedRequest).createBucketConfiguration()).isNull();
}
 
Example #3
Source File: S3ObjectOperations.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void createBucket( S3Client s3Client, String bucketName, Region region) {
    
    try{
        s3Client.createBucket(CreateBucketRequest
            .builder()
            .bucket(bucketName)
            .createBucketConfiguration(
                    CreateBucketConfiguration.builder()
                            .locationConstraint(region.id())
                            .build())
            .build());

        System.out.println(bucketName);
     } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
 
Example #4
Source File: Synch.java    From pegasus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a S3 bucket with the given name
 *
 * @param name
 * @return boolean true in case a bucket was created, false if already exists,
 * @throws S3Exception in case unable to create bucket
 */
public boolean createS3Bucket(String name) {
    S3Client s3Client = S3Client.builder().region(mAWSRegion).build();
    boolean created = true;
    try {
        CreateBucketResponse cbr =
                s3Client.createBucket(
                        CreateBucketRequest.builder()
                                .bucket(name)
                                .createBucketConfiguration(
                                        CreateBucketConfiguration.builder()
                                                .locationConstraint(mAWSRegion.value())
                                                .build())
                                .build());
    } catch (S3Exception ex) {
        if (ex.getErrorCode().equals("BucketAlreadyOwnedByYou")) {
            created = false;
        } else {
            // rethrow the exception as is
            throw ex;
        }
    }

    return created;
}
 
Example #5
Source File: CreateBucketInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private CreateBucketConfiguration toLocationConstraint(Region region) {
    if (region.equals(Region.US_EAST_1)) {
        // us-east-1 requires no location restraint. See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html
        return null;
    }
    return CreateBucketConfiguration.builder()
                                    .locationConstraint(region.id())
                                    .build();
}
 
Example #6
Source File: BucketAnalyticsConfigurationIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpFixture() throws Exception {
    S3IntegrationTestBase.setUp();
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket(BUCKET_NAME)
                                       .createBucketConfiguration(CreateBucketConfiguration.builder()
                                                                                           .locationConstraint("us-west-2")
                                                                                           .build())
                                       .build());

    s3.putObject(PutObjectRequest.builder()
                                 .bucket(BUCKET_NAME)
                                 .key(KEY)
                                 .build(), RequestBody.fromFile(new RandomTempFile("foo", 1024)));
}
 
Example #7
Source File: S3IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static void createBucket(String bucketName, int retryCount) {
    try {
        s3.createBucket(
            CreateBucketRequest.builder()
                               .bucket(bucketName)
                               .createBucketConfiguration(
                                   CreateBucketConfiguration.builder()
                                                            .locationConstraint(BucketLocationConstraint.US_WEST_2)
                                                            .build())
                               .build());
    } catch (S3Exception e) {
        System.err.println("Error attempting to create bucket: " + bucketName);
        if (e.awsErrorDetails().errorCode().equals("BucketAlreadyOwnedByYou")) {
            System.err.printf("%s bucket already exists, likely leaked by a previous run\n", bucketName);
        } else if (e.awsErrorDetails().errorCode().equals("TooManyBuckets")) {
            System.err.println("Printing all buckets for debug:");
            s3.listBuckets().buckets().forEach(System.err::println);
            if (retryCount < 2) {
                System.err.println("Retrying...");
                createBucket(bucketName, retryCount + 1);
            } else {
                throw e;
            }
        } else {
            throw e;
        }
    }
}
 
Example #8
Source File: CloudTrailIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws IOException {
    IntegrationTestBase.setUp();
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket(BUCKET_NAME)
                                       .createBucketConfiguration(
                                               CreateBucketConfiguration.builder()
                                                                        .locationConstraint(region.id())
                                                                        .build())
                                       .build());
}
 
Example #9
Source File: S3WithUrlHttpClientIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static void createBucket(String bucket, Region region) {
    s3.createBucket(CreateBucketRequest
                        .builder()
                        .bucket(bucket)
                        .createBucketConfiguration(
                            CreateBucketConfiguration.builder()
                                                     .locationConstraint(region.id())
                                                     .build())
                        .build());
}
 
Example #10
Source File: S3BucketOps.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        // snippet-start:[s3.java2.s3_bucket_ops.create_bucket]
        // snippet-start:[s3.java2.s3_bucket_ops.region]
        Region region = Region.US_WEST_2;
        S3Client s3 = S3Client.builder().region(region).build();
        // snippet-end:[s3.java2.s3_bucket_ops.region]
        String bucket = "bucket" + System.currentTimeMillis();
        System.out.println(bucket);

        // Create bucket
        CreateBucketRequest createBucketRequest = CreateBucketRequest
                .builder()
                .bucket(bucket)
                .createBucketConfiguration(CreateBucketConfiguration.builder()
                        .locationConstraint(region.id())
                        .build())
                .build();
        s3.createBucket(createBucketRequest);
        // snippet-end:[s3.java2.s3_bucket_ops.create_bucket]

        // snippet-start:[s3.java2.s3_bucket_ops.list_bucket]
        // List buckets
        ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build();
        ListBucketsResponse listBucketsResponse = s3.listBuckets(listBucketsRequest);
        listBucketsResponse.buckets().stream().forEach(x -> System.out.println(x.name()));
        // snippet-end:[s3.java2.s3_bucket_ops.list_bucket]

        // Delete empty bucket
        // snippet-start:[s3.java2.s3_bucket_ops.delete_bucket]      
        DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build();
        s3.deleteBucket(deleteBucketRequest);
        // snippet-end:[s3.java2.s3_bucket_ops.delete_bucket] 
    }