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

The following examples show how to use software.amazon.awssdk.services.s3.model.CreateBucketRequest. 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: AmazonMachineLearningIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private static void setUpS3() {
    s3 = S3Client.builder()
                 .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
                 .region(Region.US_EAST_1)
                 .build();

    s3.createBucket(CreateBucketRequest.builder().bucket(BUCKET_NAME).build());

    Waiter.run(() -> s3.putObject(PutObjectRequest.builder()
                                                  .bucket(BUCKET_NAME)
                                                  .key(KEY)
                                                  .acl(ObjectCannedACL.PUBLIC_READ)
                                                  .build(),
                                  RequestBody.fromBytes(DATA.getBytes())))
          .ignoringException(NoSuchBucketException.class)
          .orFail();
}
 
Example #2
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 #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: 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 #5
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 #6
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_NullCreateBucketConfiguration() {
    CreateBucketRequest request = CreateBucketRequest.builder()
                                                     .bucket("test-bucket")
                                                     .build();

    Context.ModifyRequest context = () -> request;

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

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

    assertThat(locationConstraint).isEqualToIgnoringCase("us-east-2");
}
 
Example #7
Source File: S3PinotFSTest.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void setUp() {
  S3Mock s3Mock = S3Mock.getInstance();
  _s3Client = s3Mock.createS3ClientV2();
  _s3PinotFS = new S3PinotFS();
  _s3PinotFS.init(_s3Client);
  _s3Client.createBucket(CreateBucketRequest.builder().bucket(BUCKET).build());
}
 
Example #8
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] 
    }
 
Example #9
Source File: ServiceIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void setupResources() throws IOException, Exception {
    s3.createBucket(CreateBucketRequest.builder().bucket(BUCKET_NAME).build());
    topicArn = sns.createTopic(CreateTopicRequest.builder().name(TOPIC_NAME).build()).topicArn();
    policyArn = createPolicy();
    roleArn = createRole();
    iam.attachRolePolicy(AttachRolePolicyRequest.builder().roleName(ROLE_NAME).policyArn(policyArn).build());
}
 
Example #10
Source File: S3BundlePersistenceProviderIT.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    final Region region = Region.US_EAST_1;
    final String bucketName = "integration-test-" + System.currentTimeMillis();
    final String endpointUrl =
            //null;                     // When using AWS S3
            "http://localhost:9000";    // When using Docker:  docker run -it -p 9000:9000 minio/minio server /data

    // Create config context and provider, and call onConfigured
    final Map<String,String> properties = new HashMap<>();
    properties.put(S3BundlePersistenceProvider.REGION_PROP, region.id());
    properties.put(S3BundlePersistenceProvider.BUCKET_NAME_PROP, bucketName);
    properties.put(S3BundlePersistenceProvider.CREDENTIALS_PROVIDER_PROP,
            S3BundlePersistenceProvider.CredentialProvider.DEFAULT_CHAIN.name());
    properties.put(S3BundlePersistenceProvider.ENDPOINT_URL_PROP, endpointUrl);

    configurationContext = mock(ProviderConfigurationContext.class);
    when(configurationContext.getProperties()).thenReturn(properties);

    provider = new S3BundlePersistenceProvider();
    provider.onConfigured(configurationContext);

    // Create a separate client just for the IT test so we can setup a new bucket
    s3Client = ((S3BundlePersistenceProvider)provider).createS3Client(configurationContext);

    final CreateBucketRequest createBucketRequest = CreateBucketRequest.builder()
            .bucket(bucketName)
            .build();

    s3Client.createBucket(createBucketRequest);

}
 
Example #11
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 #12
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 #13
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 #14
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 #15
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void createBucket_InUsEast1_Succeeds() {
    US_EAST_1_CLIENT.createBucket(CreateBucketRequest.builder().bucket(US_EAST_1_BUCKET_NAME).build());

    String region = Waiter.run(() -> US_EAST_1_CLIENT.getBucketLocation(r -> r.bucket(US_EAST_1_BUCKET_NAME)))
                          .orFail()
                          .locationConstraintAsString();
    assertThat(region).isEqualToIgnoringCase("");
}
 
Example #16
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createBucket_ThrowsIllegalArgumentException_WhenBucketNameIsGreaterThan63Characters() {
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket("wajb5vwtlkhx4ow1t9e6l39rdy7amxxyttryfdw4y4nwomxervpti82lphi5plm8")
                                       .build());
}
 
Example #17
Source File: S3Utils.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public static CreateBucketRequest createBucketRequest(final String bucket) {
    return CreateBucketRequest.builder()
            .bucket(bucket)
            .build();
}
 
Example #18
Source File: S3TestConfiguration.java    From synapse with Apache License 2.0 4 votes vote down vote up
@Bean
public S3Client fakeS3Client() {
    final LocalS3Client localS3Client = new LocalS3Client();
    localS3Client.createBucket(CreateBucketRequest.builder().bucket(INTEGRATION_TEST_SNAPSHOT_BUCKET).build());
    return localS3Client;
}
 
Example #19
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createBucket_ThrowsIllegalArgumentException_WhenBucketNameIsLessThan3Characters() {
    s3.createBucket(CreateBucketRequest.builder().bucket("s3").build());
}
 
Example #20
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createBucket_ThrowsIllegalArgumentException_WhenBucketNameIsIpAddress() {
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket("127.0.0.1")
                                       .build());
}
 
Example #21
Source File: EndpointAddressInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void accelerateEnabled_CreateBucketsRequest_shouldNotConvertToAccelerateEndpoint() {
    verifyAccelerateDisabledOperationsEndpointNotConverted(CreateBucketRequest.builder().build());
}
 
Example #22
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createBucket_ThrowsIllegalArgumentException_WhenBucketNameHasDashAdjacentAPeriod() {
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket("-.dashperiod")
                                       .build());
}
 
Example #23
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createBucket_ThrowsIllegalArgumentException_WhenBucketNameBeginsWithADash() {
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket("-dash")
                                       .build());
}
 
Example #24
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createBucket_ThrowsIllegalArgumentException_WhenBucketNameContainsAdjacentPeriods() {
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket("..period")
                                       .build());
}
 
Example #25
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createBucket_ThrowsIllegalArgumentException_WhenBucketNameBeginsWithPeriod() {
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket(".period")
                                       .build());
}
 
Example #26
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createBucket_ThrowsIllegalArgumentException_WhenBucketNameContainsWhiteSpace() {
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket("white space")
                                       .build());
}
 
Example #27
Source File: CreateBucketIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createBucket_ThrowsIllegalArgumentException_WhenBucketNameContainsUpperCaseCharacters() {
    s3.createBucket(CreateBucketRequest.builder()
                                       .bucket("UPPERCASE")
                                       .build());
}