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 :
DeleteObjectsRequest
to be able to delete up to 1000 objects in a single HTTP call towards AWS APIWhen 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 methods 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);
}
}