Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete list of objects from Amazon s3 in java

Tags:

java

amazon-s3

Here is my code:

try {
  DeleteObjectsResult delObjRes = s3Client
      .deleteObjects(multiObjectDeleteRequest);
  System.out.format("Successfully deleted all the %s items.\n", delObjRes
      .getDeletedObjects().size());

} catch (MultiObjectDeleteException e) {
  System.out.format("%s \n", e.getMessage());
  System.out.format("No. of objects successfully deleted = %s\n", e
      .getDeletedObjects().size());
  System.out.format("No. of objects failed to delete = %s\n", e.getErrors()
      .size());
  System.out.format("Printing error data...\n");
  for (DeleteError deleteError : e.getErrors()) {
    System.out.format("Object Key: %s\t%s\t%s\n", deleteError.getKey(),
        deleteError.getCode(), deleteError.getMessage());
  }
}

exception is as follows:

Exception in thread "main" Status Code: 400, AWS Service: Amazon S3, AWS Request ID: 3EB96BFE84959731, AWS Error Code: MalformedXML, AWS Error Message: The XML you provided was not well-formed or did not validate against our published schema, S3 Extended Request ID: pE+pEHF36KqItpx1y6tJe6m50lTD1C/YHe0bVOmJW5TRBV7EfxvS5+Dc6JKX5AYb
    at 

    com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:556)
        at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:289)
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:170)
        at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2648)
        at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2620)
        at com.amazonaws.services.s3.AmazonS3Client.deleteObjects(AmazonS3Client.java:1363)
        at com.neem23.cleanup.CleanUp.deleteMultipleObjects(CleanUp.java:73)
        at com.neem23.cleanup.StartCleanUp.main(StartCleanUp.java:50)
like image 852
Digit Avatar asked Oct 22 '22 01:10

Digit


2 Answers

This question seems to be quite old, but since I happened to run into a similar issue today, it still deserves an answer. Hopefully someone else finds this page with less googling.

It Turns out I hadn't read the docs properly. Have you verified the number of objects you're trying to delete? At least the current docs (as of 2014.11.12) state that the max number of entries to delete in one go is 1000. I checked the older version 1.3.9 of the javadocs, and they don't state this limitation, but the latest ones do. So, make sure you check the number of entires in the request, and split it up into multiple parts, in case there's too many keys.

like image 147
Kai Inkinen Avatar answered Oct 30 '22 16:10

Kai Inkinen


I have seen this service exception when the List<KeyVersion> that I set on the DeleteObjectsRequest object (via the setKeys method) is empty. Looking at the code you posted in the comments above, this may be the cause of the problem you are experiencing:

List<KeyVersion> keys = new ArrayList<KeyVersion>();
for (String keyName : listOfNames) {
  if (keyName != null && !keyName.isEmpty()) keys.add(new KeyVersion(keyName));
}

/* The keys list could be empty here! */

multiObjectDeleteRequest.setKeys(keys);
like image 34
Joe Bane Avatar answered Oct 30 '22 17:10

Joe Bane