在AWS中,我试图使用Lambda函数将一个文件保存到S3中。虽然这在我的本地计算机上工作,但我无法让它在Lambda工作。我一天大部分时间都在研究这个问题,希望能得到帮助。谢谢。
def pdfToTable(PDFfilename, apiKey, fileExt, bucket, key):
# parsing a PDF using an API
fileData = (PDFfilename, open(PDFfilename, "rb"))
files = {"f": fileData}
postUrl = "https://pdftables.com/api?key={0}&format={1}".format(apiKey, fileExt)
response = requests.post(postUrl, files=files)
response.raise_for_status()
# this code is probably the problem!
s3 = boto3.resource('s3')
bucket = s3.Bucket('transportation.manifests.parsed')
with open('/tmp/output2.csv', 'rb') as data:
data.write(response.content)
key = 'csv/' + key
bucket.upload_fileobj(data, key) # FYI, on my own computer, this saves the file
with open('output.csv', "wb") as f:
f.write(response.content)在S3中,有一个存储文件的存储桶transportation.manifests.parsed,其中包含应该保存文件的文件夹csv。
response.content的类型是字节。
在AWS中,当前设置的错误实际上是[Errno 2] No such file or directory: '/tmp/output2.csv': FileNotFoundError.,我的目标是以唯一的名称将文件保存到csv文件夹中,因此tmp/output2.csv可能不是最好的方法。有指引吗?
此外,我尝试使用wb和w,而不是rb,但也没有效果。wb的错误是Input <_io.BufferedWriter name='/tmp/output2.csv'> of type: <class '_io.BufferedWriter'> is not supported. -- 文档建议使用'rb‘是推荐用法,但我不明白为什么会这样。
另外,我尝试过s3_client.put_object(Key=key, Body=response.content, Bucket=bucket),但是接收了An error occurred (404) when calling the HeadObject operation: Not Found。
发布于 2018-03-08 00:37:45
发布于 2018-03-08 00:53:58
您有一个可写流,您要求boto3将其用作一个无法工作的可读流。
编写文件,然后在之后使用bucket.upload_file(),如下所示:
s3 = boto3.resource('s3')
bucket = s3.Bucket('transportation.manifests.parsed')
with open('/tmp/output2.csv', 'w') as data:
data.write(response.content)
key = 'csv/' + key
bucket.upload_file('/tmp/output2.csv', key)https://stackoverflow.com/questions/49163099
复制相似问题