0

I need to download a lot of files from S3 so I wrapped many calls to s3Client.getObject(request, destination) with futures, but there's no mention of it being blocking so I'm afraid I might run into a race condition where all the download calls were made but the files weren't really downloaded yet.

Is it safe to assume this api is blocking since there's no mention of it being async?

To clarify, by blocking I mean that the flow won't continue after the s3Client.getObject(request, destination) line until the download is finished.

enter image description here

smac2020
  • 9,637
  • 4
  • 24
  • 38
shinzou
  • 5,850
  • 10
  • 60
  • 124

3 Answers3

1

Looking at the code on Github, it is indeed blocking the call (invoke).

Thomas Jungblut
  • 20,854
  • 6
  • 68
  • 91
  • @smac2020 I assumed that the question was about the sync client and whether it is blocking - not about the async client which is obviously async. – Thomas Jungblut Sep 26 '21 at 18:21
1

TO use the latest non-blocking code (AWS SDK for Java V2), refer to this Github example. This shows how to use getObject using the asynchronous client.

https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/s3/src/main/java/com/example/s3/S3AsyncStreamOps.java

smac2020
  • 9,637
  • 4
  • 24
  • 38
0

Using SDK V2 with reactive streams integration.

  • toBytes

    Mono.fromFuture(s3Client.getObject(getObjectRequest, AsyncResponseTransformer.toBytes()))
              .map(ResponseBytes::asUtf8String)
              .flatMapMany(s -> Flux.fromArray(s.split("\n")))
              .map(stringLine -> ...)
    
  • toFile

    Mono.fromFuture(s3Client.getObject(getObjectRequest, AsyncResponseTransformer.toFile(Path.of(localFilePath))))
              .thenMany(Flux.using(
                              // https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#lines-java.nio.file.Path-
                              // This method does not read all lines into a List, but instead populates lazily as the stream is consumed.
                              () -> Files.lines(Paths.get(localFilePath)),
                              Flux::fromStream,
                              Stream::close)
              .map(stringLine -> ...)
    
Johan Perez
  • 171
  • 1
  • 4