下面是两种安装方式
# pip 安装
pip install minio
# git 安装
git clone https://github.com/minio/minio.py
cd minio-py
python setup.py
from minio import Minio, InvalidResponseError, S3Error
import re
class Bucket:
def __init__(self, minio_address, minio_admin, minio_password):
self.minioClinet = Minio(
endpoint=minio_address,
access_key=minio_admin,
secret_key=minio_password,
secure=False
)
def create_bucket(self, bucket_name):
try:
if minioClient.bucket_exists(bucket_name=bucket_name):
print("该存储桶已经存在")
else:
minioClient.make_bucket(bucket_name=bucket_name)
print("存储桶创建成功")
except ResponseError as e:
print(e)
# 列出所有的存储桶
def get_list_bucket(self):
return [bucket.name for bucket in self.minioClient.list_buckets()]
# 删除存储桶
def get_remove_bucket(self, bucket_name):
try:
minioClient.remove_bucket(bucket_name=bucket_name)
print(f"删除存储桶 {bucket_name} 成功")
except ResponseError as e:
print(e)
def upload_file_to_bucket(self, bucket_name, object_name, file_path):
# 桶是否存在 不存在则新建
check_bucket = self.minioClient.bucket_exists(bucket_name)
if not check_bucket:
if path_validation:
self.minioClient.make_bucket(bucket_name)
try:
self.minioClient.fput_object(bucket_name=bucket_name,
object_name=object_name,
file_path=file_path)
except FileNotFoundError as e:
print(e)
except S3Error as e:
print(e)
def download_file_from_bucket(self, bucket_name, object_name, file_path):
check_bucket = self.minioClient.bucket_exists(bucket_name)
if check_bucket:
try:
self.minioClient.fget_object(bucket_name=bucket_name,
object_name=object_name,
file_path=file_path)
except FileNotFoundError as e:
print(e)
except S3Error as e:
print(e)
def remove_object(self, bucket_name, object_name):
# 桶是否存在
check_bucket = self.minioClient.bucket_exists(bucket_name)
if check_bucket:
try:
self.minioClient.remove_object(bucket_name=bucket_name,
object_name=object_name)
except FileNotFoundError as e:
print(e)
except S3Error as e:
print(e)
def path_validation(self, object_name):
pathern = r"^a\/(b|c)\/[^\/]+\/[^\/]+\/[^\/]+\/[^\/]+\/[^\/]+$"
if re.match(pattern, object_name):
return True
else:
return False