Search in sources :

Example 1 with DeleteObjectRequest

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

the class COSAPIClient method delete.

@Override
public boolean delete(String hostName, Path path, boolean recursive) throws IOException {
    String key = pathToKey(path);
    LOG.debug("Object name to delete {}. Path {}", key, path.toString());
    try {
        mClient.deleteObject(new DeleteObjectRequest(mBucket, key));
        memoryCache.removeFileStatus(path.toString());
        return true;
    } catch (AmazonServiceException e) {
        if (e.getStatusCode() != 404) {
            throw new IOException(e);
        }
    }
    LOG.warn("Delete on {} not found. Nothing to delete");
    return false;
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 2 with DeleteObjectRequest

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

the class TestDeleteS3Object method testDeleteObjectSimple.

@Test
public void testDeleteObjectSimple() throws IOException {
    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);
    runner.run(1);
    runner.assertAllFlowFilesTransferred(DeleteS3Object.REL_SUCCESS, 1);
    ArgumentCaptor<DeleteObjectRequest> captureRequest = ArgumentCaptor.forClass(DeleteObjectRequest.class);
    Mockito.verify(mockS3Client, Mockito.times(1)).deleteObject(captureRequest.capture());
    DeleteObjectRequest request = captureRequest.getValue();
    assertEquals("test-bucket", request.getBucketName());
    assertEquals("delete-key", request.getKey());
    Mockito.verify(mockS3Client, Mockito.never()).deleteVersion(Mockito.any(DeleteVersionRequest.class));
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) HashMap(java.util.HashMap) DeleteVersionRequest(com.amazonaws.services.s3.model.DeleteVersionRequest) Test(org.junit.Test)

Example 3 with DeleteObjectRequest

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

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

the class DeleteS3Object method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final long startNanos = System.nanoTime();
    final String bucket = context.getProperty(BUCKET).evaluateAttributeExpressions(flowFile).getValue();
    final String key = context.getProperty(KEY).evaluateAttributeExpressions(flowFile).getValue();
    final String versionId = context.getProperty(VERSION_ID).evaluateAttributeExpressions(flowFile).getValue();
    final AmazonS3 s3 = getClient();
    // Deletes a key on Amazon S3
    try {
        if (versionId == null) {
            final DeleteObjectRequest r = new DeleteObjectRequest(bucket, key);
            // This call returns success if object doesn't exist
            s3.deleteObject(r);
        } else {
            final DeleteVersionRequest r = new DeleteVersionRequest(bucket, key, versionId);
            s3.deleteVersion(r);
        }
    } catch (final AmazonServiceException ase) {
        getLogger().error("Failed to delete S3 Object for {}; routing to failure", new Object[] { flowFile, ase });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    session.transfer(flowFile, REL_SUCCESS);
    final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
    getLogger().info("Successfully delete S3 Object for {} in {} millis; routing to success", new Object[] { flowFile, transferMillis });
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) FlowFile(org.apache.nifi.flowfile.FlowFile) AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonServiceException(com.amazonaws.AmazonServiceException) DeleteVersionRequest(com.amazonaws.services.s3.model.DeleteVersionRequest)

Example 5 with DeleteObjectRequest

use of com.amazonaws.services.s3.model.DeleteObjectRequest in project stocator by SparkTC.

the class COSAPIClient method delete.

@Override
public boolean delete(String hostName, Path path, boolean recursive) throws IOException {
    String key = pathToKey(path);
    LOG.debug("Object name to delete {}. Path {}", key, path.toString());
    try {
        mClient.deleteObject(new DeleteObjectRequest(mBucket, key));
        memoryCache.removeFileStatus(path.toString());
        return true;
    } catch (AmazonServiceException e) {
        if (e.getStatusCode() != 404) {
            throw new IOException(e);
        }
    }
    LOG.warn("Delete on {} not found. Nothing to delete");
    return false;
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Aggregations

DeleteObjectRequest (com.amazonaws.services.s3.model.DeleteObjectRequest)8 AmazonServiceException (com.amazonaws.AmazonServiceException)3 DeleteVersionRequest (com.amazonaws.services.s3.model.DeleteVersionRequest)3 IOException (java.io.IOException)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 InterruptedIOException (java.io.InterruptedIOException)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 SdkClientException (com.amazonaws.SdkClientException)1 Regions (com.amazonaws.regions.Regions)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 AWSS3RuntimeException (py.org.fundacionparaguaya.pspserver.common.exceptions.AWSS3RuntimeException)1