Search in sources :

Example 1 with DeleteVersionRequest

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

the class TestDeleteS3Object method testDeleteVersionFromExpressions.

@Test
public void testDeleteVersionFromExpressions() {
    runner.setProperty(DeleteS3Object.REGION, "us-west-2");
    runner.setProperty(DeleteS3Object.BUCKET, "${s3.bucket}");
    runner.setProperty(DeleteS3Object.VERSION_ID, "${s3.version}");
    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "test-key");
    attrs.put("s3.bucket", "test-bucket");
    attrs.put("s3.version", "test-version");
    runner.enqueue(new byte[0], attrs);
    runner.run(1);
    runner.assertAllFlowFilesTransferred(DeleteS3Object.REL_SUCCESS, 1);
    ArgumentCaptor<DeleteVersionRequest> captureRequest = ArgumentCaptor.forClass(DeleteVersionRequest.class);
    Mockito.verify(mockS3Client, Mockito.times(1)).deleteVersion(captureRequest.capture());
    DeleteVersionRequest request = captureRequest.getValue();
    assertEquals("test-bucket", request.getBucketName());
    assertEquals("test-key", request.getKey());
    assertEquals("test-version", request.getVersionId());
    Mockito.verify(mockS3Client, Mockito.never()).deleteObject(Mockito.any(DeleteObjectRequest.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 2 with DeleteVersionRequest

use of com.amazonaws.services.s3.model.DeleteVersionRequest in project opencast by opencast.

the class AwsS3DistributionServiceImpl method restoreElement.

protected MediaPackageElement restoreElement(String channelId, MediaPackage mediaPackage, String elementId, String fileName) throws DistributionException {
    String objectName = null;
    if (StringUtils.isNotBlank(fileName)) {
        objectName = buildObjectName(channelId, mediaPackage.getIdentifier().toString(), elementId, fileName);
    } else {
        objectName = buildObjectName(channelId, mediaPackage.getIdentifier().toString(), mediaPackage.getElementById(elementId));
    }
    // Get the latest version of the file
    // Note that this should be the delete marker for the file. We'll check, but if there is more than one delete marker
    // we'll have probs
    ListVersionsRequest lv = new ListVersionsRequest().withBucketName(bucketName).withPrefix(objectName).withMaxResults(1);
    VersionListing listing = s3.listVersions(lv);
    if (listing.getVersionSummaries().size() < 1) {
        throw new DistributionException("Object not found: " + objectName);
    }
    String versionId = listing.getVersionSummaries().get(0).getVersionId();
    // Verify that this is in fact a delete marker
    GetObjectMetadataRequest metadata = new GetObjectMetadataRequest(bucketName, objectName, versionId);
    // Ok, so there's no way of asking AWS directly if the object is deleted in this version of the SDK
    // So instead, we ask for its metadata
    // If it's deleted, then there *isn't* any metadata and we get a 404, which throws the exception
    // This, imo, is an incredibly boneheaded omission from the AWS SDK, and implies we should look for something which
    // sucks less
    // FIXME: This section should be refactored with a simple s3.doesObjectExist(bucketName, objectName) once we update
    // the AWS SDK
    boolean isDeleted = false;
    try {
        s3.getObjectMetadata(metadata);
    } catch (AmazonServiceException e) {
        // Note: This exception is actually a 405, not a 404.
        // This is expected, but very confusing if you're thinking it should be a 'file not found', rather than a 'method
        // not allowed on stuff that's deleted'
        // It's unclear what the expected behaviour is for things which have never existed...
        isDeleted = true;
    }
    if (isDeleted) {
        // Delete the delete marker
        DeleteVersionRequest delete = new DeleteVersionRequest(bucketName, objectName, versionId);
        s3.deleteVersion(delete);
    }
    return mediaPackage.getElementById(elementId);
}
Also used : GetObjectMetadataRequest(com.amazonaws.services.s3.model.GetObjectMetadataRequest) VersionListing(com.amazonaws.services.s3.model.VersionListing) AmazonServiceException(com.amazonaws.AmazonServiceException) DistributionException(org.opencastproject.distribution.api.DistributionException) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest) DeleteVersionRequest(com.amazonaws.services.s3.model.DeleteVersionRequest)

Example 3 with DeleteVersionRequest

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

the class DeleteObjectVersionEnabledBucket method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    String keyName = "*** Key name ****";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        // Check to ensure that the bucket is versioning-enabled.
        String bucketVersionStatus = s3Client.getBucketVersioningConfiguration(bucketName).getStatus();
        if (!bucketVersionStatus.equals(BucketVersioningConfiguration.ENABLED)) {
            System.out.printf("Bucket %s is not versioning-enabled.", bucketName);
        } else {
            // Add an object.
            PutObjectResult putResult = s3Client.putObject(bucketName, keyName, "Sample content for deletion example.");
            System.out.printf("Object %s added to bucket %s\n", keyName, bucketName);
            // Delete the version of the object that we just created.
            System.out.println("Deleting versioned object " + keyName);
            s3Client.deleteVersion(new DeleteVersionRequest(bucketName, keyName, putResult.getVersionId()));
            System.out.printf("Object %s, version %s deleted\n", keyName, putResult.getVersionId());
        }
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it 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) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Regions(com.amazonaws.regions.Regions) DeleteVersionRequest(com.amazonaws.services.s3.model.DeleteVersionRequest)

Example 4 with DeleteVersionRequest

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

the class TestDeleteS3Object method testDeleteVersionSimple.

@Test
public void testDeleteVersionSimple() {
    runner.setProperty(DeleteS3Object.REGION, "us-west-2");
    runner.setProperty(DeleteS3Object.BUCKET, "test-bucket");
    runner.setProperty(DeleteS3Object.VERSION_ID, "test-version");
    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "test-key");
    runner.enqueue(new byte[0], attrs);
    runner.run(1);
    runner.assertAllFlowFilesTransferred(DeleteS3Object.REL_SUCCESS, 1);
    ArgumentCaptor<DeleteVersionRequest> captureRequest = ArgumentCaptor.forClass(DeleteVersionRequest.class);
    Mockito.verify(mockS3Client, Mockito.times(1)).deleteVersion(captureRequest.capture());
    DeleteVersionRequest request = captureRequest.getValue();
    assertEquals("test-bucket", request.getBucketName());
    assertEquals("test-key", request.getKey());
    assertEquals("test-version", request.getVersionId());
    Mockito.verify(mockS3Client, Mockito.never()).deleteObject(Mockito.any(DeleteObjectRequest.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 5 with DeleteVersionRequest

use of com.amazonaws.services.s3.model.DeleteVersionRequest 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)

Aggregations

DeleteVersionRequest (com.amazonaws.services.s3.model.DeleteVersionRequest)5 AmazonServiceException (com.amazonaws.AmazonServiceException)3 DeleteObjectRequest (com.amazonaws.services.s3.model.DeleteObjectRequest)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 SdkClientException (com.amazonaws.SdkClientException)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 Regions (com.amazonaws.regions.Regions)1 GetObjectMetadataRequest (com.amazonaws.services.s3.model.GetObjectMetadataRequest)1 ListVersionsRequest (com.amazonaws.services.s3.model.ListVersionsRequest)1 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)1 VersionListing (com.amazonaws.services.s3.model.VersionListing)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 DistributionException (org.opencastproject.distribution.api.DistributionException)1