-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
51 lines (42 loc) · 1.31 KB
/
main.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
48
49
50
51
package s3
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
var fs afero.Fs
func init() {
fs = afero.NewOsFs()
}
func NewClient(region string) *s3.S3 {
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess := NewSession(region, false)
config := ClientConfig(region, viper.GetString("aws_assume_role_arn"), sess)
svc := s3.New(sess, config)
return svc
}
func NewSession(region string, debug bool) *session.Session {
config := &aws.Config{Region: optionalAWSString(region)}
config = config.WithCredentialsChainVerboseErrors(debug)
return session.Must(session.NewSession(config))
}
func ClientConfig(region, assumeRoleArn string, sess *session.Session) *aws.Config {
config := &aws.Config{Region: optionalAWSString(region)}
if len(assumeRoleArn) > 0 {
log.Printf("AWS Client with Assumed Role: %q", assumeRoleArn)
credentials := stscreds.NewCredentials(sess, assumeRoleArn)
return config.WithCredentials(credentials)
}
return config
}
func optionalAWSString(s string) *string {
if len(s) == 0 {
return nil
}
return aws.String(s)
}