forked from argoproj/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
253 lines (227 loc) · 7.81 KB
/
s3.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package s3
import (
"os"
"path"
"path/filepath"
"strings"
"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/minio/minio-go"
"github.com/minio/minio-go/pkg/credentials"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const nullIAMEndpoint = ""
type S3Client interface {
// PutFile puts a single file to a bucket at the specified key
PutFile(bucket, key, path string) error
// PutDirectory puts a complete directory into a bucket key prefix, with each file in the directory
// a separate key in the bucket.
PutDirectory(bucket, key, path string) error
// GetFile downloads a file to a local file path
GetFile(bucket, key, path string) error
// GetDirectory downloads a directory to a local file path
GetDirectory(bucket, key, path string) error
// IsDirectory tests if the key is acting like a s3 directory
IsDirectory(bucket, key string) (bool, error)
}
type S3ClientOpts struct {
Endpoint string
Region string
Secure bool
AccessKey string
SecretKey string
Trace bool
RoleARN string
RoleSessionName string
UseSDKCreds bool
}
type s3client struct {
S3ClientOpts
minioClient *minio.Client
}
// Get AWS credentials based on default order from aws SDK
func GetAWSCredentials(opts S3ClientOpts) (*credentials.Credentials, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(opts.Region)},
SharedConfigState: session.SharedConfigEnable,
}))
value, err := sess.Config.Credentials.Get()
if err != nil {
return nil, err
}
return credentials.NewStaticV4(value.AccessKeyID, value.SecretAccessKey, value.SessionToken), nil
}
// GetAssumeRoleCredentials gets Assumed role credentials
func GetAssumeRoleCredentials(opts S3ClientOpts) (*credentials.Credentials, error) {
sess := session.Must(session.NewSession())
// Create the credentials from AssumeRoleProvider to assume the role
// referenced by the "myRoleARN" ARN. Prompt for MFA token from stdin.
creds := stscreds.NewCredentials(sess, opts.RoleARN)
value, err := creds.Get()
if err != nil {
return nil, err
}
return credentials.NewStaticV4(value.AccessKeyID, value.SecretAccessKey, value.SessionToken), nil
}
// NewS3Client instantiates a new S3 client object backed
func NewS3Client(opts S3ClientOpts) (S3Client, error) {
s3cli := s3client{
S3ClientOpts: opts,
}
s3cli.AccessKey = strings.TrimSpace(s3cli.AccessKey)
s3cli.SecretKey = strings.TrimSpace(s3cli.SecretKey)
var minioClient *minio.Client
var err error
if s3cli.RoleARN != "" {
log.Infof("Creating minio client %s using assumed-role credentials", s3cli.RoleARN)
cred, err := GetAssumeRoleCredentials(opts)
if err != nil {
return nil, errors.WithStack(err)
}
minioClient, err = minio.NewWithCredentials(s3cli.Endpoint, cred, s3cli.Secure, s3cli.Region)
if err != nil {
return nil, errors.WithStack(err)
}
} else if s3cli.AccessKey != "" {
log.Infof("Creating igz-overridden minio client %s using static credentials with V2 signature", s3cli.Endpoint)
if s3cli.Region != "" {
minioClient, err = minio.NewWithRegion(
s3cli.Endpoint, s3cli.AccessKey, s3cli.SecretKey, s3cli.Secure, s3cli.Region)
} else {
minioClient, err = minio.NewV2(s3cli.Endpoint, s3cli.AccessKey, s3cli.SecretKey, s3cli.Secure)
}
} else if s3cli.UseSDKCreds == true {
log.Infof("Creating minio client using GetAWSCredentials credentials")
cred, err := GetAWSCredentials(opts)
if err != nil {
return nil, errors.WithStack(err)
}
minioClient, err = minio.NewWithCredentials(s3cli.Endpoint, cred, s3cli.Secure, s3cli.Region)
if err != nil {
return nil, errors.WithStack(err)
}
} else {
log.Infof("Creating igz-overridden minio client with V2 signature", s3cli.Endpoint)
minioClient, err = minio.NewV2(s3cli.Endpoint, s3cli.AccessKey, s3cli.SecretKey, s3cli.Secure)
}
if err != nil {
return nil, errors.WithStack(err)
}
if opts.Trace {
minioClient.TraceOn(log.StandardLogger().Out)
}
s3cli.minioClient = minioClient
return &s3cli, nil
}
// PutFile puts a single file to a bucket at the specified key
func (s *s3client) PutFile(bucket, key, path string) error {
log.Infof("Saving from %s to s3 (endpoint: %s, bucket: %s, key: %s)", path, s.Endpoint, bucket, key)
// NOTE: minio will detect proper mime-type based on file extension
_, err := s.minioClient.FPutObject(bucket, key, path, minio.PutObjectOptions{})
if err != nil {
return errors.WithStack(err)
}
return nil
}
type uploadTask struct {
key string
path string
}
func generatePutTasks(keyPrefix, rootPath string) chan uploadTask {
rootPath = filepath.Clean(rootPath) + "/"
uploadTasks := make(chan uploadTask)
visit := func(localPath string, fi os.FileInfo, err error) error {
relPath := strings.TrimPrefix(localPath, rootPath)
if fi.IsDir() {
return nil
}
if fi.Mode()&os.ModeSymlink != 0 {
return nil
}
t := uploadTask{
key: path.Join(keyPrefix, relPath),
path: localPath,
}
uploadTasks <- t
return nil
}
go func() {
_ = filepath.Walk(rootPath, visit)
close(uploadTasks)
}()
return uploadTasks
}
// PutDirectory puts a complete directory into a bucket key prefix, with each file in the directory
// a separate key in the bucket.
func (s *s3client) PutDirectory(bucket, key, path string) error {
for putTask := range generatePutTasks(key, path) {
err := s.PutFile(bucket, putTask.key, putTask.path)
if err != nil {
return err
}
}
return nil
}
// GetFile downloads a file to a local file path
func (s *s3client) GetFile(bucket, key, path string) error {
log.Infof("Getting from s3 (endpoint: %s, bucket: %s, key: %s) to %s", s.Endpoint, bucket, key, path)
err := s.minioClient.FGetObject(bucket, key, path, minio.GetObjectOptions{})
if err != nil {
return errors.WithStack(err)
}
return nil
}
// GetDirectory downloads a s3 directory to a local path
func (s *s3client) GetDirectory(bucket, keyPrefix, path string) error {
log.Infof("Getting directory from s3 (endpoint: %s, bucket: %s, key: %s) to %s", s.Endpoint, bucket, keyPrefix, path)
keyPrefix = filepath.Clean(keyPrefix) + "/"
doneCh := make(chan struct{})
defer close(doneCh)
objCh := s.minioClient.ListObjects(bucket, keyPrefix, true, doneCh)
for obj := range objCh {
if obj.Err != nil {
return errors.WithStack(obj.Err)
}
if strings.HasSuffix(obj.Key, "/") {
// When a dir is created through AWS S3 console, a nameless obj will be created
// automatically, its key will be {dir_name} + "/". This obj does not display in the
// console, but you can see it when using aws cli.
// If obj.Key ends with "/" means it's a dir obj, we need to skip it, otherwise it
// will be downloaded as a regular file with the same name as the dir, and it will
// creates error when downloading the files under the dir.
continue
}
relKeyPath := strings.TrimPrefix(obj.Key, keyPrefix)
localPath := filepath.Join(path, relKeyPath)
err := s.minioClient.FGetObject(bucket, obj.Key, localPath, minio.GetObjectOptions{})
if err != nil {
return errors.WithStack(err)
}
}
return nil
}
// IsDirectory tests if the key is acting like a s3 directory. This just means it has at least one
// object which is prefixed with the given key
func (s *s3client) IsDirectory(bucket, key string) (bool, error) {
doneCh := make(chan struct{})
defer close(doneCh)
objCh := s.minioClient.ListObjects(bucket, key, false, doneCh)
for obj := range objCh {
if obj.Err != nil {
return false, errors.WithStack(obj.Err)
} else {
return true, nil
}
}
return false, nil
}
// IsS3ErrCode returns if the supplied error is of a specific S3 error code
func IsS3ErrCode(err error, code string) bool {
err = errors.Cause(err)
if minioErr, ok := err.(minio.ErrorResponse); ok {
return minioErr.Code == code
}
return false
}