【发布时间】: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