简体   繁体   中英

AWS SDK for Java version 2 - Delete S3 "folder" or Delete multiple S3 objects

I am searching how to delete a S3 folder using AWS SDK for Java version 2. I only managed to find AWS SDK version 1 examples.

I know that S3 is an object store and that the concept of folder does not exist. What I mean here is :

  • List the S3 objects of a given bucket with a given prefix
  • Delete the returned objects using a DeleteObjectsRequest to be able to delete up to 1000 objects in a single HTTP call towards AWS API

When I am searching for examples, I constantly go back to this page : https://docs.aws.amazon.com/AmazonS3/latest/dev/DeletingMultipleObjectsUsingJava.html where it seems this is the version 1 of the AWS SDK for Java that is used. At least, on my side, I imported AWS SDK 2 and I cannot directly instantiate DeleteObjectsRequest as it is shown in this example. I am forced to use builders then I don't find the same mathods to specify the list of keys to be deleted.

I managed to make it work with the piece of code below.

But I find this way of doing quite cumbersome and I still would like to check with the community if this is the correct way of doing. I especially find quite cumbersome the need to go from a collection of S3Object to a collection of ObjectIdentifier and the chains of builders needed. Why DeleteObjectsRequest 's builder does not simply allow to specify a collection of strings being the keys of the objects to be deleted ?

public static void deleteS3Objects(String bucket, String prefix) {
    ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(bucket).prefix(prefix).build();
    ListObjectsV2Iterable list = s3Client.listObjectsV2Paginator(request);
    for (ListObjectsV2Response response : list) {
        List<S3Object> objects = response.contents();
        List<ObjectIdentifier> objectIdentifiers = objects.stream().map(o -> ObjectIdentifier.builder().key(o.key()).build()).collect(Collectors.toList());
        DeleteObjectsRequest deleteObjectsRequest = DeleteObjectsRequest.builder().bucket(bucket).delete(Delete.builder().objects(objectIdentifiers).build()).build();
        s3Client.deleteObjects(deleteObjectsRequest);
    }
}

This is an improvement on @Comencau's helpful answer, it handles the case of no objects being found: MalformedXML: The XML you provided was not well-formed or did not validate against our published schema

public static void deleteS3Data(String bucket, String prefix) {
    S3Client s3Client = S3Client.builder().region(region).build();
    ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(bucket).prefix(prefix).build();
    ListObjectsV2Iterable list = s3Client.listObjectsV2Paginator(request);

    List<ObjectIdentifier> objectIdentifiers = list
            .stream()
            .flatMap(r -> r.contents().stream())
            .map(o -> ObjectIdentifier.builder().key(o.key()).build())
            .collect(Collectors.toList());

    if (objectIdentifiers.isEmpty()) return;
    DeleteObjectsRequest deleteObjectsRequest = DeleteObjectsRequest
            .builder()
            .bucket(bucket)
            .delete(Delete.builder().objects(objectIdentifiers).build())
            .build();
    s3Client.deleteObjects(deleteObjectsRequest);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM