Search in sources :

Example 31 with DeleteObjectsRequest

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();
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) ArrayList(java.util.ArrayList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) Properties(java.util.Properties) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 32 with DeleteObjectsRequest

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());
        }
    }
}
Also used : MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 33 with DeleteObjectsRequest

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);
}
Also used : ArrayList(java.util.ArrayList) ObjectIdentifier(software.amazon.awssdk.services.s3.model.ObjectIdentifier) DeleteObjectsRequest(software.amazon.awssdk.services.s3.model.DeleteObjectsRequest)

Example 34 with DeleteObjectsRequest

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");
}
Also used : PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) KeyVersion(com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.amazonaws.services.s3.model.DeleteObjectsResult) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 35 with DeleteObjectsRequest

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!");
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) ObjectIdentifier(software.amazon.awssdk.services.s3.model.ObjectIdentifier) DeleteObjectsRequest(software.amazon.awssdk.services.s3.model.DeleteObjectsRequest)

Aggregations

DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)39 ArrayList (java.util.ArrayList)26 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)24 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)15 DeleteObjectsResult (com.amazonaws.services.s3.model.DeleteObjectsResult)14 IOException (java.io.IOException)10 Test (org.junit.Test)10 KeyVersion (com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion)9 AmazonClientException (com.amazonaws.AmazonClientException)8 AmazonS3 (com.amazonaws.services.s3.AmazonS3)5 MultiObjectDeleteException (com.amazonaws.services.s3.model.MultiObjectDeleteException)5 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)4 DeleteObjectsRequest (software.amazon.awssdk.services.s3.model.DeleteObjectsRequest)4 ObjectIdentifier (software.amazon.awssdk.services.s3.model.ObjectIdentifier)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)3 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)3 ExecutorService (java.util.concurrent.ExecutorService)3 NamedThreadFactory (org.apache.jackrabbit.core.data.util.NamedThreadFactory)3 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)3