Search in sources :

Example 1 with CreateBucketRequest

use of com.amazonaws.services.s3.model.CreateBucketRequest 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 CreateBucketRequest

use of com.amazonaws.services.s3.model.CreateBucketRequest in project aws-doc-sdk-examples by awsdocs.

the class CreateBucket method createBucket.

// snippet-start:[s3.java2.create_bucket_waiters.main]
public static void createBucket(S3Client s3Client, String bucketName) {
    try {
        S3Waiter s3Waiter = s3Client.waiter();
        CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).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 3 with CreateBucketRequest

use of com.amazonaws.services.s3.model.CreateBucketRequest in project aws-doc-sdk-examples by awsdocs.

the class MovieLensDatasetProvider method createBucket.

// Create a bucket by using a S3Waiter object
public static void createBucket(S3Client s3Client, String bucketName) {
    try {
        S3Waiter s3Waiter = s3Client.waiter();
        CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).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 4 with CreateBucketRequest

use of com.amazonaws.services.s3.model.CreateBucketRequest in project aws-doc-sdk-examples by awsdocs.

the class CrossRegionReplication method createBucket.

private static void createBucket(AmazonS3 s3Client, Regions region, String bucketName) {
    CreateBucketRequest request = new CreateBucketRequest(bucketName, region.getName());
    s3Client.createBucket(request);
    BucketVersioningConfiguration configuration = new BucketVersioningConfiguration().withStatus(BucketVersioningConfiguration.ENABLED);
    SetBucketVersioningConfigurationRequest enableVersioningRequest = new SetBucketVersioningConfigurationRequest(bucketName, configuration);
    s3Client.setBucketVersioningConfiguration(enableVersioningRequest);
}
Also used : CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) SetBucketVersioningConfigurationRequest(com.amazonaws.services.s3.model.SetBucketVersioningConfigurationRequest) BucketVersioningConfiguration(com.amazonaws.services.s3.model.BucketVersioningConfiguration)

Example 5 with CreateBucketRequest

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

the class S3Endpoint method doStart.

@Override
public void doStart() throws Exception {
    super.doStart();
    s3Client = configuration.getAmazonS3Client() != null ? configuration.getAmazonS3Client() : createS3Client();
    if (ObjectHelper.isNotEmpty(configuration.getAmazonS3Endpoint())) {
        s3Client.setEndpoint(configuration.getAmazonS3Endpoint());
    }
    String fileName = getConfiguration().getFileName();
    if (fileName != null) {
        LOG.trace("File name [{}] requested, so skipping bucket check...", fileName);
        return;
    }
    String bucketName = getConfiguration().getBucketName();
    LOG.trace("Querying whether bucket [{}] already exists...", bucketName);
    String prefix = getConfiguration().getPrefix();
    try {
        s3Client.listObjects(new ListObjectsRequest(bucketName, prefix, null, null, 0));
        LOG.trace("Bucket [{}] already exists", bucketName);
        return;
    } catch (AmazonServiceException ase) {
        /* 404 means the bucket doesn't exist */
        if (ase.getStatusCode() != 404) {
            throw ase;
        }
    }
    LOG.trace("Bucket [{}] doesn't exist yet", bucketName);
    // creates the new bucket because it doesn't exist yet
    CreateBucketRequest createBucketRequest = new CreateBucketRequest(getConfiguration().getBucketName());
    if (getConfiguration().getRegion() != null) {
        createBucketRequest.setRegion(getConfiguration().getRegion());
    }
    LOG.trace("Creating bucket [{}] in region [{}] with request [{}]...", configuration.getBucketName(), configuration.getRegion(), createBucketRequest);
    s3Client.createBucket(createBucketRequest);
    LOG.trace("Bucket created");
    if (configuration.getPolicy() != null) {
        LOG.trace("Updating bucket [{}] with policy [{}]", bucketName, configuration.getPolicy());
        s3Client.setBucketPolicy(bucketName, configuration.getPolicy());
        LOG.trace("Bucket policy updated");
    }
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) AmazonServiceException(com.amazonaws.AmazonServiceException)

Aggregations

CreateBucketRequest (com.amazonaws.services.s3.model.CreateBucketRequest)5 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)4 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 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 SetBucketVersioningConfigurationRequest (com.amazonaws.services.s3.model.SetBucketVersioningConfigurationRequest)1