mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-19 06:01:05 +08:00
## Summary Fixes #14159 — files from different datasets can overwrite each other in Azure Blob storage. ## Problem Both `azure_spn_conn.py` and `azure_sas_conn.py` ignore the `bucket` parameter in all storage operations (`put`, `get`, `rm`, `obj_exist`, `get_presigned_url`). Files are stored flat using only the filename, so two datasets containing a file with the same name will overwrite each other. The MinIO and S3 implementations correctly use the bucket (typically the knowledge base ID) as a path prefix to create logical folder isolation: - MinIO: uses `use_prefix_path` decorator → `{orig_bucket}/{fnm}` - S3: uses `use_prefix_path` decorator → `{prefix_path}/{bucket}/{fnm}` ## Fix Prepend `{bucket}/` to the file path in all 5 operations across both Azure connector files: | File | Methods fixed | |------|---------------| | `azure_spn_conn.py` | `put`, `get`, `rm`, `obj_exist`, `get_presigned_url` | | `azure_sas_conn.py` | `put`, `get`, `rm`, `obj_exist`, `get_presigned_url` | This matches the existing convention where `bucket` is the knowledge base ID used as a directory prefix. ## ⚠️ Migration Note Existing Azure SPN/SAS deployments have files stored without the bucket prefix. After this fix, new files will be stored under `{bucket}/{filename}` while existing files remain at `{filename}`. A one-time migration script or manual file move may be needed for existing deployments. New deployments are unaffected. ## Testing - Verified the fix is consistent across all 5 methods in both files - The `health()` method is intentionally left unchanged as it uses a hardcoded test filename without bucket semantics Co-authored-by: Jin Hai <haijin.chn@gmail.com>
99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
#
|
|
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
|
|
import logging
|
|
import os
|
|
import time
|
|
from io import BytesIO
|
|
from common.decorator import singleton
|
|
from azure.storage.blob import ContainerClient
|
|
from common import settings
|
|
|
|
|
|
@singleton
|
|
class RAGFlowAzureSasBlob:
|
|
def __init__(self):
|
|
self.conn = None
|
|
self.container_url = os.getenv('CONTAINER_URL', settings.AZURE["container_url"])
|
|
self.sas_token = os.getenv('SAS_TOKEN', settings.AZURE["sas_token"])
|
|
self.__open__()
|
|
|
|
def __open__(self):
|
|
try:
|
|
if self.conn:
|
|
self.__close__()
|
|
except Exception:
|
|
pass
|
|
|
|
try:
|
|
self.conn = ContainerClient.from_container_url(self.container_url + "?" + self.sas_token)
|
|
except Exception:
|
|
logging.exception("Fail to connect %s " % self.container_url)
|
|
|
|
def __close__(self):
|
|
del self.conn
|
|
self.conn = None
|
|
|
|
def health(self):
|
|
_bucket, fnm, binary = "txtxtxtxt1", "txtxtxtxt1", b"_t@@@1"
|
|
return self.conn.upload_blob(name=fnm, data=BytesIO(binary), length=len(binary))
|
|
|
|
def put(self, bucket, fnm, binary, tenant_id=None):
|
|
blob_name = f"{bucket}/{fnm}"
|
|
for _ in range(3):
|
|
try:
|
|
return self.conn.upload_blob(name=blob_name, data=BytesIO(binary), length=len(binary))
|
|
except Exception:
|
|
logging.exception(f"Fail put {blob_name}")
|
|
self.__open__()
|
|
time.sleep(1)
|
|
|
|
def rm(self, bucket, fnm):
|
|
try:
|
|
self.conn.delete_blob(f"{bucket}/{fnm}")
|
|
except Exception:
|
|
logging.exception(f"Fail rm {bucket}/{fnm}")
|
|
|
|
def get(self, bucket, fnm):
|
|
blob_name = f"{bucket}/{fnm}"
|
|
for _ in range(1):
|
|
try:
|
|
r = self.conn.download_blob(blob_name)
|
|
return r.read()
|
|
except Exception:
|
|
logging.exception(f"fail get {blob_name}")
|
|
self.__open__()
|
|
time.sleep(1)
|
|
return
|
|
|
|
def obj_exist(self, bucket, fnm):
|
|
try:
|
|
return self.conn.get_blob_client(f"{bucket}/{fnm}").exists()
|
|
except Exception:
|
|
logging.exception(f"Fail put {bucket}/{fnm}")
|
|
return False
|
|
|
|
def get_presigned_url(self, bucket, fnm, expires):
|
|
blob_name = f"{bucket}/{fnm}"
|
|
for _ in range(10):
|
|
try:
|
|
return self.conn.get_presigned_url("GET", bucket, blob_name, expires)
|
|
except Exception:
|
|
logging.exception(f"fail get {blob_name}")
|
|
self.__open__()
|
|
time.sleep(1)
|
|
return
|