-
Notifications
You must be signed in to change notification settings - Fork 30
/
s3.go
252 lines (210 loc) · 6.21 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
package s3
import (
"context"
"fmt"
"io"
"strings"
"github.com/drone/drone-cache-lib/storage"
"github.com/dustin/go-humanize"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/sirupsen/logrus"
)
// Options contains configuration for the S3 connection.
type Options struct {
Endpoint string
AcceleratedEndpoint string
Key string
Secret string
Access string
Token string
FileCredentials string
Profile string
// us-east-1
// us-west-1
// us-west-2
// eu-west-1
// ap-southeast-1
// ap-southeast-2
// ap-northeast-1
// sa-east-1
Region string
UseSSL bool
}
type s3Storage struct {
client *minio.Client
opts *Options
ctx context.Context
}
// New method creates an implementation of Storage with S3 as the backend.
func New(opts *Options) (storage.Storage, error) {
var creds *credentials.Credentials
if len(opts.Access) != 0 && len(opts.Secret) != 0 {
creds = credentials.NewStaticV4(opts.Access, opts.Secret, opts.Token)
} else if len(opts.FileCredentials) != 0 {
creds = credentials.NewFileAWSCredentials(opts.FileCredentials, opts.Profile)
} else {
creds = credentials.NewIAM("")
// See if the IAM role can be retrieved
_, err := creds.Get()
if err != nil {
return nil, fmt.Errorf("could not connect to %s using IAM role: %w", opts.Endpoint, err)
}
}
client, err := minio.New(opts.Endpoint, &minio.Options{
Creds: creds,
Secure: opts.UseSSL,
Region: opts.Region,
})
if err != nil {
return nil, fmt.Errorf("could not connect to %s: %w", opts.Endpoint, err)
}
if opts.AcceleratedEndpoint != "" {
client.SetS3TransferAccelerate(opts.AcceleratedEndpoint)
}
return &s3Storage{
client: client,
opts: opts,
ctx: context.Background(),
}, nil
}
func (s *s3Storage) Get(p string, dst io.Writer) error {
bucket, key := splitBucket(p)
if len(bucket) == 0 || len(key) == 0 {
return fmt.Errorf("invalid path %s", p)
}
logrus.WithFields(logrus.Fields{
"bucket": bucket,
"key": key,
}).Info("downloading file")
exists, err := s.client.BucketExists(s.ctx, bucket)
if err != nil {
return fmt.Errorf("error when accessing bucket %s: %w", bucket, err)
} else if !exists {
return fmt.Errorf("bucket %s does not exist", bucket)
}
object, err := s.client.GetObject(s.ctx, bucket, key, minio.GetObjectOptions{})
if err != nil {
return fmt.Errorf("could not retrieve %s from %s: %w", bucket, key, err)
}
numBytes, err := io.Copy(dst, object)
if err != nil {
return err
}
logrus.WithFields(logrus.Fields{
"bucket": bucket,
"key": key,
"size": humanize.Bytes(uint64(numBytes)),
}).Info("file downloaded", bucket, key)
return nil
}
func (s *s3Storage) Put(p string, src io.Reader) error {
bucket, key := splitBucket(p)
if len(bucket) == 0 || len(key) == 0 {
return fmt.Errorf("invalid path %s", p)
}
logrus.WithFields(logrus.Fields{
"bucket": bucket,
"key": key,
}).Info("uploading file")
exists, err := s.client.BucketExists(s.ctx, bucket)
if err != nil {
return fmt.Errorf("error when accessing bucket %s: %w", bucket, err)
} else if !exists {
return fmt.Errorf("bucket %s does not exist", bucket)
}
if !exists {
if err = s.client.MakeBucket(s.ctx, bucket, minio.MakeBucketOptions{Region: s.opts.Region}); err != nil {
return fmt.Errorf("could not create bucket %s: %w", bucket, err)
}
logrus.WithField("name", bucket).Info("bucket created")
} else {
logrus.WithField("name", bucket).Info("bucket found")
}
uploadInfo, err := s.client.PutObject(s.ctx, bucket, key, src, -1, minio.PutObjectOptions{ContentType: "application/tar"})
if err != nil {
return fmt.Errorf("could not put file in bucket %s at %s: %w", bucket, key, err)
}
logrus.WithFields(logrus.Fields{
"bucket": bucket,
"key": key,
"size": uploadInfo,
}).Info("file uploaded")
return nil
}
func (s *s3Storage) List(p string) ([]storage.FileEntry, error) {
bucket, key := splitBucket(p)
if len(bucket) == 0 || len(key) == 0 {
return nil, fmt.Errorf("invalid path %s", p)
}
logrus.WithFields(logrus.Fields{
"bucket": bucket,
"key": key,
}).Info("finding objects")
exists, err := s.client.BucketExists(s.ctx, bucket)
if err != nil {
return nil, fmt.Errorf("error when accessing bucket %s: %w", bucket, err)
} else if !exists {
return nil, fmt.Errorf("bucket %s does not exist", bucket)
}
var objects []storage.FileEntry
opts := minio.ListObjectsOptions{
Recursive: true,
Prefix: key,
}
for object := range s.client.ListObjects(s.ctx, bucket, opts) {
if object.Err != nil {
return nil, fmt.Errorf("could not get file in bucket %s at %s: %w", bucket, object.Key, object.Err)
}
path := bucket + "/" + object.Key
objects = append(objects, storage.FileEntry{
Path: path,
Size: object.Size,
LastModified: object.LastModified,
})
logrus.WithFields(logrus.Fields{
"bucket": bucket,
"key": object.Key,
"size": humanize.Bytes(uint64(object.Size)),
"last-modified": object.LastModified,
}).Debug("found object")
}
logrus.WithFields(logrus.Fields{
"bucket": bucket,
"key": key,
"count": len(objects),
}).Info("found objects")
return objects, nil
}
func (s *s3Storage) Delete(p string) error {
bucket, key := splitBucket(p)
if len(bucket) == 0 || len(key) == 0 {
return fmt.Errorf("invalid path %s", p)
}
logrus.WithFields(logrus.Fields{
"bucket": bucket,
"key": key,
}).Info("deleting object")
exists, err := s.client.BucketExists(s.ctx, bucket)
if err != nil {
return fmt.Errorf("error when accessing bucket %s: %w", bucket, err)
} else if !exists {
return fmt.Errorf("bucket %s does not exist", bucket)
}
err = s.client.RemoveObject(s.ctx, bucket, key, minio.RemoveObjectOptions{})
if err != nil {
return fmt.Errorf("could not delete file in %s at %s: %w", bucket, key, err)
}
return err
}
func splitBucket(p string) (string, string) {
// Remove initial forward slash
full := strings.TrimPrefix(p, "/")
// Get first index
i := strings.Index(full, "/")
if i != -1 && len(full) != i+1 {
// Bucket names need to be all lower case for the key it doesnt matter
return strings.ToLower(full[0:i]), full[i+1:]
}
return "", ""
}