首页 > 解决方案 > 使用 boto3 Python 从 Amazon S3 下载特定文件夹中的所有文件

问题描述

由于我是 python 新手。需要帮助从 S3 存储桶中存在的特定伪文件夹下载所有文件。下面的代码开始下载存储桶中存在的所有文件。我怎样才能实现我的目标。

import os
import errno
import boto3
import botocore


resource = boto3.resource('s3')
bucket = resource.Bucket('my-bucket')
client = boto3.client('s3')

def download_dir():
objList = client.list_objects(Bucket='my-bucket')['Contents']
for obj in objList:
    obj_Key = obj['Key']
    path,_destPath = os.path.split(obj_Key)
    print ("Downloading file :"+ obj_Key);
    client.download_file('my-bucket', obj_Key, _destPath)

download_dir()

提前致谢

标签: python-2.7amazon-s3boto3

解决方案


class Static_file_downloading:
    def __init__(self) -> None:
        self.client = boto3.client("s3")
        self.s3 = boto3.resource("s3")

    def getFiles(self):

        try:
            res = self.client.list_objects(
                Bucket="bucket-name",
                MaxKeys=5
            )
        except:
            print("No bucket found")
    
        result_set = []

        if "Contents" in res:
            for result in res["Contents"]:
                if result["Size"] > 0:
                    result_set.append(result)

        else:
            print("file not found")


        for i in range(len(result_set)):
            file_name = result_set[i]["Key"]
            print(f"downloading {file_name}")
            self.s3.meta.client.download_file(
                "bucket-name",
                file_name,
                path_to_download,
            )

推荐阅读