Skip to content

Commit

Permalink
chore: configure the CustomEndpointResolver as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxin0723 committed Nov 25, 2024
1 parent ecff5c5 commit 139e129
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand Down
66 changes: 29 additions & 37 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 139e129

Please sign in to comment.