· engineering  · 2 min read

Paginating in AWS SDK

Easy pagination of api calls using AWS SDK's little known built-in utilities.

Easy pagination of api calls using AWS SDK's little known built-in utilities.

It’s a common task to fetch lists of AWS resources, and managing the pagination of these resources can often seem like a chore. Whether you’re dealing with DynamoDB items, S3 objects, or account listings, each service seems to have its method for handling continuation tokens.

Pagination utilities to the rescue!

Luckily for us, Amazon have taken care of this for us in the JavaScript SDK v3, providing a set of pagination utilities that greatly simplify the task. For example:

For most of the API operations, there is now a paginator pattern implemented, with the prefix ‘paginate’, making the pattern paginate<operation> - for example:

Using it is easy, the paginators return an AsyncIterator, so we can just loop over it as follows:

const paginator = paginateListObjectsV2(paginationConfig, params);
 
const contents: _Object[] = [];
for await (const page of paginator) {
  contents.push(...page.Contents);
}

The paginationConfig parameter allows us to specify things like the page size and the startingToken

That’s it - no more boiler plate, and less scope for bugs to creep in!

Further reading

It’s worth reading up on async generator functions to understand how this has been implemented.

There’s similar functionality in the other SDKs too, for example php, python, java, perhaps the Javascript documentation is just lacking.

James Babington

About James Babington

A cloud architect and engineer with a wealth of experience across AWS, web development, and security, James enjoys writing about the technical challenges and solutions he's encountered, but most of all he loves it when a plan comes together and it all just works.

Comments

No comments yet. Be the first to comment!

Leave a Comment

Check this box if you don't want your comment to be displayed publicly.

Back to Blog

Related Posts

View All Posts »