无法从 S3 获取 csv 并使用 Python 进行转换 [英] Fail to get csv from S3 and convert it with Python
本文介绍了无法从 S3 获取 csv 并使用 Python 进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从 s3 存储桶中读取 csv 文件并在 dynamoDB 上插入每一行
I need to read csv file from s3 bucket and insert each row on dynamoDB
def load_users_dynamodb():
s3 = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("test")
obj = s3.get_object(Bucket='test-app-config', Key='extract_Users.csv')
#return obj
data = obj['Body'].read().split('
')
#return json.dumps(data)
with table.batch_writer() as batch:
for row in data:
batch.put_item(Item={
'registration': row.split(',')[0],
'name': row.split(',')[1],
'role': row.split(',')[2],
'company': row.split(',')[3],
'hiredcompany': row.split(',')[4],
'region': row.split(',')[5]
})
return 'OK'
我遇到异常,无法继续:
im getting exception and I can't proceed:
Response:
{
"errorMessage": "a bytes-like object is required, not 'str'",
"errorType": "TypeError",
"stackTrace": [
" File "/var/task/lambda_function.py", line 10, in lambda_handler
'body': load_users_dynamodb()
",
" File "/var/task/lambda_function.py", line 21, in load_users_dynamodb
data = obj['Body'].read().split('\n')
"
]
}
有人可以帮我吗?o/
推荐答案
您的问题与解码从 s3 返回的对象有关.您需要将文件读取为 csv.
Your issue related to decoding the object return from s3.You need to read the file as csv.
看看下面的代码片段:
import boto3
import csv
s3 = boto3.client('s3')
def lambda_handler(event, context):
obj = s3.get_object(Bucket='Bucket_Name', Key='File_Name.csv')
data = obj['Body'].read().decode('utf-8').splitlines()
lines = csv.reader(data)
headers = next(lines)
print('headers: %s' %(headers))
for line in lines:
print(line)
输出:
这篇关于无法从 S3 获取 csv 并使用 Python 进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文