159 lines
5.6 KiB
Python
Raw Normal View History

import time
from minio import Minio
from minio.error import S3Error
import os
from middleware.util import get_current_date_and_milliseconds
client = Minio(
endpoint="222.212.85.86:9000", # MinIO 服务器地址
access_key="adminjdskfj", # 替换为你的 Access Key
secret_key="123456ksldjfal@Y", # 替换为你的 Secret Key
secure=False # 如果未启用 HTTPS 则设为 False
)
bucket_name = "300bdf2b-a150-406e-be63-d28bd29b409f" # 你的桶名称
first_dir='ai_result'
def create_bucket():
'''
访问 MinIO 服务器打印存储桶
'''
try:
buckets = client.list_buckets()
for bucket in buckets:
print(f"Bucket: {bucket.name}, Created: {bucket.creation_date}")
except S3Error as e:
print(f"Error: {e}")
def downFile(object_name):
global bucket_name
print("1")
current_directory = os.path.dirname(os.path.abspath(__file__))
print("2")
download_path = os.path.join(current_directory, os.path.basename(object_name)) # 仅保留文件名
print("3")
try:
print("4")
os.makedirs(os.path.dirname(download_path), exist_ok=True)
print("5")
client.fget_object(bucket_name, object_name, download_path)
print(f"文件已下载到 {download_path}")
return download_path
except S3Error as e:
print(f"MinIO 错误: {e}")
except OSError as e:
print(f"本地文件系统错误: {e}")
return None
def upload_folder(folder_path, bucket_directory):
"""
上传文件夹中的所有文件到 MinIO 指定目录
:param folder_path: 本地文件夹路径
:param bucket_name: MinIO 存储桶名称
:param bucket_directory: MinIO 存储桶内的目标目录可选
"""
# 要下载的桶名和对象名
global bucket_name,first_dir
try:
# 确保存储桶存在
if not client.bucket_exists(bucket_name):
print(f"存储桶 {bucket_name} 不存在")
# 遍历文件夹中的所有文件
for root, _, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
object_name = os.path.relpath(file_path, folder_path).replace("\\", "/") # 适配 Windows/Linux
# 如果指定了桶目录,则添加前缀
if bucket_directory:
object_name = f"{bucket_directory.rstrip('/')}/{first_dir}/{object_name}"
# 上传文件
client.fput_object(bucket_name, object_name, file_path)
print(f"文件 {file_path} 已上传至 {bucket_name}/{object_name}")
except S3Error as e:
print(f"上传文件夹时出错: {e}")
def upload_file(file_path, bucket_directory):
"""
上传文件到 MinIO 指定目录
:param file_path: 本地文件路径
:param bucket_name: MinIO 存储桶名称
:param bucket_directory: MinIO 存储桶内的目标目录可选
"""
# 要下载的桶名和对象名
# bucket_name = "300bdf2b-a150-406e-be63-d28bd29b409f" # 你的桶名称
global bucket_name,first_dir
# dir_name = "ai_result"
try:
# 确保存储桶存在
if not client.bucket_exists(bucket_name):
print(f"存储桶 {bucket_name} 不存在")
# 获取文件名
file_name = os.path.basename(file_path)
formatted_date, milliseconds_timestamp = get_current_date_and_milliseconds()
# 如果指定了桶目录,则添加前缀
if bucket_directory:
object_name = f"{first_dir}/{str(formatted_date)}/{str(milliseconds_timestamp)}-{file_name}"
else:
object_name = f"{first_dir}/{str(formatted_date)}/{str(milliseconds_timestamp)}-{file_name}"
# 上传文件
client.fput_object(bucket_name, object_name, file_path)
print(f"文件 {file_path} 已上传至 {bucket_name}/{object_name}")
return object_name, "pic"
except S3Error as e:
print(f"上传文件时出错: {e}")
# 将内存中的缓存直接上传minio不做本地存储
def upload_file_from_buffer(buffer,file_name, bucket_directory=None):
"""
上传二进制流到 MinIO 指定目录
:param buffer: BytesIO 对象包含要上传的二进制数据
:param bucket_name: MinIO 存储桶名称
:param bucket_directory: MinIO 存储桶内的目标目录可选
"""
# bucket_name = "300bdf2b-a150-406e-be63-d28bd29b409f" # 你的桶名称
# dir_name = "ai_result"
global bucket_name, first_dir
try:
# 确保存储桶存在
if not client.bucket_exists(bucket_name):
print(f"存储桶 {bucket_name} 不存在")
return None
# 获取文件名(如果没有指定目录,则使用默认文件名)
# file_name = "uploaded_file.png" # 默认文件名,可以根据需要修改
# 如果指定了桶目录,则添加前缀
if bucket_directory:
object_name = f"{first_dir}/{bucket_directory.rstrip('/')}/{file_name}"
else:
object_name = f"{first_dir}/{file_name}"
# 上传二进制流
# 注意buffer.getvalue() 返回二进制数据
client.put_object(
bucket_name=bucket_name,
object_name=object_name,
data=buffer,
length=buffer.getbuffer().nbytes,
content_type="image/png" # 根据实际内容类型设置
)
print(f"二进制流已上传至 {bucket_name}/{object_name}")
return object_name, "pic"
except S3Error as e:
print(f"上传二进制流时出错: {e}")
return None