将文件夹从本地系统上传到 S3 中的特定文件夹

Uploading folder from local system to a perticular folder in S3

我应该在我的代码中更改什么,以便我可以将我的整个文件夹从本地系统上传到我的 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()

您根本没有使用 bucket_folder。这应该是您 S3 prefix 的开头,因为在 S3 中没有文件夹。都是关于键名和前缀的。

所以应该是这样的:

my_bucket.upload_file(os.path.join(path, file), bucket_folder + '/' + directory_name+'/'+file)