在java中删除Amazon s3中的对象列表

时间:2013-09-11 07:29:14

标签: java amazon-s3

这是我的代码:

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 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)

3 个答案:

答案 0 :(得分:2)

这个问题似乎很老了,但是因为我今天遇到了类似的问题,所以它仍然值得回答。希望其他人找到这个页面少用谷歌搜索。

原来我没有正确阅读文档。您是否验证了要删除的对象数量?至少当前的文档(截至2014.11.12)规定一次删除的最大条目数is 1000。 我查看了较旧的version 1.3.9 of the javadocs,但他们没有说明此限制,只是the latest ones do。因此,请确保检查请求中的entires数量,并将其拆分为多个部分,以防密钥太多。

答案 1 :(得分:1)

我在List<KeyVersion>对象上设置的DeleteObjectsRequest(通过setKeys方法)为空时,我看到了此服务异常。查看您在上述评论中发布的代码,这可能是您遇到问题的原因:

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

答案 2 :(得分:0)

要删除s3存储桶中特定源文件夹中的多个S3对象,可以使用以下代码。

这里首先列出文件夹的对象并删除列表。

列出对象

public static void listFiles(S3Request s3Request) {
        S3Validation.validateSource(s3Request);
        ListObjectsRequest listObjectRequest= new ListObjectsRequest().withBucketName(s3Request.getBucketName()).withPrefix(s3Request.getSourcekey());
        ObjectListing objectListing = AWSClient.getS3Client().listObjects(listObjectRequest);
        List<S3ObjectSummary> objectSummary=objectListing.getObjectSummaries();
        response.setList(objectSummary);
        } 

删除对象列表

        S3Response s3Response = listFiles(s3Request);
        String setDest=s3Request.getDestKey();
        List<S3ObjectSummary> objectSummary=s3Response.getList();
        for (S3ObjectSummary summary : objectSummary) {
                s3Request.setSourcekey(summary.getKey()); 
                s3Request.setDestKey(setDest+FilenameUtils.getName(summary.getKey()));
                DeleteObjectRequest deleteObjectRequest =new DeleteObjectRequest(s3Request.getBucketName(), s3Request.getSourcekey());
                AWSClient.getS3Client().deleteObject(deleteObjectRequest);
            }
    }

S3Request类是pojo类。您可以使用设置器创建单元测试。 (s3Request.setBucketName("<bucketName>"), s3Request.setSourcekey("<keypath_of_the_folder>")