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;
}
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);
}
}
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;
}
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() + "'");
}
}
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);
}
}
Aggregations