简体   繁体   English

如何使用 boto3 从 S3 向 RDS 写入数据

[英]How to write data to RDS from S3 using boto3

I want to save files that are put in my S3 bucket every 2 minutes.我想每 2 分钟保存一次放入我的 S3 存储桶中的文件。 I want to move this data to RDS using python.我想使用 python 将此数据移动到 RDS。 I am wondering what is the equivalence of this code for saving it to DynamoDB in RDS.我想知道这段代码在 RDS 中保存到 DynamoDB 的等效性是什么。 I cannot find an equivalent for the batch_writer boto3 DynamoDB call.我找不到 batch_writer boto3 DynamoDB 调用的等效项。

Thanks in advance!提前致谢! If you know a better approach feel free to comment.如果您知道更好的方法,请随时发表评论。

import boto3
import csv
import os
import tempfile

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Movies')
s3 = boto3.client('s3')


def lambda_handler(event, context):

for record in event['Records']:
    source_bucket = record['s3']['bucket']['name']
    key = record['s3']['object']['key']
    with tempfile.TemporaryDirectory() as tmpdir:
        download_path = os.path.join(tmpdir, key)
        s3.download_file(source_bucket, key, download_path)
        items = read_csv(download_path)

        with table.batch_writer() as batch:
            for item in items:
                batch.put_item(Item=item)

def read_csv(file):
    items = []
    with open(file) as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            data = {}
            data['Meta'] = {}
            data['Year'] = int(row['Year'])
            data['Title'] = row['Title'] or None
            data['Meta']['Length'] = int(row['Length'] or 0)
            data['Meta']['Subject'] = row['Subject'] or None
            data['Meta']['Actor'] = row['Actor'] or None
            data['Meta']['Actress'] = row['Actress'] or None
            data['Meta']['Director'] = row['Director'] or None
            data['Meta']['Popularity'] = row['Popularity'] or None
            data['Meta']['Awards'] = row['Awards'] == 'Yes'
            data['Meta']['Image'] = row['Image'] or None
            data['Meta'] = {k: v for k,
                            v in data['Meta'].items() if v is not None}
            items.append(data)
    return items

You can't write to RDS using Boto3, unless you are running Aurora Serverless.您无法使用 Boto3 写入 RDS,除非您运行的是 Aurora Serverless。 You would need to use the database connection library for Python that corresponds to the RDBMS engine (MySQL, PostgreSQL, etc.) that you are running in RDS.您需要使用与您在 RDS 中运行的 RDBMS 引擎(MySQL、PostgreSQL 等)相对应的 Python 数据库连接库。 You would perform batch inserts using the SQL INSERT statement.您将使用 SQL INSERT语句执行批量插入。

Add more details about your RDS database to your question if you need a more detailed answer.如果您需要更详细的答案,请将有关 RDS 数据库的更多详细信息添加到您的问题中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM