Pagination - Amazon SDK for Kotlin
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Pagination

Many Amazon operations return paginated results when the payload is too large to return in a single response. The Amazon SDK for Kotlin includes extensions to the service client interface that auto paginate the results for you. You only have to write the code that processes the results.

Pagination is exposed as a Flow<T> so that you can take advantage of Kotlin's idiomatic transforms for asynchronous collections (such as map, filter, and take). Exceptions are transparent, which makes error handling feel like a regular API call, and cancellation adheres to the general cooperative cancellation of coroutines. For more information, see flows and flow exceptions in the official guide.

Note

The following examples use Amazon S3. However, the concepts are the same for any service that has one or more paginated APIs. All pagination extensions are defined in the aws.sdk.kotlin.<service>.paginators package (such as aws.sdk.kotlin.dynamodb.paginators).

The following code example shows how you can process the paginated response from the listObjectsV2Paginated function call.

Imports

import aws.sdk.kotlin.services.s3.S3Client import aws.sdk.kotlin.services.s3.paginators.listObjectsV2Paginated import kotlinx.coroutines.flow.*

Code

val s3 = S3Client.fromEnvironment() val req = ListObjectsV2Request { bucket = "<my-bucket>" maxKeys = 1 } s3.listObjectsV2Paginated(req) // Flow<ListObjectsV2Response> .transform { it.contents?.forEach { obj -> emit(obj) } } .collect { obj -> println("key: ${obj.key}; size: ${obj.size}") }