From 62af84eea9e09e3f498fc18101d679a3f3159a25 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Sun, 18 Jan 2026 15:17:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(graph-minio):=20=E4=BF=AE=E5=A4=8D=E6=9C=AA?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=20HOST=5FIP=20=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E5=AF=BC=E8=87=B4=E7=9A=84=E9=94=99=E8=AF=AF=20Fixes:?= =?UTF-8?q?=20Question:=20=E4=B8=8D=E5=85=81=E8=AE=B8=E7=9A=84=E5=A4=96?= =?UTF-8?q?=E9=83=A8=20URL:=20localhost=EF=BC=9A=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E5=9B=BE=E8=B0=B1=E5=8A=9F=E8=83=BD=E4=B8=8A=E4=BC=A0jsonl?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8E=E6=97=A0=E6=B3=95=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=20Fixes=20#479?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../services/upload_graph_service.py | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/knowledge/services/upload_graph_service.py b/src/knowledge/services/upload_graph_service.py index b654e554..f3858470 100644 --- a/src/knowledge/services/upload_graph_service.py +++ b/src/knowledge/services/upload_graph_service.py @@ -1,5 +1,6 @@ import json import os +import tempfile import traceback import warnings from urllib.parse import urlparse @@ -97,12 +98,22 @@ class UploadGraphService: if parsed_url.scheme in ("http", "https"): # 如果是 URL logger.info(f"检测到 URL,正在从 MinIO 下载文件: {file_path}") - # 使用 MinIO 客户端下载到临时文件(使用上下文管理器) - minio_client = get_minio_client() - async with minio_client.temp_file_from_url( - file_path, allowed_extensions=[".jsonl"] - ) as actual_file_path: + # 使用知识库的方式:直接解析 URL 并使用内部 endpoint 下载(避免 HOST_IP 配置问题) + from src.knowledge.utils.kb_utils import parse_minio_url + bucket_name, object_name = parse_minio_url(file_path) + minio_client = get_minio_client() + + # 直接下载文件内容 + file_data = await minio_client.adownload_file(bucket_name, object_name) + logger.info(f"成功从 MinIO 下载文件: {object_name} ({len(file_data)} bytes)") + + # 创建临时文件 + with tempfile.NamedTemporaryFile(mode="wb", suffix=".jsonl", delete=False) as temp_file: + temp_file.write(file_data) + actual_file_path = temp_file.name + + try: def read_triples(file_path): with open(file_path, encoding="utf-8") as file: for line in file: @@ -111,7 +122,10 @@ class UploadGraphService: triples = list(read_triples(actual_file_path)) await self.txt_add_vector_entity(triples, kgdb_name, embed_model_name, batch_size) - # 退出 with 块后,临时文件自动清理 + finally: + # 清理临时文件 + if os.path.exists(actual_file_path): + os.unlink(actual_file_path) else: # 本地文件路径 - 拒绝不安全的本地路径