从AWS Java SDK 2.0中的GetObjectResponse获取S3Object

时间:2019-01-30 18:33:51

标签: java aws-java-sdk-2.x

在AWS Java SDK 1.x中,我可以像这样从S3Object获得S3Client

S3Object obj = mS3Client.getObject(pBucket, pKey);

我正在尝试使用AWS Java SDK 2.0复制该功能(并以S3Object结尾),但是我能得到的最接近的是GetObjectResponse,但我找不到任何简单的方法调用即可将响应转换为S3Object

GetObjectResponse response = mS3Client.getObject(
        GetObjectRequest.builder()
                .bucket(pBucket)
                .key(pKey)
                .build())
        .response();

如何从2.0 S3Object中获得一个S3Client,或从GetObjectResponse中构建一个?{p>

1 个答案:

答案 0 :(得分:0)

使用ResponseInputStream。希望以下代码能解决您的问题。

 ResponseInputStream<GetObjectResponse> s3objectResponse = s3Client.getObject(GetObjectRequest.builder().bucket("BucketName").key("key").build());
    BufferedReader reader = new BufferedReader(new InputStreamReader(s3objectResponse));
    String line;            
    while ((line = reader.readLine()) != null) {            
            System.out.println(line);
    }