Last active
July 3, 2022 16:17
-
-
Save em-shea/8fd7b4d96852329237790285a6715bae to your computer and use it in GitHub Desktop.
Example script to export DynamoDB table data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
# Specify your table name and the region your table is in | |
export_table_name = "ExportDataTable" | |
dynamodb = boto3.resource('dynamodb', region_name='us-east-1') | |
table = dynamodb.Table(export_table_name) | |
def export_table_data(): | |
print(f"Exporting data from {export_table_name}") | |
response = table.scan() | |
data = response['Items'] | |
# Paginate through DynamoDB table response | |
while 'LastEvaluatedKey' in response: | |
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey']) | |
data.extend(response['Items']) | |
# Create or open a text file to save exported data | |
with open(f"data_export_{export_table_name}.json","w+") as f: | |
json_data = json.dumps(data) | |
f.write(json_data) | |
print("Export complete!") | |
export_table_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment