Last active
March 19, 2025 05:47
-
-
Save sumeetswn/2a9c80be1e17071a17a93d0dcf20de51 to your computer and use it in GitHub Desktop.
Import and Migrate DynamoDB Tables
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 boto3 | |
import simplejson as json | |
AWS_ACCESS_KEY_ID = "" | |
AWS_SECRET_ACCESS_KEY = "" | |
AWS_REGION = "" | |
dynamodb_resource = boto3.resource('dynamodb', | |
aws_access_key_id=AWS_ACCESS_KEY_ID, | |
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, | |
region_name=AWS_REGION) | |
dynamodb_client = boto3.client('dynamodb', | |
aws_access_key_id=AWS_ACCESS_KEY_ID, | |
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, | |
region_name=AWS_REGION) | |
db_list = { | |
"<Table-Name>": "<File-Name>.json", | |
} | |
def get_table_data(table_instance): | |
res = table_instance.scan() | |
data = res.get('Items', []) | |
while 'LastEvaluatedKey' in res: | |
res = table_instance.scan(ExclusiveStartKey=res['LastEvaluatedKey']) | |
data.extend(res.get('Items', [])) | |
return data | |
def main(): | |
for key, val in db_list.items(): | |
table_instance = dynamodb_resource.Table(key) | |
table_items = get_table_data(table_instance) | |
json_data = json.dumps(table_items, use_decimal=True) | |
configs_file = open(f"dumps/{val}", 'w', encoding='utf-8') | |
configs_file.write(json_data) | |
configs_file.close() | |
main() |
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 boto3 | |
import simplejson as json | |
AWS_ACCESS_KEY_ID = "" | |
AWS_SECRET_ACCESS_KEY = "" | |
AWS_REGION = "" | |
dynamodb_resource = boto3.resource('dynamodb', | |
aws_access_key_id=AWS_ACCESS_KEY_ID, | |
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, | |
region_name=AWS_REGION) | |
dynamodb_client = boto3.client('dynamodb', | |
aws_access_key_id=AWS_ACCESS_KEY_ID, | |
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, | |
region_name=AWS_REGION) | |
db_list = { | |
"<Table-Name>": "<File-Name>.json", | |
} | |
def main(): | |
for key, val in db_list.items(): | |
table_instance = dynamodb_resource.Table(key) | |
configs_file = open(f"dumps/{val}", 'r', encoding='utf-8') | |
configs_data = json.load(configs_file) | |
for item in configs_data: | |
table_instance.put_item(Item=item) | |
configs_file.close() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment