Search code examples
javaamazon-s3aws-java-sdk-2.x

Get an S3Object InputStream from a GetObjectResponse in AWS Java SDK 2 Using S3AsyncClient?


How can a plain InputStream be created using the S3AsyncClient for a getObject request?

The S3AsyncClient for AWS JDK 2.0 does not seem to have a function that returns a ResponseInputStream<GetObjectResponse> the same way that the S3Client synchronous client does.

The only return type available is a CompletableFuture<GetObjectResponse>, but the methods returning that type assume that the data is going to a local path.

S3AsyncClient s3AsyncClient = ...;


CompletableFuture<GetObjectResponse> response = s3AsyncClient.getObject(
    GetObjectRequest.builder()
            .bucket(bucket)
            .key(key)
            .build(),
    path));    

There is a similar stackoverflow item, but the question and answer both utilize the synchronous S3Client.

Thank you in advance for your consideration and response.


Solution

  • Below code would get you the InputStream.

    ResponseInputStream responseInputStream = s3AsyncClient.getObject(GetObjectRequest.builder()
            .bucket(bucket)
            .key(key)
            .build(),
            AsyncResponseTransformer.toBlockingInputStream())