[英]Creating a new file, writing to it and uploading to S3 using Python in Lambda
[英]Writing a file to S3 using Lambda in Python with AWS
在AWS中,我正在嘗試使用Lambda函數將文件保存到Python中的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中,有一個bucket 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
收到An error occurred (404) when calling the HeadObject operation: Not Found
。
你有一個可寫的流,你要求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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.