boto3 pagination: Query contents of page by it's exact number

Viewed 2761

Trying to implement pagination using boto's get_paginator for query operation. I can't find proper way how to get, let's say, page num 3 without loading contents of previous two pages:

import boto3
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource("dynamodb", region_name="us-west-2")
paginator = dynamodb.meta.client.get_paginator("query")

pages = paginator.paginate(TableName="results",
                           IndexName="year-timestamp-index",
                           KeyConditionExpression=Key("year").eq("2017"),      
                           PaginationConfig={"PageSize": 50})

def get_content_of_page(requested_page_num, pages):
  pages_counter = 0
  for page in pages:
    if pages_counter == requested_page_num - 1:
      return page.get("Items")
    pages_counter += 1

get_content_of_page(pages, 3) # will load and skip contents of
                              # 2 pages and return content of 3rd

I consider it's too expensive, is it possible to get directly 3rd page?

Like Mongo's cursor.skip(page_size * (requested_page_num - 1)).limit(page_size)

0 Answers
Related