Search in sources :

Example 56 with AmazonS3Exception

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

the class CreateBucket method createBucket.

public static Bucket createBucket(String bucket_name) {
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    Bucket b = null;
    if (s3.doesBucketExistV2(bucket_name)) {
        System.out.format("Bucket %s already exists.\n", bucket_name);
        b = getBucket(bucket_name);
    } else {
        try {
            b = s3.createBucket(bucket_name);
        } catch (AmazonS3Exception e) {
            System.err.println(e.getErrorMessage());
        }
    }
    return b;
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) Bucket(com.amazonaws.services.s3.model.Bucket) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Example 57 with AmazonS3Exception

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

the class App method main.

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.format("Usage: <the bucket name> <the AWS Region to use>\n" + "Example: my-test-bucket us-east-2\n");
        return;
    }
    String bucket_name = args[0];
    String region = args[1];
    s3 = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(region).build();
    // List current buckets.
    ListMyBuckets();
    // Create the bucket.
    if (s3.doesBucketExistV2(bucket_name)) {
        System.out.format("\nCannot create the bucket. \n" + "A bucket named '%s' already exists.", bucket_name);
        return;
    } else {
        try {
            System.out.format("\nCreating a new bucket named '%s'...\n\n", bucket_name);
            s3.createBucket(new CreateBucketRequest(bucket_name, region));
        } catch (AmazonS3Exception e) {
            System.err.println(e.getErrorMessage());
        }
    }
    // Confirm that the bucket was created.
    ListMyBuckets();
    // Delete the bucket.
    try {
        System.out.format("\nDeleting the bucket named '%s'...\n\n", bucket_name);
        s3.deleteBucket(bucket_name);
    } catch (AmazonS3Exception e) {
        System.err.println(e.getErrorMessage());
    }
    // Confirm that the bucket was deleted.
    ListMyBuckets();
}
Also used : CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Example 58 with AmazonS3Exception

use of com.amazonaws.services.s3.model.AmazonS3Exception in project beam by apache.

the class S3FileSystemTest method matchNonGlobNotFoundWithS3Options.

@Test
public void matchNonGlobNotFoundWithS3Options() {
    S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options());
    S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/nonexistentfile");
    AmazonS3Exception exception = new AmazonS3Exception("mock exception");
    exception.setStatusCode(404);
    when(s3FileSystem.getAmazonS3Client().getObjectMetadata(argThat(new GetObjectMetadataRequestMatcher(new GetObjectMetadataRequest(path.getBucket(), path.getKey()))))).thenThrow(exception);
    MatchResult result = s3FileSystem.matchNonGlobPath(path);
    assertThat(result, MatchResultMatcher.create(MatchResult.Status.NOT_FOUND, new FileNotFoundException()));
}
Also used : GetObjectMetadataRequest(com.amazonaws.services.s3.model.GetObjectMetadataRequest) FileNotFoundException(java.io.FileNotFoundException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) MatchResult(org.apache.beam.sdk.io.fs.MatchResult) S3TestUtils.buildMockedS3FileSystem(org.apache.beam.sdk.io.aws.s3.S3TestUtils.buildMockedS3FileSystem) Test(org.junit.Test)

Example 59 with AmazonS3Exception

use of com.amazonaws.services.s3.model.AmazonS3Exception in project beam by apache.

the class S3FileSystemTest method matchNonGlobNotFound.

@Test
public void matchNonGlobNotFound() {
    S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("mys3"));
    S3ResourceId path = S3ResourceId.fromUri("mys3://testbucket/testdirectory/nonexistentfile");
    AmazonS3Exception exception = new AmazonS3Exception("mock exception");
    exception.setStatusCode(404);
    when(s3FileSystem.getAmazonS3Client().getObjectMetadata(argThat(new GetObjectMetadataRequestMatcher(new GetObjectMetadataRequest(path.getBucket(), path.getKey()))))).thenThrow(exception);
    MatchResult result = s3FileSystem.matchNonGlobPath(path);
    assertThat(result, MatchResultMatcher.create(MatchResult.Status.NOT_FOUND, new FileNotFoundException()));
}
Also used : GetObjectMetadataRequest(com.amazonaws.services.s3.model.GetObjectMetadataRequest) FileNotFoundException(java.io.FileNotFoundException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) MatchResult(org.apache.beam.sdk.io.fs.MatchResult) S3TestUtils.buildMockedS3FileSystem(org.apache.beam.sdk.io.aws.s3.S3TestUtils.buildMockedS3FileSystem) Test(org.junit.Test)

Example 60 with AmazonS3Exception

use of com.amazonaws.services.s3.model.AmazonS3Exception in project beam by apache.

the class S3FileSystemTest method matchNonGlobForbidden.

@Test
public void matchNonGlobForbidden() {
    S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("s3"));
    AmazonS3Exception exception = new AmazonS3Exception("mock exception");
    exception.setStatusCode(403);
    S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/keyname");
    when(s3FileSystem.getAmazonS3Client().getObjectMetadata(argThat(new GetObjectMetadataRequestMatcher(new GetObjectMetadataRequest(path.getBucket(), path.getKey()))))).thenThrow(exception);
    assertThat(s3FileSystem.matchNonGlobPath(path), MatchResultMatcher.create(MatchResult.Status.ERROR, new IOException(exception)));
}
Also used : GetObjectMetadataRequest(com.amazonaws.services.s3.model.GetObjectMetadataRequest) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) S3TestUtils.buildMockedS3FileSystem(org.apache.beam.sdk.io.aws.s3.S3TestUtils.buildMockedS3FileSystem) Test(org.junit.Test)

Aggregations

AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)62 IOException (java.io.IOException)23 Test (org.junit.Test)13 FileNotFoundException (java.io.FileNotFoundException)10 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)9 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)9 AmazonServiceException (com.amazonaws.AmazonServiceException)8 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)7 GetObjectMetadataRequest (com.amazonaws.services.s3.model.GetObjectMetadataRequest)6 S3Object (com.amazonaws.services.s3.model.S3Object)6 S3TestUtils.buildMockedS3FileSystem (org.apache.beam.sdk.io.aws.s3.S3TestUtils.buildMockedS3FileSystem)6 AmazonClientException (com.amazonaws.AmazonClientException)5 InterruptedIOException (java.io.InterruptedIOException)5 ArrayList (java.util.ArrayList)5 Path (org.apache.hadoop.fs.Path)5 ClientConfiguration (com.amazonaws.ClientConfiguration)4 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)4 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)4 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)4 URI (java.net.URI)4