/
aws_sdk.go
38 lines (32 loc) · 1.11 KB
/
aws_sdk.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
package config
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
)
func GetAwsSdkConfig(conf AWSSDKConfig) (*aws.Config, error) {
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if conf.EndpointURL != "" {
return aws.Endpoint{
PartitionID: "aws",
URL: conf.EndpointURL,
SigningRegion: conf.Region,
}, nil
}
// returning EndpointNotFoundError will allow the service to fallback to its default resolution
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
creds := aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(conf.AccessKeyID, string(conf.SecretAccessKey), ""))
creds.Invalidate()
awsCfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(conf.Region),
config.WithCredentialsProvider(creds),
config.WithEndpointResolverWithOptions(customResolver),
)
if err != nil {
return nil, fmt.Errorf("cannot load the AWS configs: %s", err)
}
return &awsCfg, nil
}