使用 AWS SDK V2 删除对象?

deleteObjects using AWS SDK V2?

我正在尝试从 AWS SDK V1.x 迁移到 V2.2。不过,我无法弄清楚 deleteObjects 方法。我发现了一堆例子 - 都是一样的 :-( 似乎从来没有使用要删除的对象列表(即列表存在,但从未在 DeleteObjectsRequest object - 我假设这是应该设置的地方,但看不到哪里。How/where 我提供对象列表吗?我找到的例子是:

    System.out.println("Deleting objects from S3 bucket: " + bucket_name);
    for (String k : object_keys) {
        System.out.println(" * " + k);
    }

    Region region = Region.US_WEST_2;
    S3Client s3 = S3Client.builder().region(region).build();
    try {
        DeleteObjectsRequest dor = DeleteObjectsRequest.builder()
                .bucket(bucket_name)
                .build();
        s3.deleteObjects(dor);
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }

看起来还需要一些对象来分配 s3 中对象的键。这是未经测试的,我把方法的链接放在最后。

System.out.println("Deleting objects from S3 bucket: " + bucket_name);

for (String k : object_keys) {
    System.out.println(" * " + k);
}

Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();

try {

   ObjectIdentifier objectToDelete = ObjectIdentifier.Builder()
            .key(KEY_OBJECT_TO_DELETE);

   Delete delete_me Delete.Builder.objects(objectToDelete) 

   DeleteObjectsRequest dor = DeleteObjectsRequest.builder()
            .bucket(bucket_name)
            .delete(delete_me)
            .build();

    s3.deleteObjects(dor);

} catch (S3Exception e) {
    System.err.println(e.awsErrorDetails().errorMessage());
    System.exit(1);
}

要删除的键 https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/model/ObjectIdentifier.html#key--

删除对象 https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/model/ObjectIdentifier.Builder.html

先前接受的答案非常有帮助,尽管它不是 100% 完整。我用它写了下面的方法。虽然我还没有特别好地测试它的错误处理,但它基本上可以工作。

  • 传入一个String个键的数组,转换成
    个数组 ObjectIdentifierdeleteObjects 需要的。
  • s3Clientlog 假定已在别处初始化。如果不需要,请随时删除日志记录。
  • 该方法当前删除成功returns次

    public int deleteS3Objects(String bucketName, String[] keys) {
    
    List<ObjectIdentifier> objIds = Arrays.stream(keys)
            .map(key -> ObjectIdentifier.builder().key(key).build())
            .collect(Collectors.toList());
    try {
        DeleteObjectsRequest dor = DeleteObjectsRequest.builder()
                .bucket(bucketName)
                .delete(Delete.builder().objects(objIds).build())
                .build();
    
        DeleteObjectsResponse delResp = s3client.deleteObjects(dor);
    
        if (delResp.errors().size() > 0) {
            String err = String.format("%d errors returned while deleting %d objects",
                    delResp.errors().size(), objIds.size());
            log.warn(err);
        }
        if (delResp.deleted().size() < objIds.size()) {
            String err = String.format("%d of %d objects deleted",
                    delResp.deleted().size(), objIds.size());
            log.warn(err);
        }
        return delResp.deleted().size();
    }
    catch(AwsServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process 
        // it, so it returned an error response.
        log.error("Error received from S3 while attempting to delete objects", e);
    }
    catch(SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        log.error("Exception occurred while attempting to delete objects from S3", e);
    }
    return 0;
    }
    

(无端评论:我觉得很奇怪,在 AWS SDK v2.3.9 中,deleteObjects 需要 Delete.BuilderObjectIdentifier 键,但 getObjectputObject 接受字符串键。为什么 DeleteObjectsRequest.Builder 没有 keys() 方法?他们还没有正式表示 SDK 是生产就绪的 AFAIK,所以其中一些可能会发生变化。)