Search in sources :

Example 41 with AmazonS3Exception

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

the class AbstractS3IT method oneTimeSetup.

@BeforeClass
public static void oneTimeSetup() {
    // Creates a client and bucket for this test
    final FileInputStream fis;
    try {
        fis = new FileInputStream(CREDENTIALS_FILE);
    } catch (FileNotFoundException e1) {
        fail("Could not open credentials file " + CREDENTIALS_FILE + ": " + e1.getLocalizedMessage());
        return;
    }
    try {
        final PropertiesCredentials credentials = new PropertiesCredentials(fis);
        client = new AmazonS3Client(credentials);
        if (client.doesBucketExist(BUCKET_NAME)) {
            fail("Bucket " + BUCKET_NAME + " exists. Choose a different bucket name to continue test");
        }
        CreateBucketRequest request = REGION.contains("east") ? // See https://github.com/boto/boto3/issues/125
        new CreateBucketRequest(BUCKET_NAME) : new CreateBucketRequest(BUCKET_NAME, REGION);
        client.createBucket(request);
    } catch (final AmazonS3Exception e) {
        fail("Can't create the key " + BUCKET_NAME + ": " + e.getLocalizedMessage());
    } catch (final IOException e) {
        fail("Caught IOException preparing tests: " + e.getLocalizedMessage());
    } finally {
        FileUtils.closeQuietly(fis);
    }
    if (!client.doesBucketExist(BUCKET_NAME)) {
        fail("Setup incomplete, tests will fail");
    }
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) FileNotFoundException(java.io.FileNotFoundException) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BeforeClass(org.junit.BeforeClass)

Example 42 with AmazonS3Exception

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

the class AbstractS3IT method oneTimeTearDown.

@AfterClass
public static void oneTimeTearDown() {
    // Empty the bucket before deleting it.
    try {
        ObjectListing objectListing = client.listObjects(BUCKET_NAME);
        while (true) {
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                client.deleteObject(BUCKET_NAME, objectSummary.getKey());
            }
            if (objectListing.isTruncated()) {
                objectListing = client.listNextBatchOfObjects(objectListing);
            } else {
                break;
            }
        }
        DeleteBucketRequest dbr = new DeleteBucketRequest(BUCKET_NAME);
        client.deleteBucket(dbr);
    } catch (final AmazonS3Exception e) {
        System.err.println("Unable to delete bucket " + BUCKET_NAME + e.toString());
    }
    if (client.doesBucketExist(BUCKET_NAME)) {
        Assert.fail("Incomplete teardown, subsequent tests might fail");
    }
}
Also used : DeleteBucketRequest(com.amazonaws.services.s3.model.DeleteBucketRequest) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) AfterClass(org.junit.AfterClass)

Example 43 with AmazonS3Exception

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

the class TestDeleteS3Object method testDeleteObjectS3Exception.

@Test
public void testDeleteObjectS3Exception() {
    runner.setProperty(DeleteS3Object.REGION, "us-west-2");
    runner.setProperty(DeleteS3Object.BUCKET, "test-bucket");
    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "delete-key");
    runner.enqueue(new byte[0], attrs);
    Mockito.doThrow(new AmazonS3Exception("NoSuchBucket")).when(mockS3Client).deleteObject(Mockito.any());
    runner.run(1);
    runner.assertAllFlowFilesTransferred(DeleteS3Object.REL_FAILURE, 1);
    ArgumentCaptor<DeleteObjectRequest> captureRequest = ArgumentCaptor.forClass(DeleteObjectRequest.class);
    Mockito.verify(mockS3Client, Mockito.never()).deleteVersion(Mockito.any(DeleteVersionRequest.class));
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) HashMap(java.util.HashMap) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) DeleteVersionRequest(com.amazonaws.services.s3.model.DeleteVersionRequest) Test(org.junit.Test)

Example 44 with AmazonS3Exception

use of com.amazonaws.services.s3.model.AmazonS3Exception in project stocator by CODAIT.

the class COSAPIClient method getFileStatusKeyBased.

private FileStatus getFileStatusKeyBased(String key, Path path) throws AmazonS3Exception {
    LOG.trace("internal method - get file status by key {}, path {}", key, path);
    FileStatus cachedFS = memoryCache.getFileStatus(path.toString());
    if (cachedFS != null) {
        return cachedFS;
    }
    ObjectMetadata meta = mClient.getObjectMetadata(mBucket, key);
    String sparkOrigin = meta.getUserMetaDataOf("data-origin");
    boolean stocatorCreated = false;
    if (sparkOrigin != null) {
        String tmp = (String) sparkOrigin;
        if (tmp.equals("stocator")) {
            stocatorCreated = true;
        }
    }
    mCachedSparkOriginated.put(key, Boolean.valueOf(stocatorCreated));
    FileStatus fs = createFileStatus(meta.getContentLength(), key, meta.getLastModified(), path);
    LOG.trace("getFileStatusKeyBased: key {} fs.path {}", key, fs.getPath());
    memoryCache.putFileStatus(path.toString(), fs);
    return fs;
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 45 with AmazonS3Exception

use of com.amazonaws.services.s3.model.AmazonS3Exception in project stocator by CODAIT.

the class COSUtils method translateException.

/**
 * Translate an exception raised in an operation into an IOException. The
 * specific type of IOException depends on the class of
 * {@link AmazonClientException} passed in, and any status codes included in
 * the operation. That is: HTTP error codes are examined and can be used to
 * build a more specific response.
 *
 * @param operation operation
 * @param path path operated on (may be null)
 * @param exception amazon exception raised
 * @return an IOE which wraps the caught exception
 */
@SuppressWarnings("ThrowableInstanceNeverThrown")
public static IOException translateException(String operation, String path, AmazonClientException exception) {
    String message = String.format("%s%s: %s", operation, path != null ? (" on " + path) : "", exception);
    if (!(exception instanceof AmazonServiceException)) {
        if (containsInterruptedException(exception)) {
            return (IOException) new InterruptedIOException(message).initCause(exception);
        }
        return new COSClientIOException(message, exception);
    } else {
        IOException ioe;
        AmazonServiceException ase = (AmazonServiceException) exception;
        // this exception is non-null if the service exception is an COS one
        AmazonS3Exception s3Exception = ase instanceof AmazonS3Exception ? (AmazonS3Exception) ase : null;
        int status = ase.getStatusCode();
        switch(status) {
            case 301:
                if (s3Exception != null) {
                    if (s3Exception.getAdditionalDetails() != null && s3Exception.getAdditionalDetails().containsKey(ENDPOINT_KEY)) {
                        message = String.format("Received permanent redirect response to " + "endpoint %s.  This likely indicates that the COS endpoint " + "configured in %s does not match the region containing " + "the bucket.", s3Exception.getAdditionalDetails().get(ENDPOINT_KEY), ENDPOINT_URL);
                    }
                    ioe = new COSIOException(message, s3Exception);
                } else {
                    ioe = new COSServiceIOException(message, ase);
                }
                break;
            // permissions
            case 401:
            case 403:
                ioe = new AccessDeniedException(path, null, message);
                ioe.initCause(ase);
                break;
            // the object isn't there
            case 404:
            case 410:
                ioe = new FileNotFoundException(message);
                ioe.initCause(ase);
                break;
            // a shorter one while it is being read.
            case 416:
                ioe = new EOFException(message);
                break;
            default:
                // no specific exit code. Choose an IOE subclass based on the class
                // of the caught exception
                ioe = s3Exception != null ? new COSIOException(message, s3Exception) : new COSServiceIOException(message, ase);
                break;
        }
        return ioe;
    }
}
Also used : COSClientIOException(com.ibm.stocator.fs.cos.exception.COSClientIOException) InterruptedIOException(java.io.InterruptedIOException) AccessDeniedException(java.nio.file.AccessDeniedException) COSIOException(com.ibm.stocator.fs.cos.exception.COSIOException) COSServiceIOException(com.ibm.stocator.fs.cos.exception.COSServiceIOException) AmazonServiceException(com.amazonaws.AmazonServiceException) FileNotFoundException(java.io.FileNotFoundException) EOFException(java.io.EOFException) COSServiceIOException(com.ibm.stocator.fs.cos.exception.COSServiceIOException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) COSIOException(com.ibm.stocator.fs.cos.exception.COSIOException) COSClientIOException(com.ibm.stocator.fs.cos.exception.COSClientIOException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

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