forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 19
/
s3.go
285 lines (241 loc) · 6.95 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Package s3backupstorage implements the BackupStorage interface for AWS S3.
//
// AWS access credentials are configured via standard AWS means, such as:
// - AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
// - credentials file at ~/.aws/credentials
// - if running on an EC2 instance, an IAM role
// See details at http://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs
package s3backupstorage
import (
"flag"
"fmt"
"io"
"sort"
"strings"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/mysqlctl/backupstorage"
)
var (
// AWS API region
region = flag.String("s3_backup_aws_region", "us-east-1", "AWS region to use")
// bucket is where the backups will go.
bucket = flag.String("s3_backup_storage_bucket", "", "S3 bucket to use for backups")
// root is a prefix added to all object names.
root = flag.String("s3_backup_storage_root", "", "root prefix for all backup-related object names")
// path component delimiter
delimiter = "/"
)
// S3BackupHandle implements the backupstorage.BackupHandle interface.
type S3BackupHandle struct {
client *s3.S3
bs *S3BackupStorage
dir string
name string
readOnly bool
errors concurrency.AllErrorRecorder
waitGroup sync.WaitGroup
}
// Directory is part of the backupstorage.BackupHandle interface.
func (bh *S3BackupHandle) Directory() string {
return bh.dir
}
// Name is part of the backupstorage.BackupHandle interface.
func (bh *S3BackupHandle) Name() string {
return bh.name
}
// AddFile is part of the backupstorage.BackupHandle interface.
func (bh *S3BackupHandle) AddFile(ctx context.Context, filename string) (io.WriteCloser, error) {
if bh.readOnly {
return nil, fmt.Errorf("AddFile cannot be called on read-only backup")
}
reader, writer := io.Pipe()
bh.waitGroup.Add(1)
go func() {
defer bh.waitGroup.Done()
uploader := s3manager.NewUploaderWithClient(bh.client)
object := objName(bh.dir, bh.name, filename)
_, err := uploader.Upload(&s3manager.UploadInput{
Bucket: bucket,
Key: object,
Body: reader,
})
if err != nil {
reader.CloseWithError(err)
bh.errors.RecordError(err)
}
}()
return writer, nil
}
// EndBackup is part of the backupstorage.BackupHandle interface.
func (bh *S3BackupHandle) EndBackup(ctx context.Context) error {
if bh.readOnly {
return fmt.Errorf("EndBackup cannot be called on read-only backup")
}
bh.waitGroup.Wait()
return bh.errors.Error()
}
// AbortBackup is part of the backupstorage.BackupHandle interface.
func (bh *S3BackupHandle) AbortBackup(ctx context.Context) error {
if bh.readOnly {
return fmt.Errorf("AbortBackup cannot be called on read-only backup")
}
return bh.bs.RemoveBackup(ctx, bh.dir, bh.name)
}
// ReadFile is part of the backupstorage.BackupHandle interface.
func (bh *S3BackupHandle) ReadFile(ctx context.Context, filename string) (io.ReadCloser, error) {
if !bh.readOnly {
return nil, fmt.Errorf("ReadFile cannot be called on read-write backup")
}
object := objName(bh.dir, bh.name, filename)
out, err := bh.client.GetObject(&s3.GetObjectInput{
Bucket: bucket,
Key: object,
})
if err != nil {
return nil, err
}
return out.Body, nil
}
var _ backupstorage.BackupHandle = (*S3BackupHandle)(nil)
// S3BackupStorage implements the backupstorage.BackupStorage interface.
type S3BackupStorage struct {
_client *s3.S3
mu sync.Mutex
}
// ListBackups is part of the backupstorage.BackupStorage interface.
func (bs *S3BackupStorage) ListBackups(ctx context.Context, dir string) ([]backupstorage.BackupHandle, error) {
c, err := bs.client()
if err != nil {
return nil, err
}
searchPrefix := objName(dir, "")
query := &s3.ListObjectsV2Input{
Bucket: bucket,
Delimiter: &delimiter,
Prefix: searchPrefix,
}
var subdirs []string
for {
objs, err := c.ListObjectsV2(query)
if err != nil {
return nil, err
}
for _, prefix := range objs.CommonPrefixes {
subdir := strings.TrimPrefix(*prefix.Prefix, *searchPrefix)
subdir = strings.TrimSuffix(subdir, delimiter)
subdirs = append(subdirs, subdir)
}
if objs.NextContinuationToken == nil {
break
}
query.ContinuationToken = objs.NextContinuationToken
}
// Backups must be returned in order, oldest first.
sort.Strings(subdirs)
result := make([]backupstorage.BackupHandle, 0, len(subdirs))
for _, subdir := range subdirs {
result = append(result, &S3BackupHandle{
client: c,
bs: bs,
dir: dir,
name: subdir,
readOnly: true,
})
}
return result, nil
}
// StartBackup is part of the backupstorage.BackupStorage interface.
func (bs *S3BackupStorage) StartBackup(ctx context.Context, dir, name string) (backupstorage.BackupHandle, error) {
c, err := bs.client()
if err != nil {
return nil, err
}
return &S3BackupHandle{
client: c,
bs: bs,
dir: dir,
name: name,
readOnly: false,
}, nil
}
// RemoveBackup is part of the backupstorage.BackupStorage interface.
func (bs *S3BackupStorage) RemoveBackup(ctx context.Context, dir, name string) error {
c, err := bs.client()
if err != nil {
return err
}
query := &s3.ListObjectsV2Input{
Bucket: bucket,
Prefix: objName(dir, ""),
}
for {
objs, err := c.ListObjectsV2(query)
if err != nil {
return err
}
objIds := make([]*s3.ObjectIdentifier, 0, len(objs.Contents))
for _, obj := range objs.Contents {
objIds = append(objIds, &s3.ObjectIdentifier{
Key: obj.Key,
})
}
quiet := true // return less in the Delete response
out, err := c.DeleteObjects(&s3.DeleteObjectsInput{
Bucket: bucket,
Delete: &s3.Delete{
Objects: objIds,
Quiet: &quiet,
},
})
if err != nil {
return err
}
for _, objError := range out.Errors {
return fmt.Errorf(objError.String())
}
if objs.NextContinuationToken == nil {
break
}
query.ContinuationToken = objs.NextContinuationToken
}
return nil
}
// Close is part of the backupstorage.BackupStorage interface.
func (bs *S3BackupStorage) Close() error {
bs.mu.Lock()
defer bs.mu.Unlock()
bs._client = nil
return nil
}
var _ backupstorage.BackupStorage = (*S3BackupStorage)(nil)
func (bs *S3BackupStorage) client() (*s3.S3, error) {
bs.mu.Lock()
defer bs.mu.Unlock()
if bs._client == nil {
bs._client = s3.New(session.New(), &aws.Config{Region: aws.String(*region)})
if len(*bucket) == 0 {
return nil, fmt.Errorf("-s3_backup_storage_bucket required")
}
if _, err := bs._client.HeadBucket(&s3.HeadBucketInput{Bucket: bucket}); err != nil {
return nil, err
}
}
return bs._client, nil
}
func objName(parts ...string) *string {
res := ""
if *root != "" {
res += *root + delimiter
}
res += strings.Join(parts, delimiter)
return &res
}
func init() {
backupstorage.BackupStorageMap["s3"] = &S3BackupStorage{}
}