Complete scan of dynamoDb with boto3 Complete scan of dynamoDb with boto3 python python

Complete scan of dynamoDb with boto3


I think the Amazon DynamoDB documentation regarding table scanning answers your question.

In short, you'll need to check for LastEvaluatedKey in the response. Here is an example using your code:

import boto3dynamodb = boto3.resource('dynamodb',                          aws_session_token=aws_session_token,                          aws_access_key_id=aws_access_key_id,                          aws_secret_access_key=aws_secret_access_key,                          region_name=region)table = dynamodb.Table('widgetsTableName')response = table.scan()data = response['Items']while 'LastEvaluatedKey' in response:    response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])    data.extend(response['Items'])


boto3 offers paginators that handle all the pagination details for you. Here is the doc page for the scan paginator. Basically, you would use it like so:

import boto3client = boto3.client('dynamodb')paginator = client.get_paginator('scan')for page in paginator.paginate():    # do something


Riffing off of Jordon Phillips's answer, here's how you'd pass a FilterExpression in with the pagination:

import boto3client = boto3.client('dynamodb')paginator = client.get_paginator('scan')operation_parameters = {  'TableName': 'foo',  'FilterExpression': 'bar > :x AND bar < :y',  'ExpressionAttributeValues': {    ':x': {'S': '2017-01-31T01:35'},    ':y': {'S': '2017-01-31T02:08'},  }}page_iterator = paginator.paginate(**operation_parameters)for page in page_iterator:    # do something