Search in sources :

Example 16 with DeleteObjectsRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest in project druid by druid-io.

the class S3Utils method deleteBucketKeys.

private static void deleteBucketKeys(ServerSideEncryptingAmazonS3 s3Client, String bucket, List<DeleteObjectsRequest.KeyVersion> keysToDelete) throws Exception {
    DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucket).withKeys(keysToDelete);
    S3Utils.retryS3Operation(() -> {
        s3Client.deleteObjects(deleteRequest);
        return null;
    });
}
Also used : DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 17 with DeleteObjectsRequest

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

the class DeleteMultiObjects method deleteBucketObjects.

// snippet-start:[s3.java2.delete_many_objects.main]
public static void deleteBucketObjects(S3Client s3, String bucketName) {
    // Upload three sample objects to the specfied Amazon S3 bucket.
    ArrayList<ObjectIdentifier> keys = new ArrayList<>();
    PutObjectRequest putOb = null;
    ObjectIdentifier objectId = null;
    for (int i = 0; i < 3; i++) {
        String keyName = "delete object example " + i;
        objectId = ObjectIdentifier.builder().key(keyName).build();
        putOb = PutObjectRequest.builder().bucket(bucketName).key(keyName).build();
        s3.putObject(putOb, RequestBody.fromString(keyName));
        keys.add(objectId);
    }
    System.out.println(keys.size() + " objects successfully created.");
    // Delete multiple objects in one request.
    Delete del = Delete.builder().objects(keys).build();
    try {
        DeleteObjectsRequest multiObjectDeleteRequest = DeleteObjectsRequest.builder().bucket(bucketName).delete(del).build();
        s3.deleteObjects(multiObjectDeleteRequest);
        System.out.println("Multiple objects are deleted!");
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Delete(software.amazon.awssdk.services.s3.model.Delete) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) ObjectIdentifier(software.amazon.awssdk.services.s3.model.ObjectIdentifier) DeleteObjectsRequest(software.amazon.awssdk.services.s3.model.DeleteObjectsRequest)

Example 18 with DeleteObjectsRequest

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

the class ListEndpointIds method deleteS3Objects.

private static void deleteS3Objects(String s3BucketName, List<String> keys) {
    AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
    String[] keysArray = keys.toArray(new String[keys.size()]);
    DeleteObjectsRequest request = new DeleteObjectsRequest(s3BucketName).withKeys(keysArray);
    System.out.println("Deleting the following Amazon S3 objects created by Amazon Pinpoint:");
    for (String key : keys) {
        System.out.println("\t- " + key);
    }
    try {
        s3Client.deleteObjects(request);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Finished deleting objects.");
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonServiceException(com.amazonaws.AmazonServiceException) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 19 with DeleteObjectsRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectsRequest 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();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) KeyVersion(com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) Regions(com.amazonaws.regions.Regions) DeleteObjectsResult(com.amazonaws.services.s3.model.DeleteObjectsResult) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 20 with DeleteObjectsRequest

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