AWS CLI 可以通过多线程并发下载文件来加速下载速度,Boto3只能使用单个线程下载。下面是一个使用 AWS CLI 多线程下载文件的示例代码:
import subprocess
import threading
def download_file(url, destination):
subprocess.call(['aws', 's3', 'cp', url, destination])
def download_files(urls, destination):
threads = []
for url in urls:
thread = threading.Thread(target=download_file, args=(url, destination))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
if __name__ == '__main__':
urls = ['s3://bucket/file1', 's3://bucket/file2', 's3://bucket/file3']
destination = '/path/to/download/directory'
download_files(urls, destination)
在这个示例中,使用了 Python 的 subprocess
模块来调用 AWS CLI 命令,使用了 Python 的 threading
模块来启动多个线程下载文件。 download_files
函数接受一个 URL 列表和一个目标目录作为输入,并调用 download_file
函数来下载每个文件。 download_file
函数使用 AWS CLI 命令下载单个文件。