-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3fs.go
484 lines (431 loc) · 12.4 KB
/
s3fs.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package s3fs
import (
"bytes"
"context"
"errors"
"fmt"
iofs "io/fs"
"path"
"strings"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
fs "github.com/ungerik/go-fs"
"github.com/ungerik/go-fs/fsimpl"
)
// TODO use multipart download/upload https://aws.github.io/aws-sdk-go-v2/docs/sdk-utilities/s3/
const (
// Prefix os S3FileSystem URLs
Prefix = "s3://"
// Separator used in S3FileSystem paths
Separator = "/"
)
var (
// DefaultPermissions used for S3 bucket files
DefaultPermissions = fs.UserAndGroupReadWrite
// DefaultDirPermissions used for S3 bucket directories
DefaultDirPermissions = fs.UserAndGroupReadWrite + fs.AllReadWrite
// Make sure S3FileSystem implements fs.FileSystem
_ fs.FileSystem = new(fileSystem)
)
type fileSystem struct {
client *s3.Client
bucketName string
prefix string
readOnly bool
}
// NewAndRegister initializes a new S3 instance + session and returns a fs.FileSystem
// implementation that contains the required settings to work with an S3 bucket.
func NewAndRegister(client *s3.Client, bucketName string, readOnly bool) fs.FileSystem {
s3fs := &fileSystem{
client: client,
bucketName: bucketName,
prefix: Prefix + bucketName,
readOnly: readOnly,
}
fs.Register(s3fs)
return s3fs
}
func NewLoadDefaultConfig(ctx context.Context, bucketName string, readOnly bool) (fs.FileSystem, error) {
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, err
}
client := s3.NewFromConfig(cfg)
return NewAndRegister(client, bucketName, readOnly), nil
}
func (s *fileSystem) ReadableWritable() (readable, writable bool) {
return true, !s.readOnly
}
func (s *fileSystem) RootDir() fs.File {
return fs.File(s.prefix + Separator)
}
func (s *fileSystem) ID() (string, error) {
return s.bucketName, nil
}
func (s *fileSystem) Prefix() string {
return s.prefix
}
func (s *fileSystem) Name() string {
return "S3 file system for bucket: s.bucketName"
}
func (s *fileSystem) String() string {
return s.Name() + " with prefix " + s.prefix
}
func (s *fileSystem) URL(cleanPath string) string {
return s.prefix + cleanPath
}
func (f *fileSystem) CleanPathFromURI(uri string) string {
return strings.TrimPrefix(uri, f.prefix)
}
func (s *fileSystem) JoinCleanFile(uriParts ...string) fs.File {
return fs.File(s.prefix + s.JoinCleanPath(uriParts...))
}
func (s *fileSystem) JoinCleanPath(uriParts ...string) string {
return fsimpl.JoinCleanPath(uriParts, s.prefix, Separator)
}
func (s *fileSystem) SplitPath(filePath string) []string {
return fsimpl.SplitPath(filePath, s.prefix, Separator)
}
func (s *fileSystem) Separator() string {
return Separator
}
func (s *fileSystem) IsAbsPath(filePath string) bool {
return path.IsAbs(filePath)
}
func (s *fileSystem) AbsPath(filePath string) string {
if path.IsAbs(filePath) {
return filePath
}
return Separator + filePath
}
func (s *fileSystem) MatchAnyPattern(name string, patterns []string) (bool, error) {
return fsimpl.MatchAnyPattern(name, patterns)
}
func (*fileSystem) SplitDirAndName(filePath string) (dir, name string) {
return fsimpl.SplitDirAndName(filePath, 0, Separator)
}
func (s *fileSystem) VolumeName(filePath string) string {
return s.bucketName
}
func (s *fileSystem) Stat(filePath string) (iofs.FileInfo, error) {
if filePath == "" {
return nil, fs.ErrEmptyPath
}
out, err := s.client.HeadObject(
context.Background(),
&s3.HeadObjectInput{
Bucket: &s.bucketName,
Key: &filePath,
},
)
if err != nil {
var notFound *types.NotFound
if errors.As(err, ¬Found) {
return nil, fs.NewErrDoesNotExist(fs.File(s.prefix + filePath))
}
return nil, err
}
return &fileInfo{
name: path.Base(filePath),
size: *out.ContentLength,
time: *out.LastModified,
}, nil
}
func (s *fileSystem) Exists(filePath string) bool {
if filePath == "" || filePath == "/" {
return false
}
_, err := s.client.HeadObject(
context.Background(),
&s3.HeadObjectInput{
Bucket: &s.bucketName,
Key: &filePath,
},
)
return err == nil
}
// IsHidden returns true if the given file path is not empty and starts with a
// dot. There are no real "hidden" files in S3 buckets, but since dot prefixes
// are the general convention to determine which directories/files are hidden
// and which are not, the function behaves this way.
func (s *fileSystem) IsHidden(filePath string) bool {
name := path.Base(filePath)
return len(name) > 0 && name[0] == '.'
}
func (s *fileSystem) IsSymbolicLink(filePath string) bool {
return false
}
func (s *fileSystem) listDirInfo(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string, recursive bool) (err error) {
if dirPath == "" {
return fs.ErrEmptyPath
}
if ctx.Err() != nil {
return ctx.Err()
}
return fs.NewErrUnsupported(s, "listDirInfo")
// if len(dirPath) > 1 && strings.HasPrefix(dirPath, "/") {
// dirPath = dirPath[1:]
// }
// if !strings.HasSuffix(dirPath, "/") {
// dirPath += "/"
// }
// info, err := s.Stat(dirPath)
// if err != nil {
// return err
// }
// if !info.IsDir() {
// return fs.NewErrIsNotDirectory(fs.File(dirPath))
// }
// // We only need to set the prefix if we're listing the objects in any other
// // directory than the root directory. For the root dir, the StartAfter param
// // suffices.
// var prefix string
// if dirPath != "/" {
// prefix = dirPath
// }
// out, err := s.client.ListObjectsV2WithContext(
// ctx,
// &s3.ListObjectsV2Input{
// Bucket: &s.bucketName,
// Prefix: &prefix,
// StartAfter: &dirPath,
// },
// )
// for _, c := range out.Contents {
// if ctx.Err() != nil {
// return ctx.Err()
// }
// // Determine the number of slashes we allow in the path. This is only
// // used if the recursive argument is set to false because then we want
// // to filter the results.
// // If the contents of 'test/test/' are to be queried, we need to allow
// // more slashes than we would have to with '/' or 'test/'.
// var nos int
// if dirPath == "/" {
// nos = 1
// } else {
// nos = 1 + strings.Count(dirPath, "/")
// }
// var isNestedObject bool
// // Different rules apply to files than to directories.
// // e.g.:
// // key: slashes ends with slash
// // test/ 1 true
// // test/doc.pdf 1 false
// // These would both only have 1 occurrence of the forward slash.
// // We want to allow the directory in this case since it is a direct
// // child of the target directory. We don't want to allow the file.
// if *c.Size == 0 { // Directory
// isNestedObject = strings.Count(*c.Key, "/") > nos
// } else { // File
// isNestedObject = strings.Count(*c.Key, "/") >= nos
// }
// if !recursive && isNestedObject {
// continue
// }
// f := fs.File(*c.Key)
// fi := s.Info(f.Name())
// callback(f, fi)
// }
// return nil
}
func (s *fileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) (err error) {
return s.listDirInfo(ctx, dirPath, callback, patterns, false)
}
func (s *fileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) (err error) {
return s.listDirInfo(ctx, dirPath, callback, patterns, true)
}
func (s *fileSystem) Touch(filePath string, perm []fs.Permissions) error {
if s.Exists(filePath) {
return nil // TODO is this OK, can we change the modified time?
}
return s.WriteAll(context.Background(), filePath, make([]byte, 0), perm)
}
func (s *fileSystem) MakeDir(dirPath string, perm []fs.Permissions) error {
if dirPath == "" {
return fs.ErrEmptyPath
}
if dirPath == "/" {
return nil
}
if !strings.HasSuffix(dirPath, "/") {
dirPath += "/"
}
// Touch pretty much does what we need. In order to create a "directory"
// in the S3 bucket, we need to pass a key that ends on '/' and has no data.
// We add the slash above and Touch writes an object with no data.
return s.Touch(dirPath, perm)
}
func (s *fileSystem) ReadAll(ctx context.Context, filePath string) ([]byte, error) {
if filePath == "" {
return nil, fs.ErrEmptyPath
}
out, err := s.client.GetObject(
ctx,
&s3.GetObjectInput{
Bucket: &s.bucketName,
Key: &filePath,
},
)
if err != nil {
var notFound *types.NotFound
if errors.As(err, ¬Found) {
return nil, fs.NewErrDoesNotExist(fs.File(s.prefix + filePath))
}
return nil, err
}
defer out.Body.Close()
data := make([]byte, int(*out.ContentLength))
n, err := out.Body.Read(data)
if err != nil {
return nil, err
}
if n < int(*out.ContentLength) {
return nil, fmt.Errorf("read %d bytes from body but content-length is %d", n, out.ContentLength)
}
return data, nil
}
func (s *fileSystem) WriteAll(ctx context.Context, filePath string, data []byte, perm []fs.Permissions) error {
if filePath == "" {
return fs.ErrEmptyPath
}
if s.readOnly {
return fs.ErrReadOnlyFileSystem
}
_, err := s.client.PutObject(
ctx,
&s3.PutObjectInput{
Bucket: &s.bucketName,
Key: &filePath,
Body: bytes.NewReader(data),
},
)
return err
}
func (s *fileSystem) OpenReader(filePath string) (iofs.File, error) {
if filePath == "" {
return nil, fs.ErrEmptyPath
}
out, err := s.client.GetObject(
context.Background(),
&s3.GetObjectInput{
Bucket: &s.bucketName,
Key: &filePath,
},
)
if err != nil {
var notFound *types.NotFound
if errors.As(err, ¬Found) {
return nil, fs.NewErrDoesNotExist(fs.File(s.prefix + filePath))
}
return nil, err
}
defer out.Body.Close()
data := make([]byte, int(*out.ContentLength))
n, err := out.Body.Read(data)
if err != nil {
return nil, err
}
if n < int(*out.ContentLength) {
return nil, fmt.Errorf("read %d bytes from body but content-length is %d", n, out.ContentLength)
}
info := &fileInfo{
name: path.Base(filePath),
size: *out.ContentLength,
time: *out.LastModified,
}
return fsimpl.NewReadonlyFileBuffer(data, info), nil
}
func (s *fileSystem) OpenWriter(filePath string, perm []fs.Permissions) (fs.WriteCloser, error) {
if filePath == "" {
return nil, fs.ErrEmptyPath
}
if s.readOnly {
return nil, fs.ErrReadOnlyFileSystem
}
var fileBuffer *fsimpl.FileBuffer
fileBuffer = fsimpl.NewFileBufferWithClose(nil, func() error {
return s.WriteAll(context.Background(), filePath, fileBuffer.Bytes(), perm)
})
return fileBuffer, nil
}
func (s *fileSystem) OpenReadWriter(filePath string, perm []fs.Permissions) (fs.ReadWriteSeekCloser, error) {
return s.openFileBuffer(filePath)
}
func (s *fileSystem) openFileBuffer(filePath string) (fileBuffer *fsimpl.FileBuffer, err error) {
if s.readOnly {
return nil, fs.ErrReadOnlyFileSystem
}
current, err := s.ReadAll(context.Background(), filePath)
if err != nil {
return nil, err
}
fileBuffer = fsimpl.NewFileBufferWithClose(current, func() error {
return s.WriteAll(context.Background(), filePath, fileBuffer.Bytes(), nil)
})
return fileBuffer, nil
}
func (s *fileSystem) CopyFile(ctx context.Context, srcFile string, destFile string, buf *[]byte) error {
if s.readOnly {
return fs.ErrReadOnlyFileSystem
}
if srcFile == "" || destFile == "" {
return fs.ErrEmptyPath
}
srcFile = s.bucketName + "/" + srcFile
_, err := s.client.CopyObject(
ctx, &s3.CopyObjectInput{
Bucket: &s.bucketName,
CopySource: &srcFile,
Key: &destFile,
},
)
var notFound *types.NotFound
if err != nil && errors.As(err, ¬Found) {
err = fs.NewErrDoesNotExist(fs.File(s.prefix + srcFile))
}
return err
}
func (s *fileSystem) Remove(filePath string) error {
if s.readOnly {
return fs.ErrReadOnlyFileSystem
}
if filePath == "" {
return fs.ErrEmptyPath
}
_, err := s.client.DeleteObject(
context.Background(),
&s3.DeleteObjectInput{
Bucket: &s.bucketName,
Key: &filePath,
})
return err
}
func (s *fileSystem) Watch(filePath string, onEvent func(fs.File, fs.Event)) (cancel func() error, err error) {
// https://stackoverflow.com/questions/18049717/waituntilobjectexists-amazon-s3-php-sdk-method-exactly-how-does-it-work
// s.client.WaitUntilObjectExists
// s.client.WaitUntilObjectNotExists
/*retChan := make(chan fs.WatchEvent)
go func() {
err := s.client.WaitUntilObjectExists(&s3.HeadObjectInput{
Bucket: &s.bucketName),
Key: &filePath),
})
if err != nil {
retChan <- fs.WatchEvent{
Err: err,
}
}
}()*/
//return retChan, nil
return nil, errors.ErrUnsupported
}
func (s *fileSystem) Close() error {
if s.client == nil {
return nil // already closed
}
fs.Unregister(s)
s.client = nil
return nil
}