forked from fclairamb/afero-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_fs.go
348 lines (302 loc) · 8.86 KB
/
s3_fs.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
// Package s3 brings S3 files handling to afero
package s3
import (
"bytes"
"errors"
"fmt"
"mime"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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"
"github.com/spf13/afero"
)
// Fs is an FS object backed by S3.
type Fs struct {
FileProps *UploadedFileProperties // FileProps define the file properties we want to set for all new files
session *session.Session // Session config
s3API *s3.S3
bucket string // Bucket name
}
// UploadedFileProperties defines all the set properties applied to future files
type UploadedFileProperties struct {
ACL *string // ACL defines the right to apply
CacheControl *string // CacheControl defines the Cache-Control header
ContentType *string // ContentType define the Content-Type header
}
// NewFs creates a new Fs object writing files to a given S3 bucket.
func NewFs(bucket string, session *session.Session) *Fs {
s3Api := s3.New(session)
return &Fs{
bucket: bucket,
session: session,
s3API: s3Api,
}
}
// ErrNotImplemented is returned when this operation is not (yet) implemented
var ErrNotImplemented = errors.New("not implemented")
// ErrNotSupported is returned when this operations is not supported by S3
var ErrNotSupported = errors.New("s3 doesn't support this operation")
// ErrAlreadyOpened is returned when the file is already opened
var ErrAlreadyOpened = errors.New("already opened")
// ErrInvalidSeek is returned when the seek operation is not doable
var ErrInvalidSeek = errors.New("invalid seek offset")
// Name returns the type of FS object this is: Fs.
func (Fs) Name() string { return "s3" }
// Create a file.
func (fs Fs) Create(name string) (afero.File, error) {
{ // It's faster to trigger an explicit empty put object than opening a file for write, closing it and re-opening it
req := &s3.PutObjectInput{
Bucket: aws.String(fs.bucket),
Key: aws.String(name),
Body: bytes.NewReader([]byte{}),
}
if fs.FileProps != nil {
applyFileCreateProps(req, fs.FileProps)
}
// If no Content-Type was specified, we'll guess one
if req.ContentType == nil {
req.ContentType = aws.String(mime.TypeByExtension(filepath.Ext(name)))
}
_, errPut := fs.s3API.PutObject(req)
if errPut != nil {
return nil, errPut
}
}
file, err := fs.OpenFile(name, os.O_WRONLY, 0750)
if err != nil {
return file, err
}
// Create(), like all of S3, is eventually consistent.
// To protect against unexpected behavior, have this method
// wait until S3 reports the object exists.
return file, fs.s3API.WaitUntilObjectExists(&s3.HeadObjectInput{
Bucket: aws.String(fs.bucket),
Key: aws.String(name),
})
}
// Mkdir makes a directory in S3.
func (fs Fs) Mkdir(name string, perm os.FileMode) error {
file, err := fs.OpenFile(fmt.Sprintf("%s/", path.Clean(name)), os.O_CREATE, perm)
if err == nil {
err = file.Close()
}
return err
}
// MkdirAll creates a directory and all parent directories if necessary.
func (fs Fs) MkdirAll(path string, perm os.FileMode) error {
return fs.Mkdir(path, perm)
}
// Open a file for reading.
func (fs *Fs) Open(name string) (afero.File, error) {
return fs.OpenFile(name, os.O_RDONLY, 0777)
}
// OpenFile opens a file.
func (fs *Fs) OpenFile(name string, flag int, _ os.FileMode) (afero.File, error) {
file := NewFile(fs, name)
// Reading and writing is technically supported but can't lead to anything that makes sense
if flag&os.O_RDWR != 0 {
return nil, ErrNotSupported
}
// Appending is not supported by S3. It's do-able though by:
// - Copying the existing file to a new place (for example $file.previous)
// - Writing a new file, streaming the content of the previous file in it
// - Writing the data you want to append
// Quite network intensive, if used in abondance this would lead to terrible performances.
if flag&os.O_APPEND != 0 {
return nil, ErrNotSupported
}
// Creating is basically a write
if flag&os.O_CREATE != 0 {
flag |= os.O_WRONLY
}
// We either write
if flag&os.O_WRONLY != 0 {
return file, file.openWriteStream()
}
info, err := file.Stat()
if err != nil {
return nil, err
}
if info.IsDir() {
return file, nil
}
return file, file.openReadStream(0)
}
// Remove a file
func (fs Fs) Remove(name string) error {
if _, err := fs.Stat(name); err != nil {
return err
}
return fs.forceRemove(name)
}
// forceRemove doesn't error if a file does not exist.
func (fs Fs) forceRemove(name string) error {
_, err := fs.s3API.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(fs.bucket),
Key: aws.String(name),
})
return err
}
// RemoveAll removes a path.
func (fs *Fs) RemoveAll(name string) error {
s3dir := NewFile(fs, name)
fis, err := s3dir.Readdir(0)
if err != nil {
return err
}
for _, fi := range fis {
fullpath := path.Join(s3dir.Name(), fi.Name())
if fi.IsDir() {
if err := fs.RemoveAll(fullpath); err != nil {
return err
}
} else {
if err := fs.forceRemove(fullpath); err != nil {
return err
}
}
}
// finally remove the "file" representing the directory
if err := fs.forceRemove(s3dir.Name() + "/"); err != nil {
return err
}
return nil
}
// Rename a file.
// There is no method to directly rename an S3 object, so the Rename
// will copy the file to an object with the new name and then delete
// the original.
func (fs Fs) Rename(oldname, newname string) error {
if oldname == newname {
return nil
}
_, err := fs.s3API.CopyObject(&s3.CopyObjectInput{
Bucket: aws.String(fs.bucket),
CopySource: aws.String(fs.bucket + oldname),
Key: aws.String(newname),
})
if err != nil {
return err
}
_, err = fs.s3API.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(fs.bucket),
Key: aws.String(oldname),
})
return err
}
// Stat returns a FileInfo describing the named file.
// If there is an error, it will be of type *os.PathError.
func (fs Fs) Stat(name string) (os.FileInfo, error) {
out, err := fs.s3API.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(fs.bucket),
Key: aws.String(name),
})
if err != nil {
var errRequestFailure awserr.RequestFailure
if errors.As(err, &errRequestFailure) {
if errRequestFailure.StatusCode() == 404 {
statDir, errStat := fs.statDirectory(name)
return statDir, errStat
}
}
return FileInfo{}, &os.PathError{
Op: "stat",
Path: name,
Err: err,
}
} else if strings.HasSuffix(name, "/") {
// user asked for a directory, but this is a file
return FileInfo{name: name}, nil
/*
return FileInfo{}, &os.PathError{
Op: "stat",
Path: name,
Err: os.ErrNotExist,
}
*/
}
return NewFileInfo(path.Base(name), false, *out.ContentLength, *out.LastModified), nil
}
func (fs Fs) statDirectory(name string) (os.FileInfo, error) {
nameClean := path.Clean(name)
out, err := fs.s3API.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(fs.bucket),
Prefix: aws.String(strings.TrimPrefix(nameClean, "/")),
MaxKeys: aws.Int64(1),
})
if err != nil {
return FileInfo{}, &os.PathError{
Op: "stat",
Path: name,
Err: err,
}
}
if (out.KeyCount == nil || *out.KeyCount == 0) && name != "" {
return nil, &os.PathError{
Op: "stat",
Path: name,
Err: os.ErrNotExist,
}
}
return NewFileInfo(path.Base(name), true, 0, time.Unix(0, 0)), nil
}
// Chmod doesn't exists in S3 but could be implemented by analyzing ACLs
func (fs Fs) Chmod(name string, mode os.FileMode) error {
var acl string
otherRead := mode&(1<<2) != 0
otherWrite := mode&(1<<1) != 0
switch {
case otherRead && otherWrite:
acl = "public-read-write"
case otherRead:
acl = "public-read"
default:
acl = "private"
}
_, err := fs.s3API.PutObjectAcl(&s3.PutObjectAclInput{
Bucket: aws.String(fs.bucket),
Key: aws.String(name),
ACL: aws.String(acl),
})
return err
}
// Chown doesn't exist in S3 should probably NOT have been added to afero as it's POSIX-only concept.
func (Fs) Chown(string, int, int) error {
return ErrNotSupported
}
// Chtimes could be implemented if needed, but that would require to override object properties using metadata,
// which makes it a non-standard solution
func (Fs) Chtimes(string, time.Time, time.Time) error {
return ErrNotSupported
}
// I couldn't find a way to make this code cleaner. It's basically a big copy-paste on two
// very similar structures.
func applyFileCreateProps(req *s3.PutObjectInput, p *UploadedFileProperties) {
if p.ACL != nil {
req.ACL = p.ACL
}
if p.CacheControl != nil {
req.CacheControl = p.CacheControl
}
if p.ContentType != nil {
req.ContentType = p.ContentType
}
}
func applyFileWriteProps(req *s3manager.UploadInput, p *UploadedFileProperties) {
if p.ACL != nil {
req.ACL = p.ACL
}
if p.CacheControl != nil {
req.CacheControl = p.CacheControl
}
if p.ContentType != nil {
req.ContentType = p.ContentType
}
}