# http://192.168.16.174:9000
# admin admin123!@#
# E:\tool\minio\bin>set MINIO_ROOT_USER=admin
# E:\tool\minio\bin>set MINIO_ROOT_PASSWORD=admin123!@#
# E:\tool\minio\bin>minio.exe server E:\tool\minio\data
# 配置minio 客户端mc
# mc alias set minio_server http://192.168.16.174:9000 admin admin123!@#
# 帮助
# mc --help
# 查看存储桶列表
# mc ls minio_server
# 列出存储桶路径文件列表
# mc ls minio_server/labr/backend/ask/202411
# 创建一个新的存储桶
# mc mb minio_server/test-wf
# 删除存储桶
# mc rb minio_server/test-wf
# mc rb minio_server/test-wf --force
# 上传 下载 删除 文件
我本地文件夹结构
py安装minio
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple minio
python代码
from minio import Minio
from minio.error import S3Error
from datetime import datetime
import uuid
class MinioClientWrapper:
"""封装 MinIO 客户端操作类,提供对 MinIO 存储桶和对象的基本操作"""
def __init__(self, endpoint, access_key, secret_key, secure=False):
"""
初始化 Minio 客户端
:param endpoint: MinIO 服务地址(例如:'192.168.16.174:9000')
:param access_key: MinIO 访问密钥
:param secret_key: MinIO 秘密密钥
:param secure: 是否使用 https(默认为 False)
"""
self.client = Minio(
endpoint, access_key=access_key, secret_key=secret_key, secure=secure
)
def get_bucket_list(self):
"""列出所有存储桶"""
try:
buckets = self.client.list_buckets()
if not buckets:
print("当前没有存储桶。")
for bucket in buckets:
print(f"存储桶名称: {bucket.name}, 创建日期: {bucket.creation_date}")
except S3Error as err:
print(f"MinIO 连接错误: {err}")
def create_bucket(self, bucket_name):
"""创建存储桶"""
try:
if not self.client.bucket_exists(bucket_name):
self.client.make_bucket(bucket_name)
print(f"存储桶 {bucket_name} 创建成功。")
else:
print(f"存储桶 {bucket_name} 已存在。")
except S3Error as err:
print(f"创建存储桶 {bucket_name} 时出错: {err}")
def delete_bucket(self, bucket_name):
"""删除存储桶"""
try:
if self.client.bucket_exists(bucket_name):
self.client.remove_bucket(bucket_name)
print(f"存储桶 {bucket_name} 删除成功。")
else:
print(f"存储桶 {bucket_name} 不存在。")
except S3Error as err:
print(f"删除存储桶 {bucket_name} 时出错: {err}")
def get_bucket_objects(self, bucket_name, prefix=""):
"""列出存储桶中的对象"""
try:
objects = list(
self.client.list_objects(bucket_name, prefix=prefix)
) # 转换生成器为列表
if not objects:
print(f"存储桶 {bucket_name} 中没有符合条件的对象。")
else:
for obj in objects:
print(f"对象名称: {obj.object_name}, 大小: {obj.size} 字节")
except S3Error as err:
print(f"获取存储桶 {bucket_name} 对象列表时出错: {err}")
def put_object(self, bucket_name, object_name, file_path):
"""上传文件到存储桶"""
try:
self.client.fput_object(bucket_name, object_name, file_path)
print(
f"文件 {file_path} 已成功上传至存储桶 {bucket_name},对象名称:{object_name}"
)
except S3Error as err:
print(f"上传文件 {file_path} 到存储桶 {bucket_name} 时出错: {err}")
def get_object(self, bucket_name, object_name, file_path):
"""从存储桶下载文件"""
try:
self.client.fget_object(bucket_name, object_name, file_path)
print(f"文件 {object_name} 已成功下载至 {file_path}")
except S3Error as err:
print(f"下载文件 {object_name} 从存储桶 {bucket_name} 时出错: {err}")
def delete_object(self, bucket_name, object_name):
"""删除存储桶中的对象"""
try:
self.client.remove_object(bucket_name, object_name)
print(f"对象 {object_name} 已成功从存储桶 {bucket_name} 中删除。")
except S3Error as err:
print(f"删除对象 {object_name} 时出错: {err}")
# 以下是如何使用封装后的 MinioClientWrapper 类进行操作
def main():
# 配置 MinIO 客户端参数
endpoint = "192.168.16.174:9000"
access_key = "admin"
secret_key = "admin123!@#"
# 创建 MinIO 客户端实例
minio_client = MinioClientWrapper(endpoint, access_key, secret_key, secure=False)
# 获取存储桶列表
minio_client.get_bucket_list()
# 创建存储桶
bucket_name = "test-bucket"
minio_client.create_bucket(bucket_name)
# 上传文件到存储桶
unique_id = str(uuid.uuid4())
file_name = f"{unique_id}.dat"
ymd = datetime.now().strftime("%Y%m%d")
object_name = f"python/test/{ymd}/{file_name}"
file_path = "mac_hash.dat" # 本地文件路径
minio_client.put_object(bucket_name, object_name, file_path)
# 获取存储桶中的对象列表
minio_client.get_bucket_objects(bucket_name, f"python/test/{ymd}/")
# 下载文件
download_path = "downloaded_file.dat"
minio_client.get_object(bucket_name, object_name, download_path)
# 删除对象
minio_client.delete_object(bucket_name, object_name)
# 删除存储桶
minio_client.delete_bucket(bucket_name)
if __name__ == "__main__":
main()
# 预览图片
# http://192.168.16.174:9000/labr/backend/ask/202411/20241129093312928457.jpg