Search in sources :

Example 1 with GetBucketLocationRequest

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

the class CreateBucket2 method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        if (!s3Client.doesBucketExistV2(bucketName)) {
            // Because the CreateBucketRequest object doesn't specify a region, the
            // bucket is created in the region specified in the client.
            s3Client.createBucket(new CreateBucketRequest(bucketName));
            // Verify that the bucket was created by retrieving it and checking its location.
            String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucketName));
            System.out.println("Bucket location: " + bucketLocation);
        }
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it and returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Regions(com.amazonaws.regions.Regions) GetBucketLocationRequest(com.amazonaws.services.s3.model.GetBucketLocationRequest)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)1 SdkClientException (com.amazonaws.SdkClientException)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 Regions (com.amazonaws.regions.Regions)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 CreateBucketRequest (com.amazonaws.services.s3.model.CreateBucketRequest)1 GetBucketLocationRequest (com.amazonaws.services.s3.model.GetBucketLocationRequest)1