我應該在我的代碼中進行哪些更改,以便我可以將我的整個檔案夾從本地系統上傳到我的 s3 存盤桶中存在的特定檔案夾。
import os
import boto3
s3_resource = boto3.resource("s3", region_name="ap-south-1")
def upload_objects():
try:
bucket_name = "<S3 bucket-name>"
root_path = '<local folder path>'
bucket_folder = '<S3 folder name>'
my_bucket = s3_resource.Bucket(bucket_name)
# s3 = boto3.resource('s3')
for path, subdirs, files in os.walk(root_path):
path = path.replace("\\","/")
directory_name = path.replace(root_path,"")
for file in files:
my_bucket.upload_file(os.path.join(path, file), directory_name '/' file)
except Exception as err:
print(err)
if __name__ == '__main__':
upload_objects()
uj5u.com熱心網友回復:
你根本沒有使用你bucket_folder的。這應該是S3 前綴的開頭,因為在 S3 中沒有檔案夾。它都是關于鍵名和前綴的。
所以它應該是這樣的:
my_bucket.upload_file(os.path.join(path, file), bucket_folder '/' directory_name '/' file)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/430422.html
