use of com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion in project herd by FINRAOS.
the class MockS3OperationsImpl method deleteObjects.
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client) {
LOGGER.debug("deleteObjects(): deleteObjectRequest.getBucketName() = " + deleteObjectsRequest.getBucketName() + ", deleteObjectRequest.getKeys() = " + deleteObjectsRequest.getKeys());
List<DeletedObject> deletedObjects = new ArrayList<>();
MockS3Bucket mockS3Bucket = mockS3Buckets.get(deleteObjectsRequest.getBucketName());
for (KeyVersion keyVersion : deleteObjectsRequest.getKeys()) {
String s3ObjectKey = keyVersion.getKey();
String s3ObjectVersion = keyVersion.getVersion();
String s3ObjectKeyVersion = s3ObjectKey + (s3ObjectVersion != null ? s3ObjectVersion : "");
mockS3Bucket.getObjects().remove(s3ObjectKey);
if (mockS3Bucket.getVersions().remove(s3ObjectKeyVersion) != null) {
DeletedObject deletedObject = new DeletedObject();
deletedObject.setKey(s3ObjectKey);
deletedObject.setVersionId(s3ObjectVersion);
deletedObjects.add(deletedObject);
}
}
return new DeleteObjectsResult(deletedObjects);
}
use of com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion in project aws-doc-sdk-examples by awsdocs.
the class DeleteMultipleObjectsNonVersionedBucket method main.
public static void main(String[] args) throws IOException {
Regions clientRegion = Regions.DEFAULT_REGION;
String bucketName = "*** Bucket name ***";
try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(clientRegion).build();
// Upload three sample objects.
ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>();
for (int i = 0; i < 3; i++) {
String keyName = "delete object example " + i;
s3Client.putObject(bucketName, keyName, "Object number " + i + " to be deleted.");
keys.add(new KeyVersion(keyName));
}
System.out.println(keys.size() + " objects successfully created.");
// Delete the sample objects.
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName).withKeys(keys).withQuiet(false);
// Verify that the objects were deleted successfully.
DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);
int successfulDeletes = delObjRes.getDeletedObjects().size();
System.out.println(successfulDeletes + " objects successfully deleted.");
} 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();
}
}
use of com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion in project aws-doc-sdk-examples by awsdocs.
the class DeleteMultipleObjectsVersionEnabledBucket method multiObjectVersionedDeleteRemoveDeleteMarkers.
private static void multiObjectVersionedDeleteRemoveDeleteMarkers(DeleteObjectsResult response) {
List<KeyVersion> keyList = new ArrayList<KeyVersion>();
for (DeletedObject deletedObject : response.getDeletedObjects()) {
// Note that the specified version ID is the version ID for the delete marker.
keyList.add(new KeyVersion(deletedObject.getKey(), deletedObject.getDeleteMarkerVersionId()));
}
// Create a request to delete the delete markers.
DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keyList);
// Delete the delete markers, leaving the objects intact in the bucket.
DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(deleteRequest);
int successfulDeletes = delObjRes.getDeletedObjects().size();
System.out.println(successfulDeletes + " delete markers successfully deleted");
}
use of com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion in project aws-doc-sdk-examples by awsdocs.
the class DeleteMultipleObjectsVersionEnabledBucket method uploadAndDeleteObjectsWithoutVersions.
private static DeleteObjectsResult uploadAndDeleteObjectsWithoutVersions() {
System.out.println("Uploading and deleting objects with no versions specified.");
// Upload three sample objects.
ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>();
for (int i = 0; i < 3; i++) {
String keyName = "delete object with version ID example " + i;
S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName, "Object number " + i + " to be deleted.");
// Gather the new object keys without version IDs.
keys.add(new KeyVersion(keyName));
}
// Delete the sample objects without specifying versions.
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keys).withQuiet(false);
// Verify that delete markers were successfully added to the objects.
DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest);
int successfulDeletes = delObjRes.getDeletedObjects().size();
System.out.println(successfulDeletes + " objects successfully marked for deletion without versions.");
return delObjRes;
}
use of com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion in project dataverse by IQSS.
the class S3AccessIO method deleteAllAuxObjects.
@Override
public void deleteAllAuxObjects() throws IOException {
if (!this.canWrite()) {
open(DataAccessOption.WRITE_ACCESS);
}
String prefix = getDestinationKey("");
List<S3ObjectSummary> storedAuxFilesSummary = null;
try {
ListObjectsRequest req = new ListObjectsRequest().withBucketName(bucketName).withPrefix(prefix);
ObjectListing storedAuxFilesList = s3.listObjects(req);
storedAuxFilesSummary = storedAuxFilesList.getObjectSummaries();
while (storedAuxFilesList.isTruncated()) {
storedAuxFilesList = s3.listNextBatchOfObjects(storedAuxFilesList);
storedAuxFilesSummary.addAll(storedAuxFilesList.getObjectSummaries());
}
} catch (AmazonClientException ase) {
logger.warning("Caught an AmazonServiceException: " + ase.getMessage());
throw new IOException("S3AccessIO: Failed to get aux objects for listing to delete.");
}
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName);
List<KeyVersion> keys = new ArrayList<>();
for (S3ObjectSummary item : storedAuxFilesSummary) {
String destinationKey = item.getKey();
keys.add(new KeyVersion(destinationKey));
}
// Check if the list of auxiliary files for a data file is empty
if (keys.isEmpty()) {
logger.fine("S3AccessIO: No auxiliary objects to delete.");
return;
}
multiObjectDeleteRequest.setKeys(keys);
logger.fine("Trying to delete auxiliary files...");
try {
s3.deleteObjects(multiObjectDeleteRequest);
} catch (MultiObjectDeleteException e) {
logger.warning("S3AccessIO: Unable to delete auxilary objects" + e.getMessage());
throw new IOException("S3AccessIO: Failed to delete one or more auxiliary objects.");
}
}
Aggregations