Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 有没有更快的方法将多个文件从s3下载到本地文件夹?_Amazon Web Services_Amazon S3_Jupyter Notebook_Python 3.6_Boto3 - Fatal编程技术网

Amazon web services 有没有更快的方法将多个文件从s3下载到本地文件夹?

Amazon web services 有没有更快的方法将多个文件从s3下载到本地文件夹?,amazon-web-services,amazon-s3,jupyter-notebook,python-3.6,boto3,Amazon Web Services,Amazon S3,Jupyter Notebook,Python 3.6,Boto3,我正在尝试使用jupyter笔记本从s3 bucket下载12000个文件,预计在21小时内完成下载。这是因为每个文件一次下载一个。我们可以并行进行多次下载吗?这样我就可以加快下载过程了 目前,我正在使用以下代码下载所有文件 ### Get unique full-resolution image basenames images = df['full_resolution_image_basename'].unique() print(f'No. of unique full-resoluti

我正在尝试使用jupyter笔记本从s3 bucket下载12000个文件,预计在21小时内完成下载。这是因为每个文件一次下载一个。我们可以并行进行多次下载吗?这样我就可以加快下载过程了

目前,我正在使用以下代码下载所有文件

### Get unique full-resolution image basenames
images = df['full_resolution_image_basename'].unique()
print(f'No. of unique full-resolution images: {len(images)}')

### Create a folder for full-resolution images
images_dir = './images/'
os.makedirs(images_dir, exist_ok=True)

### Download images
images_str = "','".join(images)
limiting_clause = f"CONTAINS(ARRAY['{images_str}'], 
full_resolution_image_basename)"
_ = download_full_resolution_images(images_dir, 
limiting_clause=limiting_clause)

请参阅下面的代码。这只适用于Python3.6+,因为使用了f-string()。对于较旧版本的python,请使用不同的字符串格式设置方法

提供
相对路径
存储桶名称
s3\u对象键
。此外,是可选的,如果未提供,该数字将是机器处理器数量的5倍

这个答案的大部分代码来自一个答案,该答案的来源来自库中的这个示例

import boto3
import os
from concurrent import futures


relative_path = './images'
bucket_name = 'bucket_name'
s3_object_keys = [] # List of S3 object keys
max_workers = 5

abs_path = os.path.abspath(relative_path)
s3 = boto3.client('s3')

def fetch(key):
    file = f'{abs_path}/{key}'
    os.makedirs(file, exist_ok=True)  
    with open(file, 'wb') as data:
        s3.download_fileobj(bucket_name, key, data)
    return file


def fetch_all(keys):

    with futures.ThreadPoolExecutor(max_workers=5) as executor:
        future_to_key = {executor.submit(fetch, key): key for key in keys}

        print("All URLs submitted.")

        for future in futures.as_completed(future_to_key):

            key = future_to_key[future]
            exception = future.exception()

            if not exception:
                yield key, future.result()
            else:
                yield key, exception


for key, result in fetch_all(S3_OBJECT_KEYS):
    print(f'key: {key}  result: {result}')

你能打电话给操作系统并打电话给管理员吗?它有一个
aws s3 cp
命令,可以并行复制文件。同样有用的还有
aws s3 sync
,它有助于从失败的副本中恢复。
fetch
应根据以下内容创建一个新会话:我同意,但我认为
client
接口的情况并非如此,因此我调整了答案以使用
client
接口,哪个
resource
界面刚刚结束。我尽量避免创建
会话
客户端
、和
资源
,因为这些操作代价高昂。我最终使用了
线程.local()
来缓存会话,这样它就被执行器中的作业重用了。@DamonMaria你能解释一下你在哪里包含了这些吗?@Austin我现在找不到我在哪里使用了这些代码。你是说
会话
本地
包含在哪里?