5

这是我的代码:

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

3 回答 3

2

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

原来我没有正确阅读文档。您是否验证了要删除的对象数量?至少当前文档(截至 2014.11.12)声明一次性删除的最大条目数为 1000。我检查了 javadocs 的旧版本 1.3.9,他们没有说明这个限制,但最新的有。因此,请确保检查请求中的整体数量,并将其拆分为多个部分,以防键过多。

于 2014-11-12T20:28:21.140 回答
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);
于 2014-03-29T20:01:27.540 回答
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 类。您可以使用 setter 创建单元测试。(s3Request.setBucketName("<bucketName>"), s3Request.setSourcekey("<keypath_of_the_folder>")

于 2019-12-30T07:54:28.590 回答