【发布时间】:2021-12-12 06:37:45
【问题描述】:
我正在遍历从 S3 提取的 excel 文件。我想将此数据附加到一个文件中。数据不足以超过 lambda 内存限制,因此我将其保存到变量中,然后将字符串转换为我希望上传到 S3 的 csv 文件。当我在本地运行此代码的变体时,它运行良好,不确定将其转换为 AWS 时出了什么问题。
import csv
import boto3
import urllib3
import tempfile
s3 = boto3.client('s3')
bucket = os.environ['S3_BUCKET']
http = urllib3.PoolManager()
def lambda_handler(event, context):
file = readS3('example.xlsx') # load file with Boto3
latest_scan = openpyxl.load_workbook(io.BytesIO(file), data_only=True)
sh = latest_scan.active
a = []
for row in sh['A']:
r5 = http.request(
'GET',
'https://example.com/api/' + str(row.value),
headers={
'Accept': 'text/csv'
}
)
a.append(r5.data.decode('utf-8'))
s = ''.join(a)
temp = tempfile.TemporaryFile(mode='w+', suffix='.csv')
with open(temp, 'w', encoding="utf-8") as f:
for line in s:
f.write(line)
temp.seek(0)
s3.put_object(temp, Bucket = bucket, Key = 'test.csv')
temp.close()
我明白了:
"errorMessage": "expected str, bytes or os.PathLike object, not _io.TextIOWrapper",
"errorType": "TypeError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line in lambda_handler\n with open(temp,
'w', encoding=\"utf-8\") as f:\n"
]
【问题讨论】:
-
您无需致电
open()。tempfile.TemporaryFile()返回一个打开的文件对象,而不是文件名。
标签: python amazon-s3 aws-lambda