首页 > 解决方案 > 使用 boto3 在 S3 存储桶中移动对象的最快方法

问题描述

我需要将所有文件从 S3 中的一个前缀复制到同一存储桶中的另一个前缀。我的解决方案是这样的:

file_list = [List of files in first prefix]
for file in file_list:
            copy_source = {'Bucket': my_bucket, 'Key': file}
            s3_client.copy(copy_source, my_bucket, new_prefix)

但是我只移动了 200 个小文件(每个 1 kb),这个过程最多需要 30 秒。一定有可能做得更快吗?

标签: pythonamazon-web-servicesamazon-s3boto3

解决方案


我会并行进行。例如:

from multiprocessing import Pool

file_list = [List of files in first prefix]
    
print(objects_to_download)

def s3_coppier(s3_file):
     copy_source = {'Bucket': my_bucket, 'Key': s3_file}
     s3_client.copy(copy_source, my_bucket, new_prefix)

# copy 5 objects at the same time
with Pool(5) as p:
    p.map(s3_coppier, file_list)

推荐阅读