Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(storage): support custom S3 endpoints for self-hosted deployments (MinIO) (#681)
pradeep7127d9be9465c3ae
diff --git a/server/internal/storage/s3.go b/server/internal/storage/s3.goindex 91c23d2502..b032ccac0b 100644--- a/server/internal/storage/s3.go+++ b/server/internal/storage/s3.go@@ -16,9 +16,10 @@ import ()type S3Storage struct {- client *s3.Client- bucket string- cdnDomain string // if set, returned URLs use this instead of bucket name+ client *s3.Client+ bucket string+ cdnDomain string // if set, returned URLs use this instead of bucket name+ endpointURL string // if set, use path-style URLs (e.g. MinIO)}// NewS3StorageFromEnv creates an S3Storage from environment variables.@@ -60,12 +61,31 @@ func NewS3StorageFromEnv() *S3Storage {cdnDomain := os.Getenv("CLOUDFRONT_DOMAIN")- slog.Info("S3 storage initialized", "bucket", bucket, "region", region, "cdn_domain", cdnDomain)+ endpointURL := os.Getenv("AWS_ENDPOINT_URL")+ s3Opts := []func(*s3.Options){}+ if endpointURL != "" {+ s3Opts = append(s3Opts, func(o *s3.Options) {+ o.BaseEndpoint = aws.String(endpointURL)+ o.UsePathStyle = true+ })+ }++ slog.Info("S3 storage initialized", "bucket", bucket, "region", region, "cdn_domain", cdnDomain, "endpoint_url", endpointURL)return &S3Storage{- client: s3.NewFromConfig(cfg),- bucket: bucket,- cdnDomain: cdnDomain,+ client: s3.NewFromConfig(cfg, s3Opts...),+ bucket: bucket,+ cdnDomain: cdnDomain,+ endpointURL: endpointURL,+ }+}++// 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 {+ if s.endpointURL != "" {+ return types.StorageClassStandard}+ return types.StorageClassIntelligentTiering}// sanitizeFilename removes characters that could cause header injection in Content-Disposition.@@ -86,6 +106,13 @@ func sanitizeFilename(name string) string {// KeyFromURL extracts the S3 object key from a CDN or bucket URL.// e.g. "https://multica-static.copilothub.ai/abc123.png" → "abc123.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)+ }+ }+// Strip the "https://domain/" prefix.for _, prefix := range []string{"https://" + s.cdnDomain + "/",@@ -146,12 +173,16 @@ func (s *S3Storage) Upload(ctx context.Context, key string, data []byte, contentContentType: aws.String(contentType),ContentDisposition: aws.String(fmt.Sprintf(`%s; filename="%s"`, disposition, safe)),CacheControl: aws.String("max-age=432000,public"),- StorageClass: types.StorageClassIntelligentTiering,+ StorageClass: s.storageClass(),})if err != nil {return "", fmt.Errorf("s3 PutObject: %w", err)}+ if s.endpointURL != "" {+ link := fmt.Sprintf("%s/%s/%s", strings.TrimRight(s.endpointURL, "/"), s.bucket, key)+ return link, nil+ }domain := s.bucketif s.cdnDomain != "" {domain = s.cdnDomaindiff --git a/server/internal/storage/s3_test.go b/server/internal/storage/s3_test.gonew file mode 100644index 0000000000..ed68bd8ad0--- /dev/null+++ b/server/internal/storage/s3_test.go@@ -0,0 +1,29 @@+package storage++import "testing"++func TestS3StorageKeyFromURL_CustomEndpointPreservesNestedKey(t *testing.T) {+ s := &S3Storage{+ bucket: "test-bucket",+ endpointURL: "http://localhost:9000",+ }++ rawURL := "http://localhost:9000/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 TestS3StorageKeyFromURL_CustomEndpointWithTrailingSlash(t *testing.T) {+ s := &S3Storage{+ bucket: "test-bucket",+ endpointURL: "http://localhost:9000/",+ }++ rawURL := "http://localhost:9000/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")+ }+}