Skip to content

Instantly share code, notes, and snippets.

@raecoo
Last active November 28, 2019 03:51
Show Gist options
  • Save raecoo/7f4291653048a61ca4df953249e04c15 to your computer and use it in GitHub Desktop.
Save raecoo/7f4291653048a61ca4df953249e04c15 to your computer and use it in GitHub Desktop.
DynamoDB data to JSON file
# Dump DynamoDB data to JSON file
# s3://[bucket]/offine-cms-[table-name].json
# Refs: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html
import decimal
import json
import boto3
from boto3.dynamodb.conditions import Key, Key, Attr
class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, decimal.Decimal):
return float(obj)
return super(DecimalEncoder, self).default(obj)
def aws_s3_sync(region_s, bucket_s, file_name_s, data_s):
from botocore.client import Config
s3_client = boto3.client(service_name='s3', region_name=region_s,
config=Config(signature_version='s3'))
s3_client.put_object(Body=data_s, Bucket=bucket_s, Key=file_name_s)
def dump(region_s, table_name_s, bucket_s):
session = boto3.Session(region_name=region_s)
dynamodb = session.resource('dynamodb')
table = dynamodb.Table(table_name_s)
response = table.scan(AttributesToGet=['content_id', 'content', 'updated_at'])
data = response['Items']
while 'LastEvaluatedKey' in response:
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
data.extend(response['Items'])
aws_s3_sync(region_s, bucket_s, 'offine-cms-{0}.json'.format(table_name_s), json.dumps(data, sort_keys=True, cls=DecimalEncoder))
dump('us-east-1', 'table-name', 'bucket-name')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment