Download a folder from S3 using Boto3

后端 未结 3 1968
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 23:54

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?

3条回答
  •  臣服心动
    2020-12-31 00:44

    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.

提交回复
热议问题