85 lines
2.7 KiB
Python
Executable File
85 lines
2.7 KiB
Python
Executable File
from minio import Minio
|
|
from minio.error import S3Error
|
|
import os
|
|
|
|
def download_minio_files(endpoint, access_key, secret_key, bucket_name, prefix='', local_dir='downloads'):
|
|
"""
|
|
下载MinIO中指定bucket和目录下的所有文件到本地
|
|
|
|
Args:
|
|
endpoint (str): MinIO服务器地址
|
|
access_key (str): MinIO访问密钥
|
|
secret_key (str): MinIO密钥
|
|
bucket_name (str): 要查询的bucket名称
|
|
prefix (str): 要查询的目录前缀
|
|
local_dir (str): 本地存储目录
|
|
"""
|
|
try:
|
|
# 创建MinIO客户端
|
|
client = Minio(
|
|
endpoint,
|
|
access_key=access_key,
|
|
secret_key=secret_key,
|
|
secure=False
|
|
)
|
|
|
|
# 确保本地目录存在
|
|
os.makedirs(local_dir, exist_ok=True)
|
|
|
|
# 列出对象
|
|
objects = client.list_objects(bucket_name, prefix=prefix, recursive=True)
|
|
|
|
print(f"Downloading files from bucket '{bucket_name}' with prefix '{prefix}':")
|
|
print("-" * 50)
|
|
|
|
# 遍历并下载所有对象
|
|
for obj in objects:
|
|
# 构建本地文件路径
|
|
local_path = os.path.join(local_dir, os.path.relpath(obj.object_name, prefix))
|
|
|
|
# 确保目录存在
|
|
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
|
|
|
# 下载文件
|
|
client.fget_object(bucket_name, obj.object_name, local_path)
|
|
|
|
print(f"Downloaded: {obj.object_name} -> {local_path}")
|
|
print(f"Size: {obj.size} bytes")
|
|
print(f"Last Modified: {obj.last_modified}")
|
|
print("-" * 50)
|
|
|
|
except S3Error as err:
|
|
print(f"MinIO Error occurred: {err}")
|
|
except Exception as e:
|
|
print(f"Error occurred: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
# 配置信息
|
|
ENDPOINT = "minio-jndsj.t-aaron.com:2443"
|
|
ACCESS_KEY = "PJM0c2qlauoXv5TMEHm2"
|
|
SECRET_KEY = "Wr69Dm3ZH39M3GCSeyB3eFLynLPuGCKYfphixZuI"
|
|
BUCKET_NAME = "algorithm"
|
|
PREFIX = "weights/024/V1.0/"
|
|
LOCAL_DIR = "downloaded_weights" # 本地存储目录
|
|
|
|
|
|
# endpoint: 'minio-jndsj.t-aaron.com:2443'
|
|
# domain: 'https://minio-jndsj.t-aaron.com:2443'
|
|
# access_key: 'PJM0c2qlauoXv5TMEHm2'
|
|
# secret_key: 'Wr69Dm3ZH39M3GCSeyB3eFLynLPuGCKYfphixZuI'
|
|
# secure: true
|
|
# image_bucket: 'image'
|
|
# video_bucket: 'video'
|
|
|
|
|
|
|
|
|
|
# 调用函数下载文件
|
|
download_minio_files(
|
|
endpoint=ENDPOINT,
|
|
access_key=ACCESS_KEY,
|
|
secret_key=SECRET_KEY,
|
|
bucket_name=BUCKET_NAME,
|
|
prefix=PREFIX,
|
|
local_dir=LOCAL_DIR
|
|
) |