From 08a904ef2a9fbf8bb86418d1cd9d419d217b9e0c Mon Sep 17 00:00:00 2001 From: lewis_yan Date: Fri, 3 Apr 2026 16:42:14 +0800 Subject: [PATCH] refactor(storage): remove aliyunoss and tencentcos storage engines --- backend/dvadmin/utils/aliyunoss.py | 62 ----------------------------- backend/dvadmin/utils/tencentcos.py | 56 -------------------------- 2 files changed, 118 deletions(-) delete mode 100644 backend/dvadmin/utils/aliyunoss.py delete mode 100644 backend/dvadmin/utils/tencentcos.py diff --git a/backend/dvadmin/utils/aliyunoss.py b/backend/dvadmin/utils/aliyunoss.py deleted file mode 100644 index b4e2894..0000000 --- a/backend/dvadmin/utils/aliyunoss.py +++ /dev/null @@ -1,62 +0,0 @@ -# -*- coding: utf-8 -*- - -import oss2 -from rest_framework.exceptions import ValidationError - -from application import dispatch - - -# 进度条 -# 当无法确定待上传的数据长度时,total_bytes的值为None。 -def percentage(consumed_bytes, total_bytes): - if total_bytes: - rate = int(100 * (float(consumed_bytes) / float(total_bytes))) - print('\r{0}% '.format(rate), end='') - - -def ali_oss_upload(file, file_name): - """ - 阿里云OSS上传 - """ - try: - file.seek(0) - file_read = file.read() - except Exception as e: - file_read = file - if not file: - raise ValidationError('请上传文件') - # 转存到oss - path_prefix = dispatch.get_system_config_values("file_storage.aliyun_path") - if not path_prefix.endswith('/'): - path_prefix = path_prefix + '/' - if path_prefix.startswith('/'): - path_prefix = path_prefix[1:] - base_fil_name = f'{path_prefix}{file_name}' - # 获取OSS配置 - # 获取的AccessKey - access_key_id = dispatch.get_system_config_values("file_storage.aliyun_access_key") - access_key_secret = dispatch.get_system_config_values("file_storage.aliyun_access_secret") - auth = oss2.Auth(access_key_id, access_key_secret) - # 这个是需要用特定的地址,不同地域的服务器地址不同,不要弄错了 - # 参考官网给的地址配置https://www.alibabacloud.com/help/zh/object-storage-service/latest/regions-and-endpoints#concept-zt4-cvy-5db - endpoint = dispatch.get_system_config_values("file_storage.aliyun_endpoint") - bucket_name = dispatch.get_system_config_values("file_storage.aliyun_bucket") - if bucket_name.endswith(endpoint): - bucket_name = bucket_name.replace(f'.{endpoint}', '') - # 你的项目名称,类似于不同的项目上传的图片前缀url不同 - bucket = oss2.Bucket(auth, endpoint, bucket_name) # 项目名称 - # 生成外网访问的文件路径 - aliyun_cdn_url = dispatch.get_system_config_values("file_storage.aliyun_cdn_url") - if aliyun_cdn_url: - if aliyun_cdn_url.endswith('/'): - aliyun_cdn_url = aliyun_cdn_url[1:] - file_path = f"{aliyun_cdn_url}/{base_fil_name}" - else: - file_path = f"https://{bucket_name}.{endpoint}/{base_fil_name}" - # 这个是阿里提供的SDK方法 - res = bucket.put_object(base_fil_name, file_read, progress_callback=percentage) - # 如果上传状态是200 代表成功 返回文件外网访问路径 - if res.status == 200: - return file_path - else: - return None diff --git a/backend/dvadmin/utils/tencentcos.py b/backend/dvadmin/utils/tencentcos.py deleted file mode 100644 index a515124..0000000 --- a/backend/dvadmin/utils/tencentcos.py +++ /dev/null @@ -1,56 +0,0 @@ -# -*- coding: utf-8 -*- -from rest_framework.exceptions import ValidationError - -from application import dispatch -from qcloud_cos import CosConfig -from qcloud_cos import CosS3Client - - -# 进度条 -# 当无法确定待上传的数据长度时,total_bytes的值为None。 -def percentage(consumed_bytes, total_bytes): - if total_bytes: - rate = int(100 * (float(consumed_bytes) / float(total_bytes))) - print('\r{0}% '.format(rate), end='') - -def tencent_cos_upload(file, file_name): - try: - file.seek(0) - file_read = file.read() - except Exception as e: - file_read = file - if not file: - raise ValidationError('请上传文件') - # 生成文件名 - path_prefix = dispatch.get_system_config_values("file_storage.tencent_path") - if not path_prefix.endswith('/'): - path_prefix = path_prefix + '/' - if path_prefix.startswith('/'): - path_prefix = path_prefix[1:] - base_fil_name = f'{path_prefix}{file_name}' - # 获取cos配置 - # 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 - secret_id = dispatch.get_system_config_values("file_storage.tencent_secret_id") # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 - secret_key = dispatch.get_system_config_values("file_storage.tencent_secret_key") # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 - region = dispatch.get_system_config_values("file_storage.tencent_region") # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket # COS 支持的所有 region 列表参见https://cloud.tencent.com/document/product/436/6224 - bucket = dispatch.get_system_config_values("file_storage.tencent_bucket") # 要访问的桶名称 - config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key) - client = CosS3Client(config) - # 访问地址 - base_file_url = f'https://{bucket}.cos.{region}.myqcloud.com' - # 生成外网访问的文件路径 - if base_file_url.endswith('/'): - file_path = base_file_url + base_fil_name - else: - file_path = f'{base_file_url}/{base_fil_name}' - # 这个是阿里提供的SDK方法 bucket是调用的4.1中配置的变量名 - try: - response = client.put_object( - Bucket=bucket, - Body=file_read, - Key=base_fil_name, - EnableMD5=False - ) - return file_path - except: - return None