【发布时间】:2019-11-12 21:52:12
【问题描述】:
我正在尝试读取上传到 s3 存储桶上的 csv 文件的内容。为此,我从触发 lambda 函数的事件中获取存储桶名称和文件密钥,并逐行读取。这是我的代码:
import json
import os
import boto3
import csv
def lambda_handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
file_key = record['s3']['object']['key']
s3 = boto3.client('s3')
csvfile = s3.get_object(Bucket=bucket, Key=file_key)
csvcontent = csvfile['Body'].read().split(b'\n')
data = []
with open(csvfile['Body'], 'r') as csv_file:
csv_file = csv.DictReader(csv_file)
data = list(csv_file)
我在 CloudWatch 上遇到的确切错误是:
[ERROR] TypeError: expected str, bytes or os.PathLike object, not list
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 19, in lambda_handler
with open(csvcontent, 'r') as csv_file:
有人可以帮我解决这个问题吗?感谢您提供的任何帮助,因为我是 lambda 新手
【问题讨论】:
-
csvcontent已包含您的数据。无需打开文件。csvcontent实际上是一个可以解析的字符串(行)列表。
标签: python amazon-s3 aws-lambda boto3 aws-serverless