From a90809c3fc32e496682bbf3d88f105ca9f2afbb7 Mon Sep 17 00:00:00 2001 From: lewis_yan Date: Fri, 3 Apr 2026 17:12:04 +0800 Subject: [PATCH] refactor(storage): rename MinIO to RustFS throughout backend - Rename minio_storage.py to rustfs_storage.py - Update all MINIO_* settings to RUSTFS_* in env.py - Update file_list.py to import rustfs_upload_file - Update test files and factory to use rustfs engine --- backend/conf/env.py | 16 +++--- backend/dvadmin/system/views/file_list.py | 8 +-- .../{minio_storage.py => rustfs_storage.py} | 52 +++++++++---------- backend/tests/factories/file_factory.py | 2 +- backend/tests/test_file_upload.py | 18 +++---- 5 files changed, 48 insertions(+), 48 deletions(-) rename backend/dvadmin/utils/{minio_storage.py => rustfs_storage.py} (61%) diff --git a/backend/conf/env.py b/backend/conf/env.py index 48bd778..841d281 100644 --- a/backend/conf/env.py +++ b/backend/conf/env.py @@ -69,12 +69,12 @@ EMAIL_USE_SSL = False EMAIL_USE_TLS = False # ================================================= # -# *************** MinIO 对象存储 配置 *************** # +# *************** RustFS 对象存储 配置 *************** # # ================================================= # -MINIO_ENDPOINT = "192.168.80.90:9000" -MINIO_ACCESS_KEY = "minioadmin" -MINIO_SECRET_KEY = "minioadmin" -MINIO_BUCKET = "pis-media" -MINIO_SECURE = False # True=HTTPS, False=HTTP -MINIO_PATH_PREFIX = "" # 存储路径前缀,可为空 -MINIO_REGION = "us-east-1" # MinIO 区域(可自定义) +RUSTFS_ENDPOINT = "192.168.80.90:9000" +RUSTFS_ACCESS_KEY = "rustfsadmin" +RUSTFS_SECRET_KEY = "rustfsadmin" +RUSTFS_BUCKET = "pis-media" +RUSTFS_SECURE = False # True=HTTPS, False=HTTP +RUSTFS_PATH_PREFIX = "" # 存储路径前缀,可为空 +RUSTFS_REGION = "us-east-1" # RustFS 区域(可自定义) diff --git a/backend/dvadmin/system/views/file_list.py b/backend/dvadmin/system/views/file_list.py index ebb94d4..8a43495 100644 --- a/backend/dvadmin/system/views/file_list.py +++ b/backend/dvadmin/system/views/file_list.py @@ -44,14 +44,14 @@ class FileSerializer(CustomModelSerializer): for chunk in file.chunks(): md5.update(chunk) validated_data['md5sum'] = md5.hexdigest() - validated_data['engine'] = 'minio' + validated_data['engine'] = 'rustfs' validated_data['mime_type'] = file.content_type ft = {'image':0,'video':1,'audio':2}.get(file.content_type.split('/')[0], None) validated_data['file_type'] = 3 if ft is None else ft - # 上传到 MinIO - from dvadmin.utils.minio_storage import minio_upload_file - file_path = minio_upload_file(file, file_name=validated_data['name']) + # 上传到 RustFS + from dvadmin.utils.rustfs_storage import rustfs_upload_file + file_path = rustfs_upload_file(file, file_name=validated_data['name']) if file_path: validated_data['file_url'] = file_path else: diff --git a/backend/dvadmin/utils/minio_storage.py b/backend/dvadmin/utils/rustfs_storage.py similarity index 61% rename from backend/dvadmin/utils/minio_storage.py rename to backend/dvadmin/utils/rustfs_storage.py index 67d672a..00cce3d 100644 --- a/backend/dvadmin/utils/minio_storage.py +++ b/backend/dvadmin/utils/rustfs_storage.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """ -MinIO (S3-compatible) 文件存储工具 +RustFS (S3-compatible) 文件存储工具 """ import uuid from typing import Optional @@ -11,24 +11,24 @@ from botocore.exceptions import ClientError from django.conf import settings -def get_minio_client(): +def get_rustfs_client(): """ - 创建 MinIO S3 客户端 + 创建 RustFS S3 客户端 """ client = boto3.client( 's3', - endpoint_url=f"{'https' if settings.MINIO_SECURE else 'http'}://{settings.MINIO_ENDPOINT}", - aws_access_key_id=settings.MINIO_ACCESS_KEY, - aws_secret_access_key=settings.MINIO_SECRET_KEY, - region_name=settings.MINIO_REGION or 'us-east-1', + endpoint_url=f"{'https' if settings.RUSTFS_SECURE else 'http'}://{settings.RUSTFS_ENDPOINT}", + aws_access_key_id=settings.RUSTFS_ACCESS_KEY, + aws_secret_access_key=settings.RUSTFS_SECRET_KEY, + region_name=settings.RUSTFS_REGION or 'us-east-1', config=Config(signature_version='s3v4'), ) return client -def minio_upload_file(file_obj, file_name: str) -> Optional[str]: +def rustfs_upload_file(file_obj, file_name: str) -> Optional[str]: """ - 上传文件到 MinIO + 上传文件到 RustFS Args: file_obj: 文件对象 (InMemoryUploadedFile 或 UploadedFile) @@ -43,7 +43,7 @@ def minio_upload_file(file_obj, file_name: str) -> Optional[str]: unique_name = f"{uuid.uuid4().hex}.{ext}" if ext else uuid.uuid4().hex # 路径前缀处理 - path_prefix = getattr(settings, 'MINIO_PATH_PREFIX', '') + path_prefix = getattr(settings, 'RUSTFS_PATH_PREFIX', '') if path_prefix: path_prefix = path_prefix.strip('/') + '/' object_name = f"{path_prefix}{unique_name}" @@ -53,29 +53,29 @@ def minio_upload_file(file_obj, file_name: str) -> Optional[str]: file_content = file_obj.read() # 上传 - client = get_minio_client() + client = get_rustfs_client() client.put_object( - Bucket=settings.MINIO_BUCKET, + Bucket=settings.RUSTFS_BUCKET, Key=object_name, Body=file_content, ContentType=getattr(file_obj, 'content_type', 'application/octet-stream'), ) # 生成访问URL - file_url = minio_get_file_url(object_name) + file_url = rustfs_get_file_url(object_name) return file_url except ClientError as e: - print(f"MinIO upload error: {e}") + print(f"RustFS upload error: {e}") return None except Exception as e: - print(f"MinIO upload unexpected error: {e}") + print(f"RustFS upload unexpected error: {e}") return None -def minio_delete_file(file_url: str) -> bool: +def rustfs_delete_file(file_url: str) -> bool: """ - 从 MinIO 删除文件 + 从 RustFS 删除文件 Args: file_url: 文件完整URL或 object_key @@ -86,23 +86,23 @@ def minio_delete_file(file_url: str) -> bool: try: # 从URL中提取 object_key object_key = file_url - if settings.MINIO_ENDPOINT in file_url: + if settings.RUSTFS_ENDPOINT in file_url: # 提取 /bucket/key 部分 - parts = file_url.split(settings.MINIO_BUCKET + '/') + parts = file_url.split(settings.RUSTFS_BUCKET + '/') if len(parts) > 1: object_key = parts[-1] else: return True # URL格式不对,当作已删除 - client = get_minio_client() - client.delete_object(Bucket=settings.MINIO_BUCKET, Key=object_key) + client = get_rustfs_client() + client.delete_object(Bucket=settings.RUSTFS_BUCKET, Key=object_key) return True except ClientError as e: - print(f"MinIO delete error: {e}") + print(f"RustFS delete error: {e}") return False -def minio_get_file_url(object_key: str) -> str: +def rustfs_get_file_url(object_key: str) -> str: """ 获取文件访问URL @@ -112,7 +112,7 @@ def minio_get_file_url(object_key: str) -> str: Returns: 完整的文件访问URL """ - scheme = 'https' if settings.MINIO_SECURE else 'http' - endpoint = settings.MINIO_ENDPOINT - bucket = settings.MINIO_BUCKET + scheme = 'https' if settings.RUSTFS_SECURE else 'http' + endpoint = settings.RUSTFS_ENDPOINT + bucket = settings.RUSTFS_BUCKET return f"{scheme}://{endpoint}/{bucket}/{object_key}" diff --git a/backend/tests/factories/file_factory.py b/backend/tests/factories/file_factory.py index 1eeb03b..9ca9e55 100644 --- a/backend/tests/factories/file_factory.py +++ b/backend/tests/factories/file_factory.py @@ -7,7 +7,7 @@ class FileListFactory(factory.django.DjangoModelFactory): model = FileList name = factory.Sequence(lambda n: f"test_file_{n}.txt") - engine = "minio" + engine = "rustfs" mime_type = "text/plain" size = "100" md5sum = factory.Faker("md5") diff --git a/backend/tests/test_file_upload.py b/backend/tests/test_file_upload.py index 8a62524..8a334d5 100644 --- a/backend/tests/test_file_upload.py +++ b/backend/tests/test_file_upload.py @@ -1,11 +1,11 @@ """ -MinIO 文件上传集成测试 +RustFS 文件上传集成测试 -依赖: 真实的 MinIO 服务运行在 MINIO_ENDPOINT (192.168.80.90:9000) +依赖: 真实的 RustFS 服务运行在 RUSTFS_ENDPOINT (192.168.80.90:9000) 测试前确保: - 1. MinIO 服务已启动 + 1. RustFS 服务已启动 2. bucket "pis-media" 已创建 - 3. MINIO_ACCESS_KEY / MINIO_SECRET_KEY 正确配置 + 3. RUSTFS_ACCESS_KEY / RUSTFS_SECRET_KEY 正确配置 """ import io import pytest @@ -22,8 +22,8 @@ class TestFileUpload: """文件上传 API 测试""" def test_upload_file_success(self, authenticate): - """POST 上传文件返回成功 + MinIO URL""" - file_content = b"Hello, MinIO!" + """POST 上传文件返回成功 + RustFS URL""" + file_content = b"Hello, RustFS!" file_obj = io.BytesIO(file_content) file_obj.name = "test.txt" @@ -36,8 +36,8 @@ class TestFileUpload: assert response.data["code"] == CODE_SUCCESS, f"上传失败: {response.data}" assert "file_url" in response.data["data"] file_url = response.data["data"]["file_url"] - # 验证 URL 指向 MinIO - assert settings.MINIO_ENDPOINT in file_url, f"文件 URL 非 MinIO 地址: {file_url}" + # 验证 URL 指向 RustFS + assert settings.RUSTFS_ENDPOINT in file_url, f"文件 URL 非 RustFS 地址: {file_url}" assert file_url.startswith("http://") or file_url.startswith("https://") def test_upload_file_unauthenticated(self, api_client): @@ -74,7 +74,7 @@ class TestFileUpload: assert response.data["code"] == CODE_SUCCESS, f"图片上传失败: {response.data}" file_url = response.data["data"]["file_url"] - assert settings.MINIO_ENDPOINT in file_url + assert settings.RUSTFS_ENDPOINT in file_url def test_upload_without_file(self, authenticate): """POST 不带文件返回错误码"""