Delete list of objects from Amazon s3 in java

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()); } } 

The 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) 
+4
source share
2 answers

I saw this service exception when the List<KeyVersion> , which I set for the DeleteObjectsRequest object (via the setKeys method), is empty . If you look at the code that you posted in the comments above, this could be the cause of the problem you are having:

 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); 
+1
source

This question seems rather old, but since today I was faced with a similar problem, it still deserves an answer. Hope someone else finds this page with fewer searches.

It turns out I did not read the documents properly. Have you checked the number of objects you are trying to delete? At least in the current documents (as of 2014.11.12) it is indicated that the maximum number of records to delete at a time is 1000 . I checked the older version 1.3.9 of javadocs and they did not indicate this limitation, but the latter ones . Therefore, make sure that you check the number of requests in the request and divide it into several parts if there are too many keys.

+1
source

Source: https://habr.com/ru/post/1501602/


All Articles