以前,我像這樣將檔案上傳到 Amazon S3 并且作業正常:
bucket.upload_fileobj(
io.BytesIO(gzipped_content),
fileName2,
ExtraArgs={"ContentType": "text/plain"})
在這種情況下,fileName2 被直接放入存盤桶的根檔案夾中。現在我想將 fileName2 放在子檔案夾中。像這樣:bucket/year/month/day/fileName2。我將變數年/月/日保存為字串。我試過這個:
bucket.upload_fileobj(
io.BytesIO(gzipped_content),
year/month/day/fileName2,
ExtraArgs={"ContentType": "text/plain"})
但是,這將引發一個錯誤:unsupported operand type(s) for /: 'str' and 'str'。我也試過這個:
path = year month day fileName2
bucket.upload_fileobj(
io.BytesIO(gzipped_content),
path,
ExtraArgs={"ContentType": "text/plain"})
但在這種情況下,路徑顯示為完整的檔案名。而不是子檔案夾。如何使用變數將檔案上傳到子檔案夾?我不能硬編碼,因為我需要在回圈中運行該函式
uj5u.com熱心網友回復:
您需要將路徑/鍵插入為字串:
bucket.upload_fileobj(
io.BytesIO(gzipped_content),
f"{year}/{month}/{day}/{fileName2}",
ExtraArgs={"ContentType": "text/plain"})
uj5u.com熱心網友回復:
您可以使用許多字串插值功能構造密鑰,包括 Python f-strings:
f"{year}/{month}/{day}/{fileName2}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331392.html
標籤:Python 蟒蛇-3.x 亚马逊网络服务 亚马逊-s3 boto3
上一篇:如果未下載s3檔案,則捕獲錯誤
