メモ:DynamoDBをjsonでexport & import
Export
CLIで落とす
aws dynamodb scan --table-name テーブル名 > ./dynamodb.json
Import
Pythonで復元
import boto3
import json
from boto3.dynamodb.types import TypeDeserializer
# DynamoDB のリソースとテーブル名を指定
dynamodb = boto3.resource('dynamodb')
table_name = 'テーブル名'
table = dynamodb.Table(table_name)
deserializer = TypeDeserializer()
with open('dynamodb.json', 'r', encoding='utf-8') as f:
backup_data = json.load(f)
# jsonの1行ずつ変換 -> DDBへput
for raw_item in backup_data['Items']:
# DynamoDB ネイティブ形式を通常の辞書形式に変換
deserialized_item = {k: deserializer.deserialize(v) for k, v in raw_item.items()}
try:
table.put_item(Item=deserialized_item)
print(f"Successfully inserted item: {deserialized_item}")
except Exception as e:
print(f"Error inserting item: {deserialized_item}, Error: {e}")
print("Complete!")