Search in sources :

Example 6 with DeleteObjectRequest

use of software.amazon.awssdk.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 7 with DeleteObjectRequest

use of software.amazon.awssdk.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 8 with DeleteObjectRequest

use of software.amazon.awssdk.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 9 with DeleteObjectRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectRequest in project FP-PSP-SERVER by FundacionParaguaya.

the class ImageUploadServiceImpl method deleteImage.

@Override
public void deleteImage(String logoUrl, String imageDirectory) {
    if (logoUrl == null) {
        return;
    }
    try {
        String strRegion = applicationProperties.getAws().getStrRegion();
        Regions region = Regions.valueOf(strRegion);
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(region).build();
        String bucketName = applicationProperties.getAws().getBucketName();
        String fileName = logoUrl.substring(logoUrl.lastIndexOf('/') + 1);
        String keyName = imageDirectory + fileName;
        s3Client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
    } catch (SdkClientException sdkClientExc) {
        LOG.error(sdkClientExc.getMessage(), sdkClientExc);
        throw new AWSS3RuntimeException(sdkClientExc);
    }
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) AWSS3RuntimeException(py.org.fundacionparaguaya.pspserver.common.exceptions.AWSS3RuntimeException) Regions(com.amazonaws.regions.Regions)

Example 10 with DeleteObjectRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectRequest in project dataverse by IQSS.

the class S3AccessIO method deleteAuxObject.

@Override
public void deleteAuxObject(String auxItemTag) throws IOException {
    if (!this.canWrite()) {
        open(DataAccessOption.WRITE_ACCESS);
    }
    String destinationKey = getDestinationKey(auxItemTag);
    try {
        DeleteObjectRequest dor = new DeleteObjectRequest(bucketName, destinationKey);
        s3.deleteObject(dor);
    } catch (AmazonClientException ase) {
        logger.warning("S3AccessIO: Unable to delete object    " + ase.getMessage());
    }
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonClientException(com.amazonaws.AmazonClientException)

Aggregations

DeleteObjectRequest (com.amazonaws.services.s3.model.DeleteObjectRequest)10 AmazonServiceException (com.amazonaws.AmazonServiceException)5 IOException (java.io.IOException)4 AmazonS3 (com.amazonaws.services.s3.AmazonS3)3 DeleteVersionRequest (com.amazonaws.services.s3.model.DeleteVersionRequest)3 AmazonClientException (com.amazonaws.AmazonClientException)2 SdkClientException (com.amazonaws.SdkClientException)2 Regions (com.amazonaws.regions.Regions)2 InterruptedIOException (java.io.InterruptedIOException)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 DeleteObjectRequest (software.amazon.awssdk.services.s3.model.DeleteObjectRequest)2 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)2 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 ChunkAlreadyExistsException (io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)1 ChunkNotFoundException (io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException)1 ChunkStorageException (io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)1 ByteBuffer (java.nio.ByteBuffer)1 Random (java.util.Random)1