forked from asonawalla/gazette
/
s3_file.go
388 lines (336 loc) · 9.18 KB
/
s3_file.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package cloudstore
import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
log "github.com/sirupsen/logrus"
)
const (
// Buffer roughly this much data in memory before flushing a multipart
// chunk. (The true maximum multipart upload fragment size is much larger.)
// Logistically, 1GiB chunks are more efficient, but we'd probably want to
// spool to disk instead. This maximum spool size makes our maximum single
// object file size 1TiB, as 10000 parts are allowed to any S3 object.
MaxSpoolSizeBytes = 1024 * 1024 * 100
)
var (
// By default, the API gives us 1000 keys per ListObjects call when we
// leave this nil. Test cases can set this to artifically force multiple
// calls to ListObjects.
s3MaxListObjectsKeys *int64
)
type s3File struct {
svc *s3.S3
bucket, key *string
object *s3.Object
// TODO(joshk): Factor members below into mini structs and use a union type
// here. Issue #2039
// Used in file read operations.
readCloser io.ReadCloser
readOffset int64
// Used in directory read operations.
results []os.FileInfo
resultsIndex int
// Used in write operations.
uploadId *string
uploadedParts []*s3.CompletedPart
spool bytes.Buffer
compressor io.WriteCloser
// Global error state for this file.
err error
}
// File interface method.
func (f *s3File) Read(p []byte) (int, error) {
var reader io.ReadCloser
reader, f.err = f.reader()
if f.err != nil {
return 0, f.err
}
var n int
n, f.err = reader.Read(p)
f.readOffset += int64(n)
return n, f.err
}
// File interface method.
func (f *s3File) Write(p []byte) (int, error) {
var n int
var buf = &f.spool
if f.compressor != nil {
n, f.err = f.compressor.Write(p)
} else {
n, f.err = buf.Write(p)
}
if f.err != nil {
if f.compressor != nil {
f.compressor.Close()
}
// If necessary, spill spool to S3 multipart chunk.
} else if buf.Len() > MaxSpoolSizeBytes {
if f.err = f.uploadSpool(); f.err != nil {
return 0, f.err
}
}
return n, f.err
}
// File interface method.
func (f *s3File) Close() error {
if f.IsDir() {
// Closing a directory object is a no-op.
} else if f.readCloser != nil {
// Here, we don't use reader(), as that would cause an unopened
// readCloser to be opened before immediately closing it.
f.err = f.readCloser.Close()
f.readCloser = nil
} else if f.isUpload() {
// Drain final multipart chunk, if necessary. If uploading a totally
// blank file, we still need to provide at least one part.
if f.spool.Len() > 0 || len(f.uploadedParts) == 0 {
if f.err = f.uploadSpool(); f.err != nil {
return f.err
}
}
var params = s3.CompleteMultipartUploadInput{
Bucket: f.bucket,
Key: f.key,
UploadId: f.uploadId,
MultipartUpload: &s3.CompletedMultipartUpload{
Parts: f.uploadedParts,
},
}
_, f.err = f.svc.CompleteMultipartUpload(¶ms)
if f.compressor != nil {
f.compressor.Close()
}
} else {
panic("cannot determine kind of s3File object for Close()")
}
return f.err
}
// File interface method.
func (f *s3File) Seek(offset int64, whence int) (int64, error) {
if f.isUpload() {
return 0, errors.New("cannot seek a S3 writer")
}
// Make |offset| absolute.
if whence == 0 {
} else if whence == 1 {
offset += f.readOffset
} else if whence == 2 {
return 0, errors.New("a S3 writer cannot seek relative to file end")
}
// If seeking before the current |readOffset|, re-open |f.readCloser|.
if offset < f.readOffset && (f.err == nil || f.err == io.EOF) {
f.readCloser.Close()
f.readCloser = nil
f.readOffset = 0
f.err = nil
} else if f.err != nil {
return f.readOffset, f.err
}
// Errors and offsets are tracked by f.Read().
io.CopyN(ioutil.Discard, f, offset-f.readOffset)
return f.readOffset, f.err
}
// File interface method.
func (f *s3File) Readdir(count int) ([]os.FileInfo, error) {
if !f.IsDir() {
return nil, errors.New("not a directory")
}
// TODO(joshk): Streaming directory listings (e.g. on-demand calls to
// s3.ListObjectsV2.) Right now, just call until all entries have been
// returned, then iterate over the results in-memory.
if len(f.results) == 0 {
f.results, f.err = f.listObjects()
if f.err != nil {
return nil, f.err
}
f.resultsIndex = 0
}
var remain = f.results[f.resultsIndex:]
var toReturn int
if nRemain := len(remain); nRemain == 0 {
return nil, io.EOF
} else if count < 0 || count > nRemain {
toReturn = nRemain
} else {
toReturn = count
}
f.resultsIndex += toReturn
return remain[:toReturn], nil
}
// File interface method
func (f *s3File) ContentSignature() (string, error) {
if f.object == nil {
return "", os.ErrNotExist
} else {
return *f.object.ETag, nil
}
}
// A s3File is also a os.FileInfo.
func (f *s3File) Stat() (os.FileInfo, error) {
return f, nil
}
// os.FileInfo interface method.
func (f *s3File) Name() string {
var name = *f.key
if name == "" {
return *f.bucket
} else if name[len(name)-1] == '/' {
name = name[:len(name)-1]
}
return name[strings.LastIndex(name, "/")+1:]
}
// os.FileInfo interface method. May return size of the compressed file,
// and not the actual bytestream length.
func (f *s3File) Size() int64 {
if f.object != nil {
return *f.object.Size
}
return 0
}
// os.FileInfo interface method.
func (f *s3File) Mode() os.FileMode {
log.Panic("s3File does not implement Mode()")
return 0
}
// os.FileInfo interface method.
func (f *s3File) ModTime() time.Time {
if f.object != nil {
return *f.object.LastModified
}
return time.Time{}
}
// os.FileInfo interface method.
func (f *s3File) IsDir() bool {
return isBucketStoreDir(*f.key)
}
// os.FileInfo interface method.
func (f *s3File) Sys() interface{} {
return nil
}
func (f *s3File) reader() (io.ReadCloser, error) {
var err error
if f.readCloser == nil {
var params = s3.GetObjectInput{
Bucket: f.bucket,
Key: f.key,
}
var resp *s3.GetObjectOutput
resp, err = f.svc.GetObject(¶ms)
if err == nil {
f.readCloser = resp.Body
}
}
return f.readCloser, err
}
// Uploads the current content of the spool as a multipart chunk to S3.
// Records the ETag of the uploaded part on success, then resets the spool and
// increments the part number to prepare for the next series of writes.
func (f *s3File) uploadSpool() error {
var buf = &f.spool
var partNumber int64 = int64(len(f.uploadedParts)) + 1
var params = s3.UploadPartInput{
Bucket: f.bucket,
Key: f.key,
PartNumber: aws.Int64(partNumber),
UploadId: f.uploadId,
Body: bytes.NewReader(buf.Bytes()),
ContentLength: aws.Int64(int64(buf.Len())),
}
var err error
var resp *s3.UploadPartOutput
resp, err = f.svc.UploadPart(¶ms)
if err != nil {
f.abortUpload()
return err
} else if resp.ETag == nil {
f.abortUpload()
err = errors.New("expected ETag on successful UploadPart")
return err
}
// On success, rotate the spool.
buf.Reset()
// Record response ETag. We must provide it to complete the upload.
f.uploadedParts = append(f.uploadedParts, &s3.CompletedPart{
ETag: resp.ETag,
PartNumber: aws.Int64(partNumber),
})
return nil
}
func (f *s3File) abortUpload() {
// Abort the upload, if possible. Returned |err| is for the initial
// part upload that we just tried -- abort error is logged but
// swallowed.
var abortParams = s3.AbortMultipartUploadInput{
Bucket: f.bucket,
Key: f.key,
UploadId: f.uploadId,
}
_, abortErr := f.svc.AbortMultipartUpload(&abortParams)
log.WithField("err", abortErr).Warn("failed to abort multipart upload")
}
func (f *s3File) listObjects() ([]os.FileInfo, error) {
if !f.IsDir() {
return nil, errors.New("not a directory")
}
// TODO(joshk): Instead, store *s3.Object? Problem is, prefixes aren't
// objects, but Readdir() must return prefixes as well as objects.
var results []os.FileInfo
var continuation *string
for {
var listParams = s3.ListObjectsV2Input{
Bucket: f.bucket,
ContinuationToken: continuation,
Delimiter: aws.String("/"),
MaxKeys: s3MaxListObjectsKeys,
Prefix: f.key,
}
objects, err := f.svc.ListObjectsV2(&listParams)
if err != nil {
return nil, err
}
for i := range objects.CommonPrefixes {
if *objects.CommonPrefixes[i].Prefix == "/" {
// Don't tangle with any prefix which actually contains the
// delimiter as part of its name. Just skip it.
continue
}
var f = &s3File{
svc: f.svc,
bucket: f.bucket,
key: objects.CommonPrefixes[i].Prefix,
}
results = append(results, f)
}
for i := range objects.Contents {
if objects.Contents[i].Key == f.key {
// A directory can also have a regular file with the same path.
// Skip it, should one occur.
continue
}
var f = &s3File{
svc: f.svc,
bucket: f.bucket,
key: objects.Contents[i].Key,
object: objects.Contents[i],
}
results = append(results, f)
}
if objects.IsTruncated != nil && !*objects.IsTruncated {
break
}
continuation = objects.NextContinuationToken
}
return results, nil
}
// Common source of truth to define what constitutes a *s3File being used for
// upload purposes.
func (f *s3File) isUpload() bool {
return f.uploadId != nil
}