diff --git a/go.mod b/go.mod index 54cb546..1f52d0c 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/aws/aws-sdk-go-v2/credentials v1.17.44 github.com/aws/aws-sdk-go-v2/service/s3 v1.67.0 github.com/aws/aws-sdk-go-v2/service/sts v1.32.4 - github.com/aws/smithy-go v1.22.0 github.com/jinzhu/configor v1.2.1 github.com/qiniu/api.v7/v7 v7.8.2 ) @@ -28,6 +27,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.4 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.24.5 // indirect github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.4 // indirect + github.com/aws/smithy-go v1.22.0 // indirect github.com/gookit/color v1.3.6 // indirect golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect golang.org/x/time v0.3.0 // indirect diff --git a/s3/s3.go b/s3/s3.go index edb7bb5..105cb4b 100644 --- a/s3/s3.go +++ b/s3/s3.go @@ -23,7 +23,6 @@ import ( "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/aws/aws-sdk-go-v2/service/sts" - smithyendpoints "github.com/aws/smithy-go/endpoints" "github.com/qor/oss" ) @@ -35,14 +34,17 @@ type Client struct { // Config S3 client config type Config struct { - AccessID string - AccessKey string - Region string - Bucket string - SessionToken string - ACL types.ObjectCannedACL - Endpoint string - S3Endpoint string + AccessID string + AccessKey string + Region string + Bucket string + SessionToken string + ACL types.ObjectCannedACL + Endpoint string + + S3Endpoint string + CustomEndpointResolver s3.EndpointResolverV2 + S3ForcePathStyle bool CacheControl string @@ -67,10 +69,17 @@ func New(cfg *Config) *Client { } if cfg.S3Endpoint != "" { - s3CfgOptions = append(s3CfgOptions, s3.WithEndpointResolverV2(&endpointResolverV2{ - Url: cfg.S3Endpoint, - })) + s3CfgOptions = append(s3CfgOptions, + func(o *s3.Options) { + o.BaseEndpoint = aws.String(cfg.S3Endpoint) + }, + ) + } + if cfg.CustomEndpointResolver != nil { + s3CfgOptions = append(s3CfgOptions, + s3.WithEndpointResolverV2(cfg.CustomEndpointResolver), + ) } // use role ARN to fetch credentials @@ -272,12 +281,14 @@ func (client Client) GetEndpoint() string { return client.Config.Endpoint } - if client.Config.S3Endpoint != "" { - return client.Config.S3Endpoint - } - - if client.Config.S3ForcePathStyle { - return fmt.Sprintf("s3.%s.amazonaws.com/%s", client.Config.Region, client.Config.Bucket) + if re, err := client.S3.Options().EndpointResolverV2.ResolveEndpoint(context.Background(), s3.EndpointParameters{ + Region: aws.String(client.Config.Region), + }); err == nil { + endpoint := re.URI.String() + for _, prefix := range []string{"https://", "http://"} { + endpoint = strings.TrimPrefix(endpoint, prefix) + } + return client.Config.Bucket + "." + endpoint } return fmt.Sprintf("%s.s3.%s.amazonaws.com", client.Config.Bucket, client.Config.Region) @@ -349,22 +360,3 @@ func (client Client) Copy(from, to string) (err error) { }) return } - -type endpointResolverV2 struct { - Url string -} - -func (r *endpointResolverV2) ResolveEndpoint( - ctx context.Context, params s3.EndpointParameters, -) ( - endpoint smithyendpoints.Endpoint, err error, -) { - - u, err := url.Parse(r.Url) - if err != nil { - return smithyendpoints.Endpoint{}, err - } - return smithyendpoints.Endpoint{ - URI: *u, - }, nil -}