How to read JSON files from S3 using the S3AsyncClient

Viewed 4309

I cant figure out how to read a JSON file from S3 into memory as String.

The examples I find calls getObjectContent() however this is not available for the GetObjectResponse I get from the S3AsyncClient.

The code I experiment is the sample code from AWS.

// Creates a default async client with credentials and AWS Region loaded from the
// environment
S3AsyncClient client = S3AsyncClient.create();

// Start the call to Amazon S3, not blocking to wait for the result
CompletableFuture<GetObjectResponse> responseFuture =
        client.getObject(GetObjectRequest.builder()
                                         .bucket("my-bucket")
                                         .key("my-object-key")
                                         .build(),
                         AsyncResponseTransformer.toFile(Paths.get("my-file.out")));

// When future is complete (either successfully or in error), handle the response
CompletableFuture<GetObjectResponse> operationCompleteFuture =
        responseFuture.whenComplete((getObjectResponse, exception) -> {
            if (getObjectResponse != null) {
                // At this point, the file my-file.out has been created with the data
                // from S3; let's just print the object version
                System.out.println(getObjectResponse.versionId());
            } else {
                // Handle the error
                exception.printStackTrace();
            }
        });

// We could do other work while waiting for the AWS call to complete in
// the background, but we'll just wait for "whenComplete" to finish instead
operationCompleteFuture.join();

How should this code be modified so that I can get the actual JSON content from the GetObjectResponse?

2 Answers

After response is transformed to bytes it can be transformed to string:

S3AsyncClient client = S3AsyncClient.create();

GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket("my-bucket").key("my-object-key").build();

client.getObject(getObjectRequest, AsyncResponseTransformer.toBytes())
      .thenApply(ResponseBytes::asUtf8String)
      .whenComplete((stringContent, exception) -> {
          if (stringContent != null)
              System.out.println(stringContent);
          else
              exception.printStackTrace();
      });

You can use AsyncResponseTransformer.toBytes in order to save response to a byte array rather than a file. javadoc

Related