From d8d34af554d68511bd5bf2f6569c8f9554d480ed Mon Sep 17 00:00:00 2001 From: glowz <24627181@qq.com> Date: Mon, 7 Jul 2025 22:22:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0MinIO=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E9=85=8D=E7=BD=AE=E5=92=8C=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- minio_api.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 minio_api.py diff --git a/minio_api.py b/minio_api.py new file mode 100644 index 0000000..2e71545 --- /dev/null +++ b/minio_api.py @@ -0,0 +1,108 @@ +from minio import Minio +from minio.error import S3Error +import os +MINIO_HOST=os.getenv("MINIO_HOST", "127.0.0.1") + +MINIO_CONFIG = { + "endpoint": f"{MINIO_HOST}:{os.getenv('MINIO_PORT', '9000')}", + "access_key": os.getenv("MINIO_USER", "rag_flow"), + "secret_key": os.getenv("MINIO_PASSWORD", "infini_rag_flow"), + "secure": False +} + +def get_minio_client(): + """创建MinIO客户端""" + return Minio( + endpoint=MINIO_CONFIG["endpoint"], + access_key=MINIO_CONFIG["access_key"], + secret_key=MINIO_CONFIG["secret_key"], + secure=MINIO_CONFIG["secure"] + ) +# def upload_file(): +# if 'files' not in request.files: +# return jsonify({ +# 'code': 400, +# 'message': '未选择文件', +# 'data': None +# }), 400 + +# files = request.files.getlist('files') +# upload_result = upload_files_to_server(files) + +# # 返回标准格式 +# return jsonify({ +# 'code': 0, +# 'message': '上传成功', +# 'data': upload_result['data'] +# }) + +def upload_files_to_server(files, parent_id=None, user_id=None): + """ + 上传文件到MinIO服务器 + :param files: 文件路径列表 + :param parent_id: 父级ID(可选) + :param user_id: 用户ID(可选) + """ + for file_path in files: + if not os.path.isfile(file_path): + print(f"文件不存在: {file_path}") + continue + + # 获取文件名 + file_name = os.path.basename(file_path) + + # 上传文件到MinIO + try: + with open(file_path, 'rb') as file_data: + minio_client.put_object ( + bucket_name=parent_id, + object_name=file_name, + data=file_data, + length=os.path.getsize(file_path) + ) + print(f"文件已上传到MinIO: {parent_id}/{file_name}") + except S3Error as e: + print(f"上传文件 '{file_name}' 失败: {e}") + except Exception as e: + print(f"发生错误: {e}") + + + + + + + +# 配置MinIO客户端 +# minio_client = Minio( +# endpoint="192.168.0.250:9000", # 替换为你的MinIO服务器地址,例如"localhost:9000" +# access_key="jAGDJmMJ63k3MyRMmi4N", # 替换为你的Access Key +# secret_key="gH2ZOiVh9pJk8bLI8jBz6d6ABNiaTrCuFiDLWojK", # 替换为你的Secret Key +# secure=False # 使用HTTPS(如果是本地测试且未配置SSL,可设置为False) +# ) + +minio_client= get_minio_client() + + +# 要上传的存储桶信息 +bucket_name = "my-bucket" # 替换为你的存储桶名称 +object_name = "my-file.txt" # 文件在MinIO中存储的名称 +file_path = "./ragflow.txt" # 本地文件路径 + +try: + # 检查存储桶是否存在,如果不存在则创建(可选) + if not minio_client.bucket_exists(bucket_name): + minio_client.make_bucket(bucket_name) + print(f"Bucket '{bucket_name}' created") + + # 上传文件 + minio_client.fput_object( + bucket_name=bucket_name, + object_name=object_name, + file_path=file_path + ) + print(f"文件 '{file_path}' 成功上传到存储桶 '{bucket_name}' 为 '{object_name}'") + +except S3Error as exc: + print("MinIO错误:", exc) +except Exception as e: + print("发生错误:", e) \ No newline at end of file