Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(storage): build region-qualified S3 public URLs (#2051) (#2065)

Bohan Jiang89b939b07c94
diff --git a/.env.example b/.env.example
index 1662417c41..43737630de 100644
--- a/.env.example
+++ b/.env.example
@@ -63,6 +63,9 @@ GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=http://localhost:3000/auth/callback
# S3 / CloudFront
+# S3_BUCKET — bucket NAME only (e.g. "my-bucket"). Do NOT include the
+# ".s3.<region>.amazonaws.com" suffix; the server builds the public URL
+# from S3_BUCKET + S3_REGION. S3_REGION must match the bucket's real region.
S3_BUCKET=
S3_REGION=us-west-2
CLOUDFRONT_KEY_PAIR_ID=
diff --git a/SELF_HOSTING_ADVANCED.md b/SELF_HOSTING_ADVANCED.md
index 161f200ea6..4f7209ccc3 100644
--- a/SELF_HOSTING_ADVANCED.md
+++ b/SELF_HOSTING_ADVANCED.md
@@ -56,13 +56,15 @@ Changes take effect after restarting the backend / compose stack. The web UI rea
### File Storage (Optional)
-For file uploads and attachments, configure S3 and CloudFront:
+For file uploads and attachments, configure S3 and (optionally) CloudFront:
| Variable | Description |
|----------|-------------|
-| `S3_BUCKET` | S3 bucket name |
-| `S3_REGION` | AWS region (default: `us-west-2`) |
-| `CLOUDFRONT_DOMAIN` | CloudFront distribution domain |
+| `S3_BUCKET` | Bucket name only (e.g. `my-bucket`). Do **not** include the `.s3.<region>.amazonaws.com` suffix — the server constructs the public URL from `S3_BUCKET` + `S3_REGION` |
+| `S3_REGION` | AWS region (default: `us-west-2`). Must match the bucket's actual region — used for both SDK signing and public URLs |
+| `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` | Static credentials. When both are unset, the AWS SDK default credential chain is used |
+| `AWS_ENDPOINT_URL` | Custom S3-compatible endpoint (e.g. MinIO, R2, B2). Setting this switches the public URL to path-style |
+| `CLOUDFRONT_DOMAIN` | CloudFront distribution domain — when set, public URLs use this host instead of the S3 host |
| `CLOUDFRONT_KEY_PAIR_ID` | CloudFront key pair ID for signed URLs |
| `CLOUDFRONT_PRIVATE_KEY` | CloudFront private key (PEM format) |
diff --git a/apps/docs/content/docs/environment-variables.mdx b/apps/docs/content/docs/environment-variables.mdx
index 88b4cb60d4..c5dab262a8 100644
--- a/apps/docs/content/docs/environment-variables.mdx
+++ b/apps/docs/content/docs/environment-variables.mdx
@@ -66,13 +66,19 @@ Multica stores user-uploaded attachments (images and files in comments). **S3 is
| Variable | Default | Description |
|---|---|---|
-| `S3_BUCKET` | empty | Setting this enables S3 storage |
-| `S3_REGION` | `us-west-2` | AWS region |
+| `S3_BUCKET` | empty | **Bucket name only** (for example `my-bucket`). Do **not** include the `.s3.<region>.amazonaws.com` suffix — the server constructs the public host from `S3_BUCKET` + `S3_REGION`. Setting this enables S3 storage |
+| `S3_REGION` | `us-west-2` | AWS region. Must match the bucket's actual region — it is used both for SDK signing and for building the public URL |
| `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` | empty | Static credentials. When both are unset, the AWS SDK default credential chain is used (IAM role / environment credentials) |
| `AWS_ENDPOINT_URL` | empty | Custom S3-compatible endpoint (for example [MinIO](https://min.io/)). Setting this switches to path-style URLs |
**When `S3_BUCKET` is unset**: the server logs `"S3_BUCKET not set, cloud upload disabled"` at startup, and all uploads fall back to local disk.
+**Public URLs** are constructed in this order of priority:
+
+1. `https://<CLOUDFRONT_DOMAIN>/<key>` if `CLOUDFRONT_DOMAIN` is set.
+2. `<AWS_ENDPOINT_URL>/<S3_BUCKET>/<key>` (path-style) if `AWS_ENDPOINT_URL` is set.
+3. `https://<S3_BUCKET>.s3.<S3_REGION>.amazonaws.com/<key>` (virtual-hosted-style). When `S3_BUCKET` contains dots, the server falls back to `https://s3.<S3_REGION>.amazonaws.com/<S3_BUCKET>/<key>` (path-style) because the AWS-issued wildcard TLS certificate does not validate dotted bucket hosts.
+
### Local disk (when S3 is not configured)
| Variable | Default | Description |
diff --git a/apps/docs/content/docs/environment-variables.zh.mdx b/apps/docs/content/docs/environment-variables.zh.mdx
index abde447ed7..44139e1431 100644
--- a/apps/docs/content/docs/environment-variables.zh.mdx
+++ b/apps/docs/content/docs/environment-variables.zh.mdx
@@ -66,13 +66,19 @@ Multica 存储用户上传的附件(评论里的图片、文件等)。**优
| 环境变量 | 默认值 | 说明 |
|---|---|---|
-| `S3_BUCKET` | 空 | 设了就启用 S3 存储 |
-| `S3_REGION` | `us-west-2` | AWS 区域 |
+| `S3_BUCKET` | 空 | **只填 bucket 名**(例如 `my-bucket`),**不要**带 `.s3.<region>.amazonaws.com` 后缀——server 会用 `S3_BUCKET` + `S3_REGION` 自己拼公开 host。设了就启用 S3 存储 |
+| `S3_REGION` | `us-west-2` | AWS 区域。必须和 bucket 所在区域一致——SDK 签名和公开 URL 都用它 |
| `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` | 空 | 静态凭证。全未设时用 AWS SDK 默认凭证链(IAM role / 环境凭证)|
| `AWS_ENDPOINT_URL` | 空 | 自定义 S3 兼容端点(例如 [MinIO](https://min.io/))。设了会切到 path-style URL |
**`S3_BUCKET` 未设时**:server 启动时打 info 日志 `"S3_BUCKET not set, cloud upload disabled"`,所有上传回落到本地磁盘。
+**公开 URL** 按优先级拼装:
+
+1. 设了 `CLOUDFRONT_DOMAIN` → `https://<CLOUDFRONT_DOMAIN>/<key>`
+2. 设了 `AWS_ENDPOINT_URL` → `<AWS_ENDPOINT_URL>/<S3_BUCKET>/<key>`(path-style)
+3. 默认走 AWS S3 → `https://<S3_BUCKET>.s3.<S3_REGION>.amazonaws.com/<key>`(virtual-hosted-style)。bucket 名含点时会回落到 `https://s3.<S3_REGION>.amazonaws.com/<S3_BUCKET>/<key>`(path-style),因为 AWS 通配证书无法覆盖含点 host。
+
### 本地磁盘(S3 未配时)
| 环境变量 | 默认值 | 说明 |
diff --git a/apps/docs/content/docs/getting-started/self-hosting.zh.mdx b/apps/docs/content/docs/getting-started/self-hosting.zh.mdx
index acf33b9962..07d8d08ef5 100644
--- a/apps/docs/content/docs/getting-started/self-hosting.zh.mdx
+++ b/apps/docs/content/docs/getting-started/self-hosting.zh.mdx
@@ -212,13 +212,15 @@ Changes take effect after restarting the backend / compose stack. The web UI rea
### File Storage (Optional)
-For file uploads and attachments, configure S3 and CloudFront:
+For file uploads and attachments, configure S3 and (optionally) CloudFront:
| Variable | Description |
|----------|-------------|
-| `S3_BUCKET` | S3 bucket name |
-| `S3_REGION` | AWS region (default: `us-west-2`) |
-| `CLOUDFRONT_DOMAIN` | CloudFront distribution domain |
+| `S3_BUCKET` | Bucket name only (e.g. `my-bucket`). Do **not** include the `.s3.<region>.amazonaws.com` suffix — the server constructs the public URL from `S3_BUCKET` + `S3_REGION` |
+| `S3_REGION` | AWS region (default: `us-west-2`). Must match the bucket's actual region — used for both SDK signing and public URLs |
+| `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` | Static credentials. When both are unset, the AWS SDK default credential chain is used |
+| `AWS_ENDPOINT_URL` | Custom S3-compatible endpoint (e.g. MinIO, R2, B2). Setting this switches the public URL to path-style |
+| `CLOUDFRONT_DOMAIN` | CloudFront distribution domain — when set, public URLs use this host instead of the S3 host |
| `CLOUDFRONT_KEY_PAIR_ID` | CloudFront key pair ID for signed URLs |
| `CLOUDFRONT_PRIVATE_KEY` | CloudFront private key (PEM format) |
diff --git a/server/internal/storage/s3.go b/server/internal/storage/s3.go
index 824411fce0..b5799ae990 100644
--- a/server/internal/storage/s3.go
+++ b/server/internal/storage/s3.go
@@ -18,6 +18,7 @@ import (
type S3Storage struct {
client *s3.Client
bucket string
+ region string // used to construct virtual-hosted-style public URLs when no CDN/endpoint is set
cdnDomain string // if set, returned URLs use this instead of bucket name
endpointURL string // if set, use path-style URLs (e.g. MinIO)
}
@@ -35,6 +36,12 @@ func NewS3StorageFromEnv() *S3Storage {
slog.Info("S3_BUCKET not set, cloud upload disabled")
return nil
}
+ if looksLikeS3Hostname(bucket) {
+ slog.Warn(
+ "S3_BUCKET looks like a hostname rather than a bucket name — uploads and public URLs will likely both fail. Use only the bucket name (e.g. \"my-bucket\"), not \"<bucket>.s3.<region>.amazonaws.com\".",
+ "value", bucket,
+ )
+ }
region := os.Getenv("S3_REGION")
if region == "" {
@@ -74,6 +81,7 @@ func NewS3StorageFromEnv() *S3Storage {
return &S3Storage{
client: s3.NewFromConfig(cfg, s3Opts...),
bucket: bucket,
+ region: region,
cdnDomain: cdnDomain,
endpointURL: endpointURL,
}
@@ -83,6 +91,15 @@ func (s *S3Storage) CdnDomain() string {
return s.cdnDomain
}
+// looksLikeS3Hostname returns true when the configured S3_BUCKET value looks
+// like an S3 endpoint hostname rather than a bucket name. Real bucket names
+// can never legitimately contain "amazonaws.com", so this is an unambiguous
+// misconfiguration signal — the most common form being users pasting
+// "<bucket>.s3.<region>.amazonaws.com" into S3_BUCKET.
+func looksLikeS3Hostname(bucket string) bool {
+ return strings.Contains(bucket, "amazonaws.com")
+}
+
// storageClass returns the appropriate S3 storage class.
// Custom endpoints (e.g. MinIO) only support STANDARD; real AWS defaults to INTELLIGENT_TIERING.
func (s *S3Storage) storageClass() types.StorageClass {
@@ -94,6 +111,8 @@ func (s *S3Storage) storageClass() types.StorageClass {
// KeyFromURL extracts the S3 object key from a CDN or bucket URL.
// e.g. "https://multica-static.copilothub.ai/abc123.png" → "abc123.png"
+//
+// "https://my-bucket.s3.us-east-1.amazonaws.com/uploads/x/y.png" → "uploads/x/y.png"
func (s *S3Storage) KeyFromURL(rawURL string) string {
if s.endpointURL != "" {
prefix := strings.TrimRight(s.endpointURL, "/") + "/" + s.bucket + "/"
@@ -102,11 +121,26 @@ func (s *S3Storage) KeyFromURL(rawURL string) string {
}
}
- // Strip the "https://domain/" prefix.
- for _, prefix := range []string{
- "https://" + s.cdnDomain + "/",
- "https://" + s.bucket + "/",
- } {
+ // Strip known "https://host/" prefixes. Order matters: the more specific
+ // region-qualified hosts come first so they win over the legacy bucket-only
+ // prefix that we used to write before the suffix bug was fixed.
+ prefixes := make([]string, 0, 5)
+ if s.cdnDomain != "" {
+ prefixes = append(prefixes, "https://"+s.cdnDomain+"/")
+ }
+ if s.region != "" {
+ // virtual-hosted-style: https://<bucket>.s3.<region>.amazonaws.com/<key>
+ prefixes = append(prefixes,
+ "https://"+s.bucket+".s3."+s.region+".amazonaws.com/",
+ // path-style: https://s3.<region>.amazonaws.com/<bucket>/<key>
+ "https://s3."+s.region+".amazonaws.com/"+s.bucket+"/",
+ )
+ }
+ // Legacy / fallback: the buggy "https://<bucket>/<key>" form that older
+ // records may still hold, plus a generic bucket-host prefix.
+ prefixes = append(prefixes, "https://"+s.bucket+"/")
+
+ for _, prefix := range prefixes {
if strings.HasPrefix(rawURL, prefix) {
return strings.TrimPrefix(rawURL, prefix)
}
@@ -161,10 +195,17 @@ func (s *S3Storage) Upload(ctx context.Context, key string, data []byte, content
}
// uploadedURL returns the URL stored for client consumption after an upload.
-// Priority: CDN domain > custom endpoint > bucket. The CDN domain wins even when
-// a custom endpoint is set so S3-compatible backends (MinIO, R2, B2, Wasabi, etc.)
-// can be paired with a separate public-read domain — writes still go through the
-// SDK with the custom endpoint; only the reader-facing URL changes.
+// Priority: CDN domain > custom endpoint > AWS S3 region-qualified host. The CDN
+// domain wins even when a custom endpoint is set so S3-compatible backends
+// (MinIO, R2, B2, Wasabi, etc.) can be paired with a separate public-read
+// domain — writes still go through the SDK with the custom endpoint; only the
+// reader-facing URL changes.
+//
+// For the default AWS S3 case, virtual-hosted-style is preferred:
+// https://<bucket>.s3.<region>.amazonaws.com/<key>. When the bucket name
+// contains dots, the AWS-issued wildcard TLS certificate (`*.s3.amazonaws.com`)
+// fails to validate the host, so we fall back to path-style:
+// https://s3.<region>.amazonaws.com/<bucket>/<key>.
func (s *S3Storage) uploadedURL(key string) string {
if s.cdnDomain != "" {
return fmt.Sprintf("https://%s/%s", s.cdnDomain, key)
@@ -172,5 +213,8 @@ func (s *S3Storage) uploadedURL(key string) string {
if s.endpointURL != "" {
return fmt.Sprintf("%s/%s/%s", strings.TrimRight(s.endpointURL, "/"), s.bucket, key)
}
- return fmt.Sprintf("https://%s/%s", s.bucket, key)
+ if strings.Contains(s.bucket, ".") {
+ return fmt.Sprintf("https://s3.%s.amazonaws.com/%s/%s", s.region, s.bucket, key)
+ }
+ return fmt.Sprintf("https://%s.s3.%s.amazonaws.com/%s", s.bucket, s.region, key)
}
diff --git a/server/internal/storage/s3_test.go b/server/internal/storage/s3_test.go
index f3e9840ccc..a4464b0127 100644
--- a/server/internal/storage/s3_test.go
+++ b/server/internal/storage/s3_test.go
@@ -28,42 +28,117 @@ func TestS3StorageKeyFromURL_CustomEndpointWithTrailingSlash(t *testing.T) {
}
}
+func TestS3StorageKeyFromURL_VirtualHostedStylePreservesNestedKey(t *testing.T) {
+ s := &S3Storage{
+ bucket: "test-bucket",
+ region: "us-east-1",
+ }
+
+ rawURL := "https://test-bucket.s3.us-east-1.amazonaws.com/uploads/abc/file.png"
+
+ if got := s.KeyFromURL(rawURL); got != "uploads/abc/file.png" {
+ t.Fatalf("KeyFromURL(%q) = %q, want %q", rawURL, got, "uploads/abc/file.png")
+ }
+}
+
+func TestS3StorageKeyFromURL_PathStylePreservesNestedKey(t *testing.T) {
+ s := &S3Storage{
+ bucket: "bucket.with.dots",
+ region: "us-east-1",
+ }
+
+ rawURL := "https://s3.us-east-1.amazonaws.com/bucket.with.dots/uploads/abc/file.png"
+
+ if got := s.KeyFromURL(rawURL); got != "uploads/abc/file.png" {
+ t.Fatalf("KeyFromURL(%q) = %q, want %q", rawURL, got, "uploads/abc/file.png")
+ }
+}
+
+func TestS3StorageKeyFromURL_LegacyBucketOnlyHostStillRoundTrips(t *testing.T) {
+ // Old records written before the suffix bug was fixed look like
+ // "https://<bucket>/<key>". They were broken at fetch time but were still
+ // stored, so KeyFromURL must continue to recognise that prefix when we
+ // migrate or delete those records.
+ s := &S3Storage{
+ bucket: "test-bucket",
+ region: "us-east-1",
+ }
+
+ rawURL := "https://test-bucket/uploads/abc/file.png"
+
+ if got := s.KeyFromURL(rawURL); got != "uploads/abc/file.png" {
+ t.Fatalf("KeyFromURL(%q) = %q, want %q", rawURL, got, "uploads/abc/file.png")
+ }
+}
+
+func TestLooksLikeS3Hostname(t *testing.T) {
+ cases := []struct {
+ bucket string
+ want bool
+ }{
+ {"my-bucket", false},
+ {"bucket.with.dots", false},
+ {"my-bucket.s3.us-east-1.amazonaws.com", true},
+ {"my-bucket.s3.amazonaws.com", true},
+ {"s3.us-east-1.amazonaws.com", true},
+ }
+ for _, tc := range cases {
+ t.Run(tc.bucket, func(t *testing.T) {
+ if got := looksLikeS3Hostname(tc.bucket); got != tc.want {
+ t.Fatalf("looksLikeS3Hostname(%q) = %v, want %v", tc.bucket, got, tc.want)
+ }
+ })
+ }
+}
+
func TestS3StorageUploadedURL(t *testing.T) {
const key = "uploads/abc/file.png"
cases := []struct {
name string
bucket string
+ region string
cdnDomain string
endpointURL string
want string
}{
{
- name: "bucket only",
+ name: "default aws virtual hosted style",
bucket: "test-bucket",
- want: "https://test-bucket/uploads/abc/file.png",
+ region: "us-east-1",
+ want: "https://test-bucket.s3.us-east-1.amazonaws.com/uploads/abc/file.png",
+ },
+ {
+ name: "default aws path style when bucket contains dots",
+ bucket: "bucket.with.dots",
+ region: "us-east-1",
+ want: "https://s3.us-east-1.amazonaws.com/bucket.with.dots/uploads/abc/file.png",
},
{
name: "cdn only",
bucket: "test-bucket",
+ region: "us-east-1",
cdnDomain: "cdn.example.com",
want: "https://cdn.example.com/uploads/abc/file.png",
},
{
name: "endpoint only",
bucket: "test-bucket",
+ region: "us-east-1",
endpointURL: "http://localhost:9000",
want: "http://localhost:9000/test-bucket/uploads/abc/file.png",
},
{
name: "endpoint with trailing slash",
bucket: "test-bucket",
+ region: "us-east-1",
endpointURL: "http://localhost:9000/",
want: "http://localhost:9000/test-bucket/uploads/abc/file.png",
},
{
name: "endpoint and cdn both set prefers cdn",
bucket: "test-bucket",
+ region: "us-east-1",
cdnDomain: "cdn.example.com",
endpointURL: "http://localhost:9000",
want: "https://cdn.example.com/uploads/abc/file.png",
@@ -74,6 +149,7 @@ func TestS3StorageUploadedURL(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
s := &S3Storage{
bucket: tc.bucket,
+ region: tc.region,
cdnDomain: tc.cdnDomain,
endpointURL: tc.endpointURL,
}