Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an S3Object from a GetObjectResponse in AWS Java SDK 2.0

In AWS Java SDK 1.x, I could get an S3Object from an S3Client like this.

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

I'm trying to replicate that functionality using AWS Java SDK 2.0 (and end up with an S3Object), but the closest I can get is a GetObjectResponse, and I can't find any simple method calls to turn the response into an S3Object.

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

How can I get an S3Object from the 2.0 S3Client, or build one from the GetObjectResponse?

like image 993
the_storyteller Avatar asked Jan 30 '19 18:01

the_storyteller


People also ask

How do I get files from S3Object?

You can get the object's contents by calling getObjectContent on the S3Object . This returns an S3ObjectInputStream that behaves as a standard Java InputStream object. The following example downloads an object from S3 and saves its contents to a file (using the same name as the object's key).

Can you interface with AWS using Java SDK?

Develop and deploy applications with the AWS SDK for Java. The SDK makes it easy to call AWS services using idiomatic Java APIs.

How do I download from Amazon S3 bucket in Java?

To download the file we need a file name which is a key to represent file in the S3 bucket. To implement this we are using Spring boot with aws-java-sdk-s3. Amazon S3 Java SDK provides a simple interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web.


1 Answers

use ResponseInputStream. Hope the below code solves your problem.

GetObjectRequest request = GetObjectRequest.builder()
    .bucket("BucketName")
    .key("key")
    .build();
ResponseInputStream<GetObjectResponse> s3objectResponse = s3Client
    .getObject(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(s3objectResponse));

String line;            
while ((line = reader.readLine()) != null) {            
    System.out.println(line);
}
like image 73
Swaminathan S Avatar answered Nov 05 '22 14:11

Swaminathan S