feat: auto-enable path-style addressing when a custom endpoint is set #99
@@ -3,6 +3,7 @@ package storage
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go-v2/aws"
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||||||
@@ -12,6 +13,19 @@ import (
|
|||||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// s3ClientOptions returns the per-client overrides applied to every S3 client
|
||||||
|
// constructed by this package. When the AWS_ENDPOINT_URL_S3 (or
|
||||||
|
// AWS_ENDPOINT_URL) env var is set — typically because the runtime is
|
||||||
|
// pointing at a local MinIO/S3-compatible endpoint — path-style addressing
|
||||||
|
// is enabled so requests look like `http://host:9000/bucket/key` instead of
|
||||||
|
// `http://bucket.host:9000/key`. Production deployments leave those vars
|
||||||
|
// unset and continue talking to real S3 with virtual-hosted style.
|
||||||
|
func s3ClientOptions(o *s3.Options) {
|
||||||
|
if os.Getenv("AWS_ENDPOINT_URL_S3") != "" || os.Getenv("AWS_ENDPOINT_URL") != "" {
|
||||||
|
o.UsePathStyle = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Uploader is the interface for uploading objects to S3 using the transfer manager
|
// Uploader is the interface for uploading objects to S3 using the transfer manager
|
||||||
type Uploader interface {
|
type Uploader interface {
|
||||||
UploadObject(ctx context.Context, input *transfermanager.UploadObjectInput, opts ...func(*transfermanager.Options)) (*transfermanager.UploadObjectOutput, error)
|
UploadObject(ctx context.Context, input *transfermanager.UploadObjectInput, opts ...func(*transfermanager.Options)) (*transfermanager.UploadObjectOutput, error)
|
||||||
@@ -131,7 +145,7 @@ func New(bucket string) (*S3, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
client := s3.NewFromConfig(cfg)
|
client := s3.NewFromConfig(cfg, s3ClientOptions)
|
||||||
uploader := transfermanager.New(client, func(o *transfermanager.Options) {
|
uploader := transfermanager.New(client, func(o *transfermanager.Options) {
|
||||||
o.PartSizeBytes = 5 * 1024 * 1024
|
o.PartSizeBytes = 5 * 1024 * 1024
|
||||||
})
|
})
|
||||||
@@ -147,7 +161,7 @@ func New(bucket string) (*S3, error) {
|
|||||||
// NewS3 creates a new S3 storage instance using direct PutObject
|
// NewS3 creates a new S3 storage instance using direct PutObject
|
||||||
// This is useful when you want more control over the AWS configuration
|
// This is useful when you want more control over the AWS configuration
|
||||||
func NewS3(cfg aws.Config, bucket string) *S3 {
|
func NewS3(cfg aws.Config, bucket string) *S3 {
|
||||||
client := s3.NewFromConfig(cfg)
|
client := s3.NewFromConfig(cfg, s3ClientOptions)
|
||||||
return &S3{
|
return &S3{
|
||||||
bucket: bucket,
|
bucket: bucket,
|
||||||
directSvc: client,
|
directSvc: client,
|
||||||
|
|||||||
+29
@@ -40,6 +40,35 @@ func (m *mockPresigner) PresignGetObject(ctx context.Context, params *s3.GetObje
|
|||||||
return m.presignFunc(ctx, params, optFns...)
|
return m.presignFunc(ctx, params, optFns...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test path-style toggle
|
||||||
|
|
||||||
|
func TestS3ClientOptions_PathStyleTogglesOnCustomEndpoint(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
envVar string
|
||||||
|
value string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{name: "no env var → virtual-hosted", envVar: "", expected: false},
|
||||||
|
{name: "AWS_ENDPOINT_URL_S3 set → path-style", envVar: "AWS_ENDPOINT_URL_S3", value: "http://minio:9000", expected: true},
|
||||||
|
{name: "AWS_ENDPOINT_URL set → path-style", envVar: "AWS_ENDPOINT_URL", value: "http://minio:9000", expected: true},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Setenv("AWS_ENDPOINT_URL", "")
|
||||||
|
t.Setenv("AWS_ENDPOINT_URL_S3", "")
|
||||||
|
if tc.envVar != "" {
|
||||||
|
t.Setenv(tc.envVar, tc.value)
|
||||||
|
}
|
||||||
|
opts := s3.Options{}
|
||||||
|
s3ClientOptions(&opts)
|
||||||
|
if opts.UsePathStyle != tc.expected {
|
||||||
|
t.Fatalf("UsePathStyle = %v, want %v", opts.UsePathStyle, tc.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Test NewS3 constructor
|
// Test NewS3 constructor
|
||||||
|
|
||||||
func TestNewS3(t *testing.T) {
|
func TestNewS3(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user