python-2.7 - 使用 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()
提前致谢
解决方案
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,
)
推荐阅读
- excel - VBA 代码无法复制某些工作表,出现“权限被拒绝”消息
- node.js - 为什么数据没有完全发送到 mongodb 数据库?
- cassandra - 使用 Cassandra 的内部网络应用程序数据模型
- python - AttributeError:“int”对象没有属性“forward”
- python - 如何为 MS Windows PACKAGE_ID 和 PACKAGE_INFO 结构创建 Python ctypes 结构?
- actionscript-3 - AS3 Color.interpolateColor 仅在应用程序版本中给出错误?
- ios - 如何像糖果迷一样振动手机
- asp.net - Asp.net 托管的 Blazor 应用程序在 Script Documents 文件夹中生成了太多的 WASM 文件/对象
- ios - 没有这样的文件或目录:Xcode
- python - 请求生成的令牌时出现问题