I would like to export DynamoDB Table to S3 bucket in CSV format using Python (Boto3)

前端 未结 1 691
迷失自我
迷失自我 2021-01-23 12:46

This question has been asked earlier in the following link: How to write dynamodb scan data's in CSV and upload to s3 bucket using python?

I have amended the code as

相关标签:
1条回答
  • 2021-01-23 13:20

    I have revised the code to be simpler and to also handle paginated responses for tables with more than 1MB of data:

    import csv
    import boto3
    import json
    
    TABLE_NAME = 'employee_details'
    OUTPUT_BUCKET = 'my-bucket'
    TEMP_FILENAME = '/tmp/employees.csv'
    OUTPUT_KEY = 'employees.csv'
    
    s3_resource = boto3.resource('s3')
    dynamodb_resource = boto3.resource('dynamodb')
    table = dynamodb_resource.Table(TABLE_NAME)
    
    def lambda_handler(event, context):
    
        with open(TEMP_FILENAME, 'w') as output_file:
            writer = csv.writer(output_file)
            header = True
            first_page = True
    
            # Paginate results
            while True:
    
                # Scan DynamoDB table
                if first_page:
                    response = table.scan()
                    first_page = False
                else:
                    response = table.scan(ExclusiveStartKey = response['LastEvaluatedKey'])
    
                for item in response['Items']:
    
                    # Write header row?
                    if header:
                        writer.writerow(item.keys())
                        header = False
    
                    writer.writerow(item.values())
    
                # Last page?
                if 'LastEvaluatedKey' not in response:
                    break
    
        # Upload temp file to S3
        s3_resource.Bucket(OUTPUT_BUCKET).upload_file(TEMP_FILENAME, OUTPUT_KEY)
    
    0 讨论(0)
提交回复
热议问题