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
?
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).
Develop and deploy applications with the AWS SDK for Java. The SDK makes it easy to call AWS services using idiomatic Java APIs.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With