Search in sources :

Example 1 with DeleteObjectRequest

use of com.amazonaws.s3.model.DeleteObjectRequest in project beneficiary-fhir-data by CMSgov.

the class DataSetMoveTask method call.

/**
 * @see java.util.concurrent.Callable#call()
 */
@Override
public Void call() throws Exception {
    LOGGER.debug("Renaming data set '{}' in S3, now that processing is complete...", manifest);
    /*
     * S3 doesn't support batch/transactional operations, or an atomic move
     * operation. Instead, we have to first copy all of the objects to their
     * new location, and then remove the old objects. If something blows up
     * in the middle of this method, orphaned S3 objects WILL be created.
     * That's an unlikely enough occurrence, though, that we're not going to
     * engineer around it right now.
     */
    // First, get a list of all the object keys to work on.
    List<String> s3KeySuffixesToMove = manifest.getEntries().stream().map(e -> String.format("%s/%s", manifest.getTimestampText(), e.getName())).collect(Collectors.toList());
    s3KeySuffixesToMove.add(String.format("%s/%d_manifest.xml", manifest.getTimestampText(), manifest.getSequenceId()));
    /*
     * Then, loop through each of those objects and copy them (S3 has no
     * bulk copy operation).
     */
    for (String s3KeySuffixToMove : s3KeySuffixesToMove) {
        String sourceKey = String.format("%s/%s", CcwRifLoadJob.S3_PREFIX_PENDING_DATA_SETS, s3KeySuffixToMove);
        String targetKey = String.format("%s/%s", CcwRifLoadJob.S3_PREFIX_COMPLETED_DATA_SETS, s3KeySuffixToMove);
        /*
       * Before copying, grab the metadata of the source object to ensure
       * that we maintain its encryption settings (by default, the copy
       * will maintain all metadata EXCEPT: server-side-encryption,
       * storage-class, and website-redirect-location).
       */
        ObjectMetadata objectMetadata = s3TaskManager.getS3Client().getObjectMetadata(options.getS3BucketName(), sourceKey);
        CopyObjectRequest copyRequest = new CopyObjectRequest(options.getS3BucketName(), sourceKey, options.getS3BucketName(), targetKey);
        if (objectMetadata.getSSEAwsKmsKeyId() != null) {
            copyRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(objectMetadata.getSSEAwsKmsKeyId()));
        }
        Copy copyOperation = s3TaskManager.getS3TransferManager().copy(copyRequest);
        try {
            copyOperation.waitForCopyResult();
            s3TaskManager.getS3Client().waiters().objectExists().run(new WaiterParameters<GetObjectMetadataRequest>(new GetObjectMetadataRequest(options.getS3BucketName(), targetKey)));
        } catch (InterruptedException e) {
            throw new BadCodeMonkeyException(e);
        }
    }
    LOGGER.debug("Data set copied in S3 (step 1 of move).");
    /*
     * After everything's been copied, loop over it all again and delete it the source objects. (We
     * could do it all in the same loop, but this is a bit easier to clean up from if it goes
     * sideways.)
     */
    for (String s3KeySuffixToMove : s3KeySuffixesToMove) {
        String sourceKey = String.format("%s/%s", CcwRifLoadJob.S3_PREFIX_PENDING_DATA_SETS, s3KeySuffixToMove);
        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(options.getS3BucketName(), sourceKey);
        s3TaskManager.getS3Client().deleteObject(deleteObjectRequest);
        s3TaskManager.getS3Client().waiters().objectNotExists().run(new WaiterParameters<GetObjectMetadataRequest>(new GetObjectMetadataRequest(options.getS3BucketName(), sourceKey)));
    }
    LOGGER.debug("Data set deleted in S3 (step 2 of move).");
    LOGGER.debug("Renamed data set '{}' in S3, now that processing is complete.", manifest);
    return null;
}
Also used : Logger(org.slf4j.Logger) BadCodeMonkeyException(gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException) WaiterParameters(com.amazonaws.waiters.WaiterParameters) SSEAwsKeyManagementParams(com.amazonaws.services.s3.model.SSEAwsKeyManagementParams) LoggerFactory(org.slf4j.LoggerFactory) Callable(java.util.concurrent.Callable) ExtractionOptions(gov.cms.bfd.pipeline.ccw.rif.extract.ExtractionOptions) CcwRifLoadJob(gov.cms.bfd.pipeline.ccw.rif.CcwRifLoadJob) Collectors(java.util.stream.Collectors) Copy(com.amazonaws.services.s3.transfer.Copy) DataSetManifest(gov.cms.bfd.pipeline.ccw.rif.extract.s3.DataSetManifest) DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) List(java.util.List) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) GetObjectMetadataRequest(com.amazonaws.services.s3.model.GetObjectMetadataRequest) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) GetObjectMetadataRequest(com.amazonaws.services.s3.model.GetObjectMetadataRequest) Copy(com.amazonaws.services.s3.transfer.Copy) BadCodeMonkeyException(gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) SSEAwsKeyManagementParams(com.amazonaws.services.s3.model.SSEAwsKeyManagementParams)

Example 2 with DeleteObjectRequest

use of com.amazonaws.s3.model.DeleteObjectRequest in project zuliasearch by zuliaio.

the class S3DocumentStorage method deleteAssociatedDocuments.

@Override
public void deleteAssociatedDocuments(String uniqueId) throws Exception {
    FindIterable<Document> found = client.getDatabase(dbName).getCollection(COLLECTION).find(Filters.eq("metadata." + DOCUMENT_UNIQUE_ID_KEY, uniqueId));
    for (Document doc : found) {
        client.getDatabase(dbName).getCollection(COLLECTION).deleteOne(Filters.eq("_id", doc.getObjectId("_id")));
        Document s3Info = doc.get("s3", Document.class);
        DeleteObjectRequest dor = DeleteObjectRequest.builder().bucket(s3Info.getString("bucket")).key(s3Info.getString("key")).build();
        s3.deleteObject(dor);
    }
}
Also used : DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) Document(org.bson.Document) AssociatedDocument(io.zulia.message.ZuliaBase.AssociatedDocument)

Example 3 with DeleteObjectRequest

use of com.amazonaws.s3.model.DeleteObjectRequest in project orientdb-enterprise-agent by SAP.

the class S3UploaderTest method deleteFileOnS3.

private boolean deleteFileOnS3(String bucketName, String keyName) {
    boolean success = false;
    try {
        AWSCredentials awsCredentials = new BasicAWSCredentials(this.ACCESS_KEY, this.SECRET_ACCESS_KEY);
        AmazonS3Client s3client = new AmazonS3Client(awsCredentials);
        s3client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
        success = true;
    } catch (AmazonServiceException ase) {
        OLogManager.instance().info(this, "Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason.");
        OLogManager.instance().info(this, "Error Message:    %s", ase.getMessage());
        OLogManager.instance().info(this, "HTTP Status Code: %s", ase.getStatusCode());
        OLogManager.instance().info(this, "AWS Error Code:   %s", ase.getErrorCode());
        OLogManager.instance().info(this, "Error Type:       %s", ase.getErrorType());
        OLogManager.instance().info(this, "Request ID:       %s", ase.getRequestId());
    } catch (AmazonClientException ace) {
        OLogManager.instance().info(this, "Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network.");
        OLogManager.instance().info(this, "Error Message: %s", ace.getMessage());
    } catch (Exception e) {
        OLogManager.instance().info(this, "Caught an exception client side.");
        OLogManager.instance().info(this, "Error Message: %s", e.getMessage());
    }
    return success;
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException)

Example 4 with DeleteObjectRequest

use of com.amazonaws.s3.model.DeleteObjectRequest in project pipes by pipecraft.

the class S3Bucket method delete.

@Override
public void delete(S3ObjectSummary obj) throws IOException {
    try {
        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(getBucketName(), obj.getKey());
        s3.deleteObject(deleteObjectRequest);
    } catch (AmazonClientException e) {
        throw mapToIOException(extractAmazonServiceException(e), "Failed deleting '" + getBucketName() + "/" + obj.getKey() + "'");
    }
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonClientException(com.amazonaws.AmazonClientException)

Example 5 with DeleteObjectRequest

use of com.amazonaws.s3.model.DeleteObjectRequest in project kaldb by slackhq.

the class S3BlobFs method delete.

@Override
public boolean delete(URI segmentUri, boolean forceDelete) throws IOException {
    LOG.info("Deleting uri {} force {}", segmentUri, forceDelete);
    try {
        if (isDirectory(segmentUri)) {
            if (!forceDelete) {
                Preconditions.checkState(isEmptyDirectory(segmentUri), "ForceDelete flag is not set and directory '%s' is not empty", segmentUri);
            }
            String prefix = normalizeToDirectoryPrefix(segmentUri);
            ListObjectsV2Response listObjectsV2Response;
            ListObjectsV2Request.Builder listObjectsV2RequestBuilder = ListObjectsV2Request.builder().bucket(segmentUri.getHost());
            if (prefix.equals(DELIMITER)) {
                ListObjectsV2Request listObjectsV2Request = listObjectsV2RequestBuilder.build();
                listObjectsV2Response = s3Client.listObjectsV2(listObjectsV2Request);
            } else {
                ListObjectsV2Request listObjectsV2Request = listObjectsV2RequestBuilder.prefix(prefix).build();
                listObjectsV2Response = s3Client.listObjectsV2(listObjectsV2Request);
            }
            boolean deleteSucceeded = true;
            for (S3Object s3Object : listObjectsV2Response.contents()) {
                DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(segmentUri.getHost()).key(s3Object.key()).build();
                DeleteObjectResponse deleteObjectResponse = s3Client.deleteObject(deleteObjectRequest);
                deleteSucceeded &= deleteObjectResponse.sdkHttpResponse().isSuccessful();
            }
            return deleteSucceeded;
        } else {
            String prefix = DELIMITER + sanitizePath(segmentUri.getPath());
            DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(segmentUri.getHost()).key(prefix).build();
            DeleteObjectResponse deleteObjectResponse = s3Client.deleteObject(deleteObjectRequest);
            return deleteObjectResponse.sdkHttpResponse().isSuccessful();
        }
    } catch (NoSuchKeyException e) {
        return false;
    } catch (S3Exception e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) NoSuchKeyException(software.amazon.awssdk.services.s3.model.NoSuchKeyException) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) DeleteObjectResponse(software.amazon.awssdk.services.s3.model.DeleteObjectResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) IOException(java.io.IOException) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response) NoSuchKeyException(software.amazon.awssdk.services.s3.model.NoSuchKeyException) URISyntaxException(java.net.URISyntaxException) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

DeleteObjectRequest (com.amazonaws.services.s3.model.DeleteObjectRequest)25 DeleteObjectRequest (software.amazon.awssdk.services.s3.model.DeleteObjectRequest)19 IOException (java.io.IOException)11 AmazonClientException (com.amazonaws.AmazonClientException)7 AmazonServiceException (com.amazonaws.AmazonServiceException)7 S3Client (software.amazon.awssdk.services.s3.S3Client)6 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)6 HashMap (java.util.HashMap)4 SdkClientException (com.amazonaws.SdkClientException)3 SQSEvent (com.amazonaws.services.lambda.runtime.events.SQSEvent)3 SQSMessage (com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage)3 DeleteVersionRequest (com.amazonaws.services.s3.model.DeleteVersionRequest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Collections.singletonList (java.util.Collections.singletonList)3 Consumer (java.util.function.Consumer)3 Stream (java.util.stream.Stream)3 Assertions (org.assertj.core.api.Assertions)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3