/
config.go
47 lines (37 loc) · 1.2 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package config
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"golang.org/x/xerrors"
)
func EndpointResolver(endpoint string) aws.EndpointResolverWithOptionsFunc {
return aws.EndpointResolverWithOptionsFunc(func(_, reg string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: endpoint,
SigningRegion: reg,
Source: aws.EndpointSourceCustom,
}, nil
})
}
func MakeAWSOptions(region, endpoint string) []func(*awsconfig.LoadOptions) error {
var options []func(*awsconfig.LoadOptions) error
if region != "" {
options = append(options, awsconfig.WithRegion(region))
}
if endpoint != "" {
options = append(options, awsconfig.WithEndpointResolverWithOptions(EndpointResolver(endpoint)))
}
return options
}
func LoadDefaultAWSConfig(ctx context.Context, region, endpoint string) (aws.Config, error) {
cfg, err := awsconfig.LoadDefaultConfig(ctx, MakeAWSOptions(region, endpoint)...)
if err != nil {
return aws.Config{}, xerrors.Errorf("aws config load error: %w", err)
}
if cfg.Region == "" {
return aws.Config{}, xerrors.New("aws region is required")
}
return cfg, nil
}