更新 Elasticsearch 和 MinIO 配置,重构文件上传逻辑,添加图片链接处理功能
This commit is contained in:
108
minio_api.py
108
minio_api.py
@@ -2,7 +2,7 @@ from minio import Minio
|
||||
from minio.error import S3Error
|
||||
import os
|
||||
from datetime import timedelta
|
||||
MINIO_HOST=os.getenv("MINIO_HOST", "127.0.0.1")
|
||||
MINIO_HOST="127.0.0.1"
|
||||
|
||||
MINIO_CONFIG = {
|
||||
"endpoint": f"{MINIO_HOST}:{os.getenv('MINIO_PORT', '9000')}",
|
||||
@@ -37,35 +37,6 @@ def get_minio_client():
|
||||
# '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}")
|
||||
|
||||
|
||||
|
||||
@@ -81,48 +52,55 @@ def upload_files_to_server(files, parent_id=None, user_id=None):
|
||||
# secure=False # 使用HTTPS(如果是本地测试且未配置SSL,可设置为False)
|
||||
# )
|
||||
|
||||
minio_client= get_minio_client()
|
||||
def upload_file2minio(bucket_name, object_name, file_path):
|
||||
"""上传文件到MinIO
|
||||
# 通过fput_object上传时:
|
||||
|
||||
# 如果object_name为image\image.jpg,则上传后的名字就是image\image.jpg;
|
||||
|
||||
# 要上传的存储桶信息
|
||||
bucket_name = "my-bucket" # 替换为你的存储桶名称
|
||||
object_name = "image/1.jpg" # 文件在MinIO中存储的名称
|
||||
file_path = "G:\\11\\ragflow_api_test\\2.jpg" # 本地文件路径
|
||||
|
||||
|
||||
# 通过fput_object上传时:
|
||||
|
||||
# 如果object_name为image\image.jpg,则上传后的名字就是image\image.jpg;
|
||||
|
||||
# 如果object_name为image/image.jpg,则上传后image为文件夹,文件名为image.jpg;
|
||||
|
||||
|
||||
try:
|
||||
# 检查存储桶是否存在,如果不存在则创建(可选)
|
||||
if not minio_client.bucket_exists(bucket_name):
|
||||
minio_client.make_bucket(bucket_name)
|
||||
print(f"Bucket '{bucket_name}' created")
|
||||
# 如果object_name为image/image.jpg,则上传后image为文件夹,文件名为image.jpg;
|
||||
|
||||
# 上传文件
|
||||
minio_client.fput_object(
|
||||
bucket_name=bucket_name,
|
||||
object_name=object_name,
|
||||
file_path=file_path
|
||||
)
|
||||
"""
|
||||
|
||||
# 获取文件的预签名URL(可选)
|
||||
res = minio_client.get_presigned_url("GET", bucket_name, object_name, expires=timedelta(days=7))
|
||||
minio_client= get_minio_client()
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
# 获取文件的预签名URL(可选)
|
||||
#res = minio_client.get_presigned_url("GET", bucket_name, object_name, expires=timedelta(days=7))
|
||||
|
||||
#res = "http://127.0.0.1:9000" + "/"+bucket_name+"/" + object_name
|
||||
|
||||
|
||||
#print(res)
|
||||
print(f"文件 '{file_path}' 成功上传到存储桶 '{bucket_name}' 为 '{object_name}'")
|
||||
|
||||
except S3Error as exc:
|
||||
print("MinIO错误:", exc)
|
||||
except Exception as e:
|
||||
print("发生错误:", e)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 要上传的存储桶信息
|
||||
bucket_name = "my-bucket" # 替换为你的存储桶名称
|
||||
object_name = "image/1.jpg" # 文件在MinIO中存储的名称
|
||||
file_path = "G:\\11\\ragflow_api_test\\2.jpg" # 本地文件路径
|
||||
upload_file2minio(bucket_name, object_name, file_path)
|
||||
|
||||
#res = "http://127.0.0.1:9000" + "/"+bucket_name+"/" + object_name
|
||||
|
||||
|
||||
print(res)
|
||||
print(f"文件 '{file_path}' 成功上传到存储桶 '{bucket_name}' 为 '{object_name}'")
|
||||
|
||||
except S3Error as exc:
|
||||
print("MinIO错误:", exc)
|
||||
except Exception as e:
|
||||
print("发生错误:", e)
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user