-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3_client.go
108 lines (100 loc) · 2.42 KB
/
s3_client.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package services
import (
"net/http"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// S3Client makes AWS SDK S3 Client from cli and environment variables
type S3Client struct {
accessKeyID string
secretAccessKey string
endpoint string
region string
noSSL bool
s3 *s3.S3
mux sync.Mutex
err error
cl *http.Client
inited bool
}
const (
awsAccessKeyID = "aws-access-key-id"
awsSecretAccessKey = "aws-secret-access-key"
awsEndpoint = "aws-endpoint"
awsRegion = "aws-region"
awsNoSSL = "aws-no-ssl"
)
// RegisterS3ClientFlags registers cli flags for S3 client
func RegisterS3ClientFlags(f []cli.Flag) []cli.Flag {
return append(f,
cli.StringFlag{
Name: awsAccessKeyID,
Usage: "AWS Access Key ID",
Value: "",
EnvVar: "AWS_ACCESS_KEY_ID",
},
cli.StringFlag{
Name: awsSecretAccessKey,
Usage: "AWS Secret Access Key",
Value: "",
EnvVar: "AWS_SECRET_ACCESS_KEY",
},
cli.StringFlag{
Name: awsEndpoint,
Usage: "AWS Endpoint",
Value: "",
EnvVar: "AWS_ENDPOINT",
},
cli.StringFlag{
Name: awsRegion,
Usage: "AWS Region",
Value: "",
EnvVar: "AWS_REGION",
},
cli.BoolFlag{
Name: awsNoSSL,
EnvVar: "AWS_NO_SSL",
},
)
}
// NewS3Client initializes S3Client
func NewS3Client(c *cli.Context, cl *http.Client) *S3Client {
return &S3Client{
accessKeyID: c.String(awsAccessKeyID),
secretAccessKey: c.String(awsSecretAccessKey),
endpoint: c.String(awsEndpoint),
region: c.String(awsRegion),
noSSL: c.Bool(awsNoSSL),
cl: cl,
}
}
// Get get AWS SDK S3 Client
func (s *S3Client) Get() *s3.S3 {
s.mux.Lock()
defer s.mux.Unlock()
if s.inited {
return s.s3
}
s.s3 = s.get()
s.inited = true
return s.s3
}
func (s *S3Client) get() *s3.S3 {
log.Info("initializing S3")
c := &aws.Config{
Credentials: credentials.NewStaticCredentials(s.accessKeyID, s.secretAccessKey, ""),
Endpoint: aws.String(s.endpoint),
Region: aws.String(s.region),
DisableSSL: aws.Bool(s.noSSL),
S3ForcePathStyle: aws.Bool(true),
HTTPClient: s.cl,
}
ss := session.New(c)
s.s3 = s3.New(ss)
return s.s3
}