From 9c4a694394a0079cda5c779238e98fe18d0d0557 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Tue, 11 Aug 2026 09:06:14 +0800 Subject: [PATCH] fix: R2 public URL without bucket prefix (custom domain serves at root) S3_PUBLIC_URL (custom domain) serves objects at /, not //. Only include bucket prefix when falling back to the S3 endpoint path-style URL. --- lib/storage.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/storage.js b/lib/storage.js index 879f982..efdaee2 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -38,8 +38,13 @@ export async function saveObject(name, buffer, contentType) { headers: { 'Content-Type': contentType || 'application/octet-stream' }, body: new Uint8Array(buffer), }); + // 公开域名直连 bucket(对象 key = 文件名,不带 bucket 前缀); + // 无公开域名时回退到 S3 endpoint 路径式(endpoint/bucket/name) const base = (S3_PUBLIC_URL || S3_ENDPOINT).replace(/\/$/, ''); - return { url: `${base}/${S3_BUCKET}/${name}` }; + const url = S3_PUBLIC_URL + ? `${base}/${name}` + : `${base}/${S3_BUCKET}/${name}`; + return { url }; } // 读取对象 → Buffer 或 null