本文介绍了使用Boto3从S3下载文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Boto3 Python SDK
,我能够使用 bucket.download_file()
是否可以下载整个文件夹?
解决方案
快速肮脏,但可以正常工作:
import boto3
import os
def downloadDirectoryFroms3 (bucketName,remoteDirectoryName):
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket(bucketName)
for bucket.objects.filter(Prefix = remoteDirectoryName)中的obj:
如果不是os.path.exists(os.path.dirname(obj.key)):
os.makedirs(os.path.dirname(obj.key))
bucket.download_file (obj.key,obj.key)#保存到同一路径
假设您要从以下位置下载目录foo / bar s3,则for循环将迭代路径以Prefix = foo / bar开头的所有文件。
Using Boto3 Python SDK
, I was able to download files using the method bucket.download_file()
Is there a way to download an entire folder?
解决方案
quick and dirty but it works:
import boto3
import os
def downloadDirectoryFroms3(bucketName, remoteDirectoryName):
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket(bucketName)
for obj in bucket.objects.filter(Prefix = remoteDirectoryName):
if not os.path.exists(os.path.dirname(obj.key)):
os.makedirs(os.path.dirname(obj.key))
bucket.download_file(obj.key, obj.key) # save to same path
Assuming you want to download the directory foo/bar from s3 then the for-loop will iterate all the files whose path starts with the Prefix=foo/bar.
这篇关于使用Boto3从S3下载文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!