Search in sources :

Example 21 with DeleteObjectsRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest 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;
}
Also used : 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 22 with DeleteObjectsRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project aws-doc-sdk-examples by awsdocs.

the class DeleteObjects method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and at least\n" + "one object name (key) to delete.\n" + "\n" + "Ex: DeleteObjects <bucketname> <objectname1> [objectname2, ...]\n";
    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucket_name = args[0];
    String[] object_keys = Arrays.copyOfRange(args, 1, args.length);
    System.out.println("Deleting objects from S3 bucket: " + bucket_name);
    for (String k : object_keys) {
        System.out.println(" * " + k);
    }
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    try {
        DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket_name).withKeys(object_keys);
        s3.deleteObjects(dor);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonServiceException(com.amazonaws.AmazonServiceException) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 23 with DeleteObjectsRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project crate by crate.

the class S3BlobContainer method doDeleteBlobs.

private void doDeleteBlobs(List<String> blobNames, boolean relative) throws IOException {
    if (blobNames.isEmpty()) {
        return;
    }
    final Set<String> outstanding;
    if (relative) {
        outstanding = blobNames.stream().map(this::buildKey).collect(Collectors.toSet());
    } else {
        outstanding = new HashSet<>(blobNames);
    }
    try (AmazonS3Reference clientReference = blobStore.clientReference()) {
        // S3 API only allows 1k blobs per delete so we split up the given blobs into requests of max. 1k deletes
        final List<DeleteObjectsRequest> deleteRequests = new ArrayList<>();
        final List<String> partition = new ArrayList<>();
        for (String key : outstanding) {
            partition.add(key);
            if (partition.size() == MAX_BULK_DELETES) {
                deleteRequests.add(bulkDelete(blobStore.bucket(), partition));
                partition.clear();
            }
        }
        if (partition.isEmpty() == false) {
            deleteRequests.add(bulkDelete(blobStore.bucket(), partition));
        }
        AmazonClientException aex = null;
        for (DeleteObjectsRequest deleteRequest : deleteRequests) {
            List<String> keysInRequest = deleteRequest.getKeys().stream().map(DeleteObjectsRequest.KeyVersion::getKey).collect(Collectors.toList());
            try {
                clientReference.client().deleteObjects(deleteRequest);
                outstanding.removeAll(keysInRequest);
            } catch (MultiObjectDeleteException e) {
                // We are sending quiet mode requests so we can't use the deleted keys entry on the exception and instead
                // first remove all keys that were sent in the request and then add back those that ran into an exception.
                outstanding.removeAll(keysInRequest);
                outstanding.addAll(e.getErrors().stream().map(MultiObjectDeleteException.DeleteError::getKey).collect(Collectors.toSet()));
                aex = ExceptionsHelper.useOrSuppress(aex, e);
            } catch (AmazonClientException e) {
                // The AWS client threw any unexpected exception and did not execute the request at all so we do not
                // remove any keys from the outstanding deletes set.
                aex = ExceptionsHelper.useOrSuppress(aex, e);
            }
        }
        if (aex != null) {
            throw aex;
        }
    } catch (Exception e) {
        throw new IOException("Failed to delete blobs [" + outstanding + "]", e);
    }
    assert outstanding.isEmpty();
}
Also used : MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) NoSuchFileException(java.nio.file.NoSuchFileException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) AmazonClientException(com.amazonaws.AmazonClientException) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 24 with DeleteObjectsRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project crate by crate.

the class S3BlobContainer method deleteBlobsIgnoringIfNotExists.

@Override
public void deleteBlobsIgnoringIfNotExists(List<String> blobNames) throws IOException {
    if (blobNames.isEmpty()) {
        return;
    }
    final Set<String> outstanding = blobNames.stream().map(this::buildKey).collect(Collectors.toSet());
    try (AmazonS3Reference clientReference = blobStore.clientReference()) {
        // S3 API only allows 1k blobs per delete so we split up the given blobs into requests of max. 1k deletes
        final List<DeleteObjectsRequest> deleteRequests = new ArrayList<>();
        final List<String> partition = new ArrayList<>();
        for (String key : outstanding) {
            partition.add(key);
            if (partition.size() == MAX_BULK_DELETES) {
                deleteRequests.add(bulkDelete(blobStore.bucket(), partition));
                partition.clear();
            }
        }
        if (partition.isEmpty() == false) {
            deleteRequests.add(bulkDelete(blobStore.bucket(), partition));
        }
        AmazonClientException aex = null;
        for (DeleteObjectsRequest deleteRequest : deleteRequests) {
            List<String> keysInRequest = deleteRequest.getKeys().stream().map(DeleteObjectsRequest.KeyVersion::getKey).collect(Collectors.toList());
            try {
                clientReference.client().deleteObjects(deleteRequest);
                outstanding.removeAll(keysInRequest);
            } catch (MultiObjectDeleteException e) {
                // We are sending quiet mode requests so we can't use the deleted keys entry on the exception and instead
                // first remove all keys that were sent in the request and then add back those that ran into an exception.
                outstanding.removeAll(keysInRequest);
                outstanding.addAll(e.getErrors().stream().map(MultiObjectDeleteException.DeleteError::getKey).collect(Collectors.toSet()));
                aex = ExceptionsHelper.useOrSuppress(aex, e);
            } catch (AmazonClientException e) {
                // The AWS client threw any unexpected exception and did not execute the request at all so we do not
                // remove any keys from the outstanding deletes set.
                aex = ExceptionsHelper.useOrSuppress(aex, e);
            }
        }
        if (aex != null) {
            throw aex;
        }
    } catch (Exception e) {
        throw new IOException("Failed to delete blobs [" + outstanding + "]", e);
    }
    assert outstanding.isEmpty();
}
Also used : MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) NoSuchFileException(java.nio.file.NoSuchFileException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) AmazonClientException(com.amazonaws.AmazonClientException) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 25 with DeleteObjectsRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project alluxio by Alluxio.

the class S3AUnderFileSystem method deleteObjects.

@Override
protected List<String> deleteObjects(List<String> keys) throws IOException {
    if (!mUfsConf.getBoolean(PropertyKey.UNDERFS_S3_BULK_DELETE_ENABLED)) {
        return super.deleteObjects(keys);
    }
    Preconditions.checkArgument(keys != null && keys.size() <= getListingChunkLengthMax());
    try {
        List<DeleteObjectsRequest.KeyVersion> keysToDelete = new ArrayList<>();
        for (String key : keys) {
            keysToDelete.add(new DeleteObjectsRequest.KeyVersion(key));
        }
        DeleteObjectsResult deletedObjectsResult = mClient.deleteObjects(new DeleteObjectsRequest(mBucketName).withKeys(keysToDelete));
        List<String> deletedObjects = new ArrayList<>();
        for (DeleteObjectsResult.DeletedObject deletedObject : deletedObjectsResult.getDeletedObjects()) {
            deletedObjects.add(deletedObject.getKey());
        }
        return deletedObjects;
    } catch (AmazonClientException e) {
        throw new IOException(e);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.amazonaws.services.s3.model.DeleteObjectsResult) IOException(java.io.IOException) DeleteObjectsRequest(com.amazonaws.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