use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project jackrabbit-oak by apache.
the class DataStoreUtils method deleteBucket.
public static void deleteBucket(String bucket, Map<String, ?> map, Date date) throws Exception {
log.info("cleaning bucket [" + bucket + "]");
Properties props = new Properties();
props.putAll(map);
AmazonS3Client s3service = Utils.openService(props);
TransferManager tmx = new TransferManager(s3service);
if (s3service.doesBucketExist(bucket)) {
for (int i = 0; i < 4; i++) {
tmx.abortMultipartUploads(bucket, date);
ObjectListing prevObjectListing = s3service.listObjects(bucket);
while (prevObjectListing != null) {
List<DeleteObjectsRequest.KeyVersion> deleteList = new ArrayList<DeleteObjectsRequest.KeyVersion>();
for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
deleteList.add(new DeleteObjectsRequest.KeyVersion(s3ObjSumm.getKey()));
}
if (deleteList.size() > 0) {
DeleteObjectsRequest delObjsReq = new DeleteObjectsRequest(bucket);
delObjsReq.setKeys(deleteList);
s3service.deleteObjects(delObjsReq);
}
if (!prevObjectListing.isTruncated())
break;
prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing);
}
}
s3service.deleteBucket(bucket);
log.info("bucket [ " + bucket + "] cleaned");
} else {
log.info("bucket [" + bucket + "] doesn't exists");
}
tmx.shutdownNow();
s3service.shutdown();
}
use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project herd by FINRAOS.
the class S3DaoImpl method deleteKeyVersions.
/**
* Deletes a list of keys/key versions from the specified S3 bucket.
*
* @param s3Client the S3 client
* @param s3BucketName the S3 bucket name
* @param keyVersions the list of S3 keys/key versions
*/
private void deleteKeyVersions(AmazonS3Client s3Client, String s3BucketName, List<DeleteObjectsRequest.KeyVersion> keyVersions) {
// Create a request to delete multiple objects in the specified bucket.
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(s3BucketName);
// The Multi-Object Delete request can contain a list of up to 1000 keys.
for (int i = 0; i < keyVersions.size() / MAX_KEYS_PER_DELETE_REQUEST + 1; i++) {
List<DeleteObjectsRequest.KeyVersion> keysSubList = keyVersions.subList(i * MAX_KEYS_PER_DELETE_REQUEST, Math.min(keyVersions.size(), (i + 1) * MAX_KEYS_PER_DELETE_REQUEST));
multiObjectDeleteRequest.setKeys(keysSubList);
try {
s3Operations.deleteObjects(multiObjectDeleteRequest, s3Client);
} catch (MultiObjectDeleteException multiObjectDeleteException) {
logMultiObjectDeleteException(multiObjectDeleteException);
throw multiObjectDeleteException;
}
LOGGER.info("Successfully requested the deletion of the listed below keys/key versions from the S3 bucket. s3KeyCount={} s3BucketName=\"{}\"", keysSubList.size(), s3BucketName);
for (DeleteObjectsRequest.KeyVersion keyVersion : keysSubList) {
LOGGER.info("s3Key=\"{}\" s3VersionId=\"{}\"", keyVersion.getKey(), keyVersion.getVersion());
}
}
}
use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project ddf by codice.
the class DeleteBean method delete.
public void delete(@Header(AWS2S3Constants.KEY) String s3Key) {
LOGGER.trace("Deleting: {} / {}", s3Bucket, s3Key);
ArrayList<ObjectIdentifier> toDelete = new ArrayList<>();
toDelete.add(ObjectIdentifier.builder().key(s3Key).build());
DeleteObjectsRequest deleteRequest = DeleteObjectsRequest.builder().bucket(s3Bucket).delete(Delete.builder().objects(toDelete).build()).build();
s3Client.deleteObjects(deleteRequest);
}
use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project aws-doc-sdk-examples by awsdocs.
the class DeleteMultipleObjectsVersionEnabledBucket method uploadAndDeleteObjectsWithVersions.
private static void uploadAndDeleteObjectsWithVersions() {
System.out.println("Uploading and deleting objects with versions specified.");
// Upload three sample objects.
ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>();
for (int i = 0; i < 3; i++) {
String keyName = "delete object without version ID example " + i;
PutObjectResult putResult = S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName, "Object number " + i + " to be deleted.");
// Gather the new object keys with version IDs.
keys.add(new KeyVersion(keyName, putResult.getVersionId()));
}
// Delete the specified versions of the sample objects.
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keys).withQuiet(false);
// Verify that the object versions were successfully deleted.
DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest);
int successfulDeletes = delObjRes.getDeletedObjects().size();
System.out.println(successfulDeletes + " objects successfully deleted");
}
use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project aws-doc-sdk-examples by awsdocs.
the class DeleteObjects method deleteBucketObjects.
// snippet-start:[s3.java2.delete_objects.main]
public static void deleteBucketObjects(S3Client s3, String bucketName, String objectName) {
ArrayList<ObjectIdentifier> toDelete = new ArrayList<ObjectIdentifier>();
toDelete.add(ObjectIdentifier.builder().key(objectName).build());
try {
DeleteObjectsRequest dor = DeleteObjectsRequest.builder().bucket(bucketName).delete(Delete.builder().objects(toDelete).build()).build();
s3.deleteObjects(dor);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
System.out.println("Done!");
}
Aggregations