Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
feat: add configurable S3 path-style addressing (#4739)
Multica Eveda624a883539
diff --git a/.env.example b/.env.exampleindex 90f1d9b9d6..9179d2b390 100644--- a/.env.example+++ b/.env.example@@ -156,6 +156,10 @@ AWS_SECRET_ACCESS_KEY=# ATTACHMENT_DOWNLOAD_MODE=auto or set proxy explicitly so browsers/CLI do# not need direct access to the object store.AWS_ENDPOINT_URL=+# S3_USE_PATH_STYLE — optional S3 addressing mode. Empty preserves the default:+# true when AWS_ENDPOINT_URL is set for MinIO/LocalStack-style endpoints, false+# for AWS S3. Set to false for providers that require virtual-hosted-style URLs.+S3_USE_PATH_STYLE=ATTACHMENT_DOWNLOAD_MODE=autoATTACHMENT_DOWNLOAD_URL_TTL=30mCLOUDFRONT_KEY_PAIR_ID=diff --git a/SELF_HOSTING_ADVANCED.md b/SELF_HOSTING_ADVANCED.mdindex 13c8589375..667385d731 100644--- a/SELF_HOSTING_ADVANCED.md+++ b/SELF_HOSTING_ADVANCED.md@@ -93,7 +93,8 @@ For file uploads and attachments, configure S3 and (optionally) CloudFront:| `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 to path-style URLs |+| `AWS_ENDPOINT_URL` | Custom S3-compatible endpoint (e.g. MinIO, R2, B2). Setting this defaults to path-style URLs for backward compatibility |+| `S3_USE_PATH_STYLE` | Optional S3 addressing mode. Leave empty for the default (`true` when `AWS_ENDPOINT_URL` is set, `false` for AWS S3). Set `false` for S3-compatible providers that require virtual-hosted-style URLs || `ATTACHMENT_DOWNLOAD_MODE` | Attachment download behavior: `auto` (default), `cloudfront`, `presign`, or `proxy`. Use `proxy` for private buckets behind Docker/VPC-only endpoints such as `http://rustfs:9000` || `ATTACHMENT_DOWNLOAD_URL_TTL` | TTL for CloudFront signed URLs and S3 presigned download URLs (default: `30m`) || `CLOUDFRONT_DOMAIN` | CloudFront distribution domain — when set, public URLs use this host instead of the S3 host |diff --git a/docker-compose.selfhost.yml b/docker-compose.selfhost.ymlindex 864d7aae27..8579b6e9eb 100644--- a/docker-compose.selfhost.yml+++ b/docker-compose.selfhost.yml@@ -69,6 +69,7 @@ services:S3_BUCKET: ${S3_BUCKET:-}S3_REGION: ${S3_REGION:-us-west-2}AWS_ENDPOINT_URL: ${AWS_ENDPOINT_URL:-}+ S3_USE_PATH_STYLE: ${S3_USE_PATH_STYLE:-}AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID:-}AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY:-}ATTACHMENT_DOWNLOAD_MODE: ${ATTACHMENT_DOWNLOAD_MODE:-auto}diff --git a/server/internal/storage/s3.go b/server/internal/storage/s3.goindex d98756b8a5..0a481c1da0 100644--- a/server/internal/storage/s3.go+++ b/server/internal/storage/s3.go@@ -6,6 +6,7 @@ import ("fmt""io""log/slog"+ "net/url""os""strings""time"@@ -18,11 +19,12 @@ 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)+ 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 // custom S3-compatible endpoint (e.g. MinIO)+ usePathStyle bool // controls path-style S3 addressing}// NewS3StorageFromEnv creates an S3Storage from environment variables.@@ -32,6 +34,8 @@ type S3Storage struct {// - S3_BUCKET (required)// - S3_REGION (default: us-west-2)// - AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY (optional; falls back to default credential chain)+// - AWS_ENDPOINT_URL (optional S3-compatible endpoint)+// - S3_USE_PATH_STYLE (optional; defaults to true when AWS_ENDPOINT_URL is set)func NewS3StorageFromEnv() *S3Storage {bucket := os.Getenv("S3_BUCKET")if bucket == "" {@@ -71,21 +75,25 @@ func NewS3StorageFromEnv() *S3Storage {cdnDomain := os.Getenv("CLOUDFRONT_DOMAIN")endpointURL := os.Getenv("AWS_ENDPOINT_URL")+ usePathStyle := s3UsePathStyleFromEnv(endpointURL)s3Opts := []func(*s3.Options){}- if endpointURL != "" {+ if endpointURL != "" || usePathStyle {s3Opts = append(s3Opts, func(o *s3.Options) {- o.BaseEndpoint = aws.String(endpointURL)- o.UsePathStyle = true+ if endpointURL != "" {+ o.BaseEndpoint = aws.String(endpointURL)+ }+ o.UsePathStyle = usePathStyle})}- slog.Info("S3 storage initialized", "bucket", bucket, "region", region, "cdn_domain", cdnDomain, "endpoint_url", endpointURL)+ slog.Info("S3 storage initialized", "bucket", bucket, "region", region, "cdn_domain", cdnDomain, "endpoint_url", endpointURL, "use_path_style", usePathStyle)return &S3Storage{- client: s3.NewFromConfig(cfg, s3Opts...),- bucket: bucket,- region: region,- cdnDomain: cdnDomain,- endpointURL: endpointURL,+ client: s3.NewFromConfig(cfg, s3Opts...),+ bucket: bucket,+ region: region,+ cdnDomain: cdnDomain,+ endpointURL: endpointURL,+ usePathStyle: usePathStyle,}}@@ -102,6 +110,31 @@ func looksLikeS3Hostname(bucket string) bool {return strings.Contains(bucket, "amazonaws.com")}+func s3UsePathStyleFromEnv(endpointURL string) bool {+ defaultValue := endpointURL != ""+ raw, ok := os.LookupEnv("S3_USE_PATH_STYLE")+ if !ok || strings.TrimSpace(raw) == "" {+ return defaultValue+ }+ parsed, err := parseBoolEnv(raw)+ if err != nil {+ slog.Warn("invalid S3_USE_PATH_STYLE value, using default", "value", raw, "default", defaultValue)+ return defaultValue+ }+ return parsed+}++func parseBoolEnv(raw string) (bool, error) {+ switch strings.ToLower(strings.TrimSpace(raw)) {+ case "1", "t", "true", "y", "yes", "on":+ return true, nil+ case "0", "f", "false", "n", "no", "off":+ return false, nil+ default:+ return false, fmt.Errorf("invalid bool %q", raw)+ }+}+// 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 {@@ -117,9 +150,13 @@ func (s *S3Storage) storageClass() types.StorageClass {// "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 + "/"- if strings.HasPrefix(rawURL, prefix) {- return strings.TrimPrefix(rawURL, prefix)+ for _, prefix := range []string{+ customEndpointObjectPrefix(s.endpointURL, s.bucket, true),+ customEndpointObjectPrefix(s.endpointURL, s.bucket, false),+ } {+ if strings.HasPrefix(rawURL, prefix) {+ return strings.TrimPrefix(rawURL, prefix)+ }}}@@ -254,10 +291,32 @@ func (s *S3Storage) uploadedURL(key string) string {return fmt.Sprintf("https://%s/%s", s.cdnDomain, key)}if s.endpointURL != "" {- return fmt.Sprintf("%s/%s/%s", strings.TrimRight(s.endpointURL, "/"), s.bucket, key)+ return customEndpointObjectURL(s.endpointURL, s.bucket, key, s.usePathStyle)}- if strings.Contains(s.bucket, ".") {+ if s.usePathStyle || 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)}++func customEndpointObjectURL(endpointURL, bucket, key string, usePathStyle bool) string {+ return customEndpointObjectPrefix(endpointURL, bucket, usePathStyle) + key+}++func customEndpointObjectPrefix(endpointURL, bucket string, usePathStyle bool) string {+ trimmed := strings.TrimRight(endpointURL, "/")+ if usePathStyle {+ return trimmed + "/" + bucket + "/"+ }++ u, err := url.Parse(trimmed)+ if err != nil || u.Scheme == "" || u.Host == "" {+ return trimmed + "/"+ }+ u.Host = bucket + "." + u.Host+ u.Path = strings.TrimRight(u.Path, "/") + "/"+ u.RawPath = ""+ u.RawQuery = ""+ u.Fragment = ""+ return u.String()+}diff --git a/server/internal/storage/s3_test.go b/server/internal/storage/s3_test.goindex dd72e761c4..aee5a21245 100644--- a/server/internal/storage/s3_test.go+++ b/server/internal/storage/s3_test.go@@ -92,6 +92,20 @@ func TestS3StorageKeyFromURL_CustomEndpointWithTrailingSlash(t *testing.T) {}}+func TestS3StorageKeyFromURL_CustomEndpointVirtualHostedStylePreservesNestedKey(t *testing.T) {+ s := &S3Storage{+ bucket: "test-bucket",+ endpointURL: "https://objects.example.com",+ usePathStyle: false,+ }++ rawURL := "https://test-bucket.objects.example.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_VirtualHostedStylePreservesNestedKey(t *testing.T) {s := &S3Storage{bucket: "test-bucket",@@ -155,16 +169,92 @@ func TestLooksLikeS3Hostname(t *testing.T) {}}+func TestS3UsePathStyleFromEnv(t *testing.T) {+ t.Run("defaults to false without custom endpoint", func(t *testing.T) {+ t.Setenv("S3_USE_PATH_STYLE", "")+ if got := s3UsePathStyleFromEnv(""); got {+ t.Fatalf("s3UsePathStyleFromEnv() = %v, want false", got)+ }+ })++ t.Run("defaults to true with custom endpoint", func(t *testing.T) {+ t.Setenv("S3_USE_PATH_STYLE", "")+ if got := s3UsePathStyleFromEnv("https://objects.example.com"); !got {+ t.Fatalf("s3UsePathStyleFromEnv() = %v, want true", got)+ }+ })++ t.Run("can disable path style for custom endpoint", func(t *testing.T) {+ t.Setenv("S3_USE_PATH_STYLE", "false")+ if got := s3UsePathStyleFromEnv("https://objects.example.com"); got {+ t.Fatalf("s3UsePathStyleFromEnv() = %v, want false", got)+ }+ })++ t.Run("invalid value keeps default", func(t *testing.T) {+ t.Setenv("S3_USE_PATH_STYLE", "maybe")+ if got := s3UsePathStyleFromEnv("https://objects.example.com"); !got {+ t.Fatalf("s3UsePathStyleFromEnv() = %v, want true", got)+ }+ })+}++func TestNewS3StorageFromEnv_ConfiguresEndpointPathStyle(t *testing.T) {+ t.Run("defaults custom endpoints to path style", func(t *testing.T) {+ t.Setenv("S3_BUCKET", "test-bucket")+ t.Setenv("S3_REGION", "us-east-1")+ t.Setenv("AWS_ACCESS_KEY_ID", "AKID")+ t.Setenv("AWS_SECRET_ACCESS_KEY", "SECRET")+ t.Setenv("AWS_ENDPOINT_URL", "https://objects.example.com")+ t.Setenv("S3_USE_PATH_STYLE", "")++ store := NewS3StorageFromEnv()+ if store == nil {+ t.Fatal("NewS3StorageFromEnv() = nil")+ }+ if !store.usePathStyle {+ t.Fatalf("usePathStyle = false, want true")+ }+ if !store.client.Options().UsePathStyle {+ t.Fatalf("client UsePathStyle = false, want true")+ }+ })++ t.Run("can disable path style for custom endpoints", func(t *testing.T) {+ t.Setenv("S3_BUCKET", "test-bucket")+ t.Setenv("S3_REGION", "us-east-1")+ t.Setenv("AWS_ACCESS_KEY_ID", "AKID")+ t.Setenv("AWS_SECRET_ACCESS_KEY", "SECRET")+ t.Setenv("AWS_ENDPOINT_URL", "https://objects.example.com")+ t.Setenv("S3_USE_PATH_STYLE", "false")++ store := NewS3StorageFromEnv()+ if store == nil {+ t.Fatal("NewS3StorageFromEnv() = nil")+ }+ if store.usePathStyle {+ t.Fatalf("usePathStyle = true, want false")+ }+ if store.client.Options().UsePathStyle {+ t.Fatalf("client UsePathStyle = true, want false")+ }+ if got, want := store.uploadedURL("uploads/file.txt"), "https://test-bucket.objects.example.com/uploads/file.txt"; got != want {+ t.Fatalf("uploadedURL() = %q, want %q", got, 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 string+ bucket string+ region string+ cdnDomain string+ endpointURL string+ usePathStyle bool+ want string}{{name: "default aws virtual hosted style",@@ -186,36 +276,48 @@ func TestS3StorageUploadedURL(t *testing.T) {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 path style",+ bucket: "test-bucket",+ region: "us-east-1",+ endpointURL: "http://localhost:9000",+ usePathStyle: true,+ want: "http://localhost:9000/test-bucket/uploads/abc/file.png",+ },+ {+ name: "endpoint path style with trailing slash",+ bucket: "test-bucket",+ region: "us-east-1",+ endpointURL: "http://localhost:9000/",+ usePathStyle: true,+ 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 virtual hosted style",+ bucket: "test-bucket",+ region: "us-east-1",+ endpointURL: "https://objects.example.com",+ usePathStyle: false,+ want: "https://test-bucket.objects.example.com/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",+ name: "endpoint and cdn both set prefers cdn",+ bucket: "test-bucket",+ region: "us-east-1",+ cdnDomain: "cdn.example.com",+ endpointURL: "http://localhost:9000",+ usePathStyle: false,+ want: "https://cdn.example.com/uploads/abc/file.png",},}for _, tc := range cases {t.Run(tc.name, func(t *testing.T) {s := &S3Storage{- bucket: tc.bucket,- region: tc.region,- cdnDomain: tc.cdnDomain,- endpointURL: tc.endpointURL,+ bucket: tc.bucket,+ region: tc.region,+ cdnDomain: tc.cdnDomain,+ endpointURL: tc.endpointURL,+ usePathStyle: tc.usePathStyle,}if got := s.uploadedURL(key); got != tc.want {t.Fatalf("uploadedURL() = %q, want %q", got, tc.want)