Dark模式 oss存储
Deploy dev / deploy (push) Successful in 1m16s

This commit is contained in:
Azmat@qq.com
2026-09-24 18:06:00 +08:00
parent ceb5163881
commit 5dbb240b59
46 changed files with 3491 additions and 350 deletions
+2 -3
View File
@@ -45,9 +45,8 @@ class AssetFileSerializer(serializers.ModelSerializer):
]
def get_preview_url(self, obj):
# 存储字段优先(如外部已写入绝对 URL);否则用 object_key 拼 TOS 公读直链。
# TOS 桶公读,直链(虚拟主机式)免签名、且稳定可缓存——不再逐图签发预签名 URL
# (签名是纯本地计算但每次都变、1h 过期会打不到浏览器/CDN 缓存;直链一劳永逸)。
# 存储字段优先(如外部已写入绝对 URL);否则用 object_key 拼公读直链。
# 桶公读,直链不过期、可缓存。不再签发 1 小时就失效的预签名 URL
if obj.preview_url:
return obj.preview_url
if not obj.object_key or not settings.TOS.get("endpoint"):
+24 -5
View File
@@ -19,13 +19,25 @@ class TosStorage:
def __init__(self) -> None:
tos = settings.TOS
self.bucket = tos["bucket"]
config_kwargs = {
"signature_version": "s3v4",
"s3": {"addressing_style": "virtual"},
}
try:
client_config = Config(
**config_kwargs,
request_checksum_calculation="when_required",
response_checksum_validation="when_required",
)
except TypeError:
client_config = Config(**config_kwargs)
self.client = boto3.client(
"s3",
endpoint_url=tos["endpoint"],
aws_access_key_id=tos["access_key_id"],
aws_secret_access_key=tos["secret_access_key"],
region_name="cn-shanghai",
config=Config(s3={"addressing_style": "virtual"}),
region_name=tos.get("region") or "cn-shanghai",
config=client_config,
)
def upload_fileobj(self, *, fileobj: BinaryIO, object_key: str, content_type: str) -> StoredObject:
@@ -57,6 +69,13 @@ class TosStorage:
def public_url(self, *, object_key: str, bucket: str | None = None) -> str:
"""桶公读时的虚拟主机式直链:https://{bucket}.{host}/{key}
相比预签名 URL:不签名(省 boto3 客户端/签名开销)、且 URL 稳定可被浏览器/CDN 缓存
(签名链每次都变、1h 过期,反而打不到缓存)。仅用于公开可读的预览图。"""
host = urlparse(settings.TOS["endpoint"]).netloc
return f"https://{bucket or self.bucket}.{host}/{quote(object_key, safe='/')}"
(签名链每次都变、1h 过期,反而打不到缓存)。仅用于公开可读的预览图。
历史文件的 bucket 仍是火山引擎旧桶,必须用旧域名,不能拼到当前阿里云地址上。"""
bucket_name = bucket or self.bucket
endpoint = settings.TOS.get("endpoint") or ""
legacy_bucket = settings.TOS.get("legacy_bucket") or ""
legacy_endpoint = settings.TOS.get("legacy_endpoint") or ""
if legacy_bucket and legacy_endpoint and bucket_name == legacy_bucket:
endpoint = legacy_endpoint
host = urlparse(endpoint).netloc
return f"https://{bucket_name}.{host}/{quote(object_key, safe='/')}"