Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Created February 25, 2024 12:20
Show Gist options
  • Save thanakijwanavit/888d8738477243bf084baed898e86dbe to your computer and use it in GitHub Desktop.
Save thanakijwanavit/888d8738477243bf084baed898e86dbe to your computer and use it in GitHub Desktop.
export and decode dynamodb
def decodeDynamo(path):
allItems = []
with gzip.open(path, 'rb') as f:
for line in f:
try:
line_str = line.decode('utf-8').strip()
item_data = json.loads(line_str)['Item']['data']['S']
allItems.append(json.loads(item_data))
except json.JSONDecodeError as e: # Change this to the specific exceptions you expect
print(f"Error parsing line: {e}")
return allItems
def exportTable():
# Initialize the DynamoDB client
dynamodb = boto3.client('dynamodb', region_name='ap-southeast-1')
# Format today's date in YYYY-MM-DD format for the S3 prefix
today = datetime.now(timezone.utc).strftime('%Y-%m-%d') # Adjusted for UTC time
# Set the parameters for the export
table_name = 'order-table-dev'
s3_bucket = 'villa-order-log' # Replace with your actual S3 bucket name
s3_prefix = f'dynamodb-export/{table_name}/{today}/' # Systematically naming the prefix
try:
# Start the export job
response = dynamodb.export_table_to_point_in_time(
TableArn=f'arn:aws:dynamodb:ap-southeast-1:your-account-id:table/{table_name}',
S3Bucket=s3_bucket,
S3Prefix=s3_prefix,
ExportFormat='DYNAMODB_JSON' # Choose between 'DYNAMODB_JSON' and 'ION'
)
return response
except Exception as e:
print(e)
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment