From c27c6cea136eb39a9c56aa48a8ec80da6f4d0b18 Mon Sep 17 00:00:00 2001 From: Athurg Gooth Date: Sat, 3 Jun 2023 14:38:28 +0800 Subject: [PATCH] fix: failed to upload OSS with S3 SDK (#1792) Fix failed to upload OSS with S3 SDK Co-authored-by: Athurg Feng --- plugin/storage/s3/s3.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plugin/storage/s3/s3.go b/plugin/storage/s3/s3.go index f6992172..028c563e 100644 --- a/plugin/storage/s3/s3.go +++ b/plugin/storage/s3/s3.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "strings" "github.com/aws/aws-sdk-go-v2/aws" s3config "github.com/aws/aws-sdk-go-v2/config" @@ -29,13 +30,18 @@ type Client struct { } func NewClient(ctx context.Context, config *Config) (*Client, error) { + // For some s3-compatible object stores, converting the hostname is not required, + // and not setting this option will result in not being able to access the corresponding object store address. + // But Aliyun OSS should disable this option + hostnameImmutable := true + if strings.HasSuffix(config.EndPoint, "aliyuncs.com") { + hostnameImmutable = false + } resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...any) (aws.Endpoint, error) { return aws.Endpoint{ - URL: config.EndPoint, - SigningRegion: config.Region, - // For some s3-compatible object stores, converting the hostname is not required, - // and not setting this option will result in not being able to access the corresponding object store address. - HostnameImmutable: true, + URL: config.EndPoint, + SigningRegion: config.Region, + HostnameImmutable: hostnameImmutable, }, nil })