Coroutines
The AWS SDK for Kotlin is asynchronous by default. The SDK for Kotlin uses suspend
functions
for all operations, which are meant to be called from a coroutine.
For a more in-depth guide to coroutines, see the official Kotlin
documentation
Making concurrent requests
The asyncasync
returns a Deferred
If you don't care about the results (only that an operation completed), you can use the
launchlaunch
is conceptually similar to
async
. The difference is that launch returns a Jobasync
returns a Deferred
.
The following is an example of making concurrent requests to Amazon S3 using the headObject
import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking import kotlin.system.measureTimeMillis import aws.sdk.kotlin.services.s3.S3Client fun main(): Unit = runBlocking { val s3 = S3Client { region = "us-east-2" } val myBucket = "<your-bucket-name-here>" val key1 = "<your-object-key-here>" val key2 = "<your-second-object-key-here>" val resp1 = async { s3.headObject{ bucket = myBucket key = key1 } } val resp2 = async { s3.headObject{ bucket = myBucket key = key2 } } val elapsed = measureTimeMillis { val totalContentSize = resp1.await().contentLength + resp2.await().contentLength println("content length of $key1 + $key2 = $totalContentSize") } println("requests completed in $elapsed ms") }
Making blocking requests
To make service calls from existing code that doesn’t use coroutines and implements a
different threading model, you can use the runBlocking
As its name suggests, this runBlocking
builder launches a new coroutine and
blocks the current thread until it completes.
Warning
runBlocking
should not generally be used from a coroutine. It is designed to bridge regular
blocking code to libraries that are written in suspending style (such as in main functions and tests).