fix: R2 public URL without bucket prefix (custom domain serves at root)

S3_PUBLIC_URL (custom domain) serves objects at /<name>, not
/<bucket>/<name>. Only include bucket prefix when falling back to the
S3 endpoint path-style URL.
This commit is contained in:
lezi-fun committed 2026-08-11 09:06:14 +08:00
1 parent 24379f0ab1
commit 778c2bcf4c
1 file changed
+6 -1
+6 -1
View File
@@ -38,8 +38,13 @@ export async function saveObject(name, buffer, contentType) {
headers: { 'Content-Type': contentType || 'application/octet-stream' }, headers: { 'Content-Type': contentType || 'application/octet-stream' },
body: new Uint8Array(buffer), body: new Uint8Array(buffer),
}); });
// 公开域名直连 bucket(对象 key = 文件名,不带 bucket 前缀);
// 无公开域名时回退到 S3 endpoint 路径式(endpoint/bucket/name)
const base = (S3_PUBLIC_URL || S3_ENDPOINT).replace(/\/$/, ''); 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 // 读取对象 → Buffer 或 null