【问题标题】:Automate CSV File Creation Using Boto3 S3 and Lambda使用 Boto3 S3 和 Lambda 自动创建 CSV 文件
【发布时间】:2020-04-14 06:45:14
【问题描述】:

我正在 LinuxAcademy.com 网站上做一个实验室课程名称是使用 Lambda、Python 和 Boto3 自动化 AWS,而我遇到问题的具体实验室是 Lecture: Importing CSV 文件到 DynamoDB 中

在本实验中,我们将一个 .csv 文件上传到 S3,在该特定存储桶中生成一个 S3 事件,然后启动如下所示的 Lambda 脚本:

import csv
import os
import tempfile

import boto3

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_file)

            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']['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}

上传到 s3 的 .csv 文件确实会调用 lambda 函数。 我在第 20 行遇到错误:items = read_csv(download_file)

来自 AWS CloudWatch 的错误:

[ERROR] NameError: name 'download_file' is not defined
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 20, in lambda_handler
    items = read_csv(download_file)

【问题讨论】:

    标签: python-3.x amazon-web-services aws-lambda boto3


    【解决方案1】:
    s3.download_file(source_bucket, key, download_path)
    items = read_csv(download_file)
    

    第一行在Amazon S3客户端调用download_file()方法下载文件到本地磁盘。

    第二行调用read_csv() 函数,传递一个名为download_file 的变量。但是,尚未定义名为 download_file 的变量,这就是您收到错误消息的原因。

    在查看代码时,read_csv() 函数期望打开文件的名称。这似乎在 download_path 变量中可用,其中包含本地目录和密钥。因此,将其更改为:

    items = read_csv(download_path)
    

    【讨论】:

    • 我已按照建议修改了代码。我现在收到一个新错误,根据我的研究,我倾向于认为我提供的 .csv 文件没有被正确解析。 CloudWatch 的新错误:[ERROR] TypeError: 'NoneType' object is not iterable Traceback(最近一次调用最后一次):文件“/var/task/lambda_function.py”,第 26 行,在 lambda_handler 中的项目:跨度>
    猜你喜欢
    • 1970-01-01
    • 2021-05-20
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2018-10-10
    • 1970-01-01
    • 2021-07-13
    相关资源
    最近更新 更多