-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths3driver.go
606 lines (474 loc) · 14.1 KB
/
s3driver.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
package main
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"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/fclairamb/ftpserver/server"
"log"
"os"
"path/filepath"
"strings"
"time"
)
type S3Driver struct {
s3Session *session.Session
s3Client *s3.S3
s3BucketName string
rootPrefix string
ftpPort int
ftpUser string
ftpPasswd string
}
func (d *S3Driver) WelcomeUser(cc server.ClientContext) (string, error) {
cc.SetDebug(true)
return "Welcome to the FTP server for S3", nil
}
func (d *S3Driver) AuthUser(cc server.ClientContext, user, pass string) (server.ClientHandlingDriver, error) {
if user != d.ftpUser {
log.Println("username does not match expected user", user)
return nil, fmt.Errorf("incorrect username: %s", user)
}
if pass != d.ftpPasswd {
log.Println("incorrect password")
return nil, errors.New("incorrect password")
}
_, err := session.NewSession()
if err != nil {
log.Println("error creating S3 session (check AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY env vars on server):", err)
return nil, err
}
return d, nil
}
func (d *S3Driver) GetTLSConfig() (*tls.Config, error) {
return nil, errors.New("TLS not implemented")
}
func (d *S3Driver) ChangeDirectory(cc server.ClientContext, directory string) error {
// Query S3 to see if the directory exists. We return an error if we cannot list it.
var err error
// root dir is special case, always exists in a bucket
if directory == "/" {
return nil
}
var prefix string
if prefix, err = d.getS3Key(directory + "/"); err != nil {
return err
}
delimiter := "/"
params := &s3.ListObjectsV2Input{
Bucket: &S3_BUCKET_NAME,
Prefix: &prefix,
Delimiter: &delimiter, // limits the search to directories (they have a trailing slash)
}
var resp *s3.ListObjectsV2Output
if resp, err = d.s3Client.ListObjectsV2(params); err != nil {
return stripNewlines(err)
}
// prefix of "" is a special case, the root directory of a bucket which can have zero objects
if prefix == "" {
return nil
}
if *resp.KeyCount == 0 {
return errors.New("No such directory: " + directory)
}
return nil
}
func (d *S3Driver) MakeDirectory(cc server.ClientContext, directory string) error {
var err error
var s3Key string
if s3Key, err = d.getS3Key(directory + "/"); err != nil {
return err
}
if directory == "" || directory == "/" || s3Key == "" {
return fmt.Errorf("Cannot mkdir on: %s", directory)
}
var parentKey string
if parentKey, err = d.getS3Key(filepath.Join(directory, "../") + "/"); err != nil {
return err
}
var parentExists bool
if parentExists, err = d.isS3Dir(parentKey); err != nil {
return err
}
if !parentExists {
return fmt.Errorf("Directory '%s' has non-existent parent directory: %s", directory, parentKey)
}
params := &s3.PutObjectInput{
Bucket: &S3_BUCKET_NAME,
Key: &s3Key,
Body: bytes.NewReader([]byte("")),
}
if _, err = d.s3Client.PutObject(params); err != nil {
return stripNewlines(err)
}
return nil
}
// AWS errors may include newlines that interfere with FTP commands so strip them out
func stripNewlines(err error) error {
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
strippedMessage := strings.Replace(awsErr.Message(), "\n", "", -1)
return awserr.New(awsErr.Code(), strippedMessage, awsErr.OrigErr())
} else {
return err
}
}
return nil
}
func (d *S3Driver) ListFiles(cc server.ClientContext) ([]os.FileInfo, error) {
var err error
var prefix string
if prefix, err = d.getS3Key(cc.Path()); err != nil {
return nil, err
}
// all dirs in S3 apart from root dir should end with /. Non standard between ftp clients.
if len(prefix) > 0 && !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
delimiter := "/" // delimiter keeps the listing from being recursive
params := &s3.ListObjectsV2Input{
Bucket: &S3_BUCKET_NAME,
Prefix: &prefix,
Delimiter: &delimiter,
}
files := []os.FileInfo{}
for {
var resp *s3.ListObjectsV2Output
if resp, err = d.s3Client.ListObjectsV2(params); err != nil {
return nil, stripNewlines(err)
}
// directories other than CWD
for _, dir := range resp.CommonPrefixes {
relKey := strings.Replace(*dir.Prefix, d.rootPrefix, "", 1)
var dirInfo os.FileInfo
if dirInfo, err = d.GetFileInfo(cc, relKey); err != nil {
return nil, err
}
files = append(files, dirInfo)
}
// files and CWD
for _, f := range resp.Contents {
// don't list CWD in the list of files
if *f.Key == prefix {
continue
}
relKey := strings.Replace(*f.Key, d.rootPrefix, "", 1)
var fi os.FileInfo
if fi, err = d.getFakeFileInfo(relKey, *f.Size, *f.LastModified); err != nil {
return nil, err
}
files = append(files, fi)
}
if resp.IsTruncated == nil || !*resp.IsTruncated {
break
}
params.ContinuationToken = resp.NextContinuationToken
}
return files, nil
}
func (d *S3Driver) UserLeft(cc server.ClientContext) {
}
func (d *S3Driver) OpenFile(cc server.ClientContext, path string, flag int) (server.FileStream, error) {
// our implementation uses an interface that mimics a file but uploads/downloads to S3
var err error
var s3file *S3VirtualFile
var s3key string
var parentExists bool
if s3key, err = d.getS3Key(path); err != nil {
return nil, err
}
if parentExists, err = d.parentExists(s3key); err != nil {
return nil, err
}
if !parentExists {
return nil, fmt.Errorf("Path has non-existent parent directory: %s", path)
}
if s3file, err = NewS3VirtualFile(s3key, flag, d.s3Session, d.s3Client); err != nil {
return nil, err
}
return s3file, nil
}
func (d *S3Driver) getObjectInfo(key string) (*s3.GetObjectOutput, error) {
params := &s3.GetObjectInput{
Bucket: &S3_BUCKET_NAME,
Key: &key,
}
req, resp := d.s3Client.GetObjectRequest(params)
if err := req.Send(); err != nil {
return nil, stripNewlines(err)
}
return resp, nil
}
func (d *S3Driver) GetFileInfo(cc server.ClientContext, path string) (os.FileInfo, error) {
var err error
relPath := path
if relPath, err = d.getS3Key(path); err != nil {
return nil, err
}
var resp *s3.GetObjectOutput
// check for directories (trailing slashes) if we can't find the file
if resp, err = d.getObjectInfo(relPath); err != nil {
if resp, err = d.getObjectInfo(relPath + "/"); err != nil {
return nil, err
} else {
relPath += "/"
}
}
// resp itself, resp.ContentLength and resp.LastModified are sometimes nil (!) so check for this state. Aws!
if resp == nil {
return nil, fmt.Errorf("Error getting file info for key: %s", relPath)
}
var objectSize int64
if resp.ContentLength != nil {
objectSize = *resp.ContentLength
if strings.HasSuffix(relPath, "/") {
// the size of a directory, just faking it.
objectSize = 4096
}
}
var modTime time.Time
if resp.LastModified != nil {
modTime = *resp.LastModified
}
var f os.FileInfo
if f, err = d.getFakeFileInfo(relPath, objectSize, modTime); err != nil {
return nil, err
}
return f, nil
}
func (d *S3Driver) CanAllocate(cc server.ClientContext, size int) (bool, error) {
return true, nil
}
func (d *S3Driver) ChmodFile(cc server.ClientContext, path string, mode os.FileMode) error {
return errors.New("ChmodFile not implemented")
}
func (d *S3Driver) DeleteFile(cc server.ClientContext, path string) error {
// list objects matching the path, then use DeleteObjects on all of them. Needed because you must delete all
// child key/objects belonging to a directory key before deleting that key
var err error
var relPath string
if relPath, err = d.getS3Key(path); err != nil {
return err
}
var isDir bool
if isDir, err = d.isS3Dir(relPath); err != nil {
return err
}
if isDir && !strings.HasSuffix(relPath, "/") {
relPath += "/"
}
listParams := &s3.ListObjectsV2Input{
Bucket: &S3_BUCKET_NAME,
Prefix: &relPath,
//Delimiter: &delimiter, // empty delim makes this recursive
}
var delObjects []*s3.ObjectIdentifier
for {
var resp *s3.ListObjectsV2Output
if resp, err = d.s3Client.ListObjectsV2(listParams); err != nil {
return stripNewlines(err)
}
for _, f := range resp.Contents {
delObjects = append(delObjects, &s3.ObjectIdentifier{Key: f.Key})
}
// AWS using pointers to bools (?!) so need to check for nil
if resp.IsTruncated == nil || !*resp.IsTruncated {
break
}
listParams.ContinuationToken = resp.NextContinuationToken
}
if len(delObjects) == 0 {
return fmt.Errorf("No such file or directory: %s [S3 key: %s]", path, relPath)
}
delParams := &s3.DeleteObjectsInput{
Bucket: &S3_BUCKET_NAME,
Delete: &s3.Delete{Objects: delObjects},
}
if _, err = d.s3Client.DeleteObjects(delParams); err != nil {
return stripNewlines(err)
}
return nil
}
// returns true or false depending on if the path exists as a file (no trailing slash) or a directory (trailing slash)
// in S3.
func (d *S3Driver) isS3Dir(s3Key string) (bool, error) {
var err error
// clean strips any trailing slashes off
s3Key = filepath.Clean(s3Key)
// see if the key exists as a directory and fallback to checking if it's a file
_, err = d.getObjectInfo(s3Key + "/")
if err == nil {
return true, nil
}
_, err = d.getObjectInfo(s3Key)
if err == nil {
return false, nil
}
return false, fmt.Errorf("No such file or directory: %s", s3Key)
}
func (d *S3Driver) RenameFile(cc server.ClientContext, from, to string) error {
// S3 doesn't have rename (or move). We're copying all objects that match the input file or directory key
// to the new key name
var err error
var relFrom, relTo string
if relFrom, err = d.getS3Key(from); err != nil {
return err
}
if relTo, err = d.getS3Key(to); err != nil {
return err
}
// sanity checks
if relFrom == "" || relTo == "" {
return errors.New("Cannot rename to or from the root directory")
}
if relFrom == relTo {
return errors.New("Cannot rename a directory to itself")
}
var isDir bool
if isDir, err = d.isS3Dir(relFrom); err != nil {
return err
}
if isDir {
// if source is a dir then so is dest
if !strings.HasSuffix(relFrom, "/") {
relFrom += "/"
}
if !strings.HasSuffix(relTo, "/") {
relTo += "/"
}
}
// check for missing parent directories in destination
var parentDir string
if parentDir, err = d.getS3Key(filepath.Dir(to)); err != nil {
return err
}
if _, err = d.getObjectInfo(parentDir + "/"); err != nil && len(parentDir) > 0 {
return fmt.Errorf("Parent directory of destination does not exist: %s. err: %s", parentDir, err)
}
listParams := &s3.ListObjectsV2Input{
Bucket: &S3_BUCKET_NAME,
Prefix: &relFrom,
}
var srcObjects []*s3.ObjectIdentifier
for {
var resp *s3.ListObjectsV2Output
if resp, err = d.s3Client.ListObjectsV2(listParams); err != nil {
return stripNewlines(err)
}
for _, f := range resp.Contents {
srcObjects = append(srcObjects, &s3.ObjectIdentifier{Key: f.Key})
}
// AWS using pointers to bools (?!) so need to check for nil
if resp.IsTruncated == nil || !*resp.IsTruncated {
break
}
listParams.ContinuationToken = resp.NextContinuationToken
}
if len(srcObjects) == 0 {
return fmt.Errorf("Zero files matching pattern:%s", from)
}
// copy all destinations objects from source to dest (already ordered from top level directory key)
for _, objId := range srcObjects {
toPath := strings.Replace(*objId.Key, relFrom, relTo, 1)
copySrc := S3_BUCKET_NAME + "/" + *objId.Key
copyParams := &s3.CopyObjectInput{
Bucket: &S3_BUCKET_NAME,
Key: &toPath,
CopySource: ©Src,
}
if _, err = d.s3Client.CopyObject(copyParams); err != nil {
return stripNewlines(err)
}
}
// delete original file (or nested directory of matching keys). Faster than looping over them.
delParams := &s3.DeleteObjectsInput{
Bucket: &S3_BUCKET_NAME,
Delete: &s3.Delete{Objects: srcObjects},
}
if _, err = d.s3Client.DeleteObjects(delParams); err != nil {
return stripNewlines(err)
}
return nil
}
func (d *S3Driver) GetSettings() *server.Settings {
config := server.Settings{
ListenHost: "0.0.0.0",
ListenPort: d.ftpPort,
MaxConnections: 300,
}
return &config
}
// Return a fakeInfo struct that satisfies the os.FileInfo interface, emulating a file from an S3 object
func (d *S3Driver) getFakeFileInfo(name string, size int64, modTime time.Time) (os.FileInfo, error) {
isDir := strings.HasSuffix(name, "/")
mode := os.FileMode(0666)
if isDir {
mode = mode | os.ModeDir
}
f := fakeInfo{
name: filepath.Base(name),
size: size,
mode: mode,
modTime: modTime,
isDir: isDir,
}
return f, nil
}
// check that any parent directories (S3 keys) exist in path. S3 misbehaves when a child has missing parent directories.
func (d *S3Driver) parentExists(s3Key string) (bool, error) {
var err error
var dir string
dir = filepath.Dir(s3Key) + "/"
if len(dir) > 0 && dir != "." && dir != "/" {
var isDir bool
// ignore errors (eg: no directory)
if isDir, _ = d.isS3Dir(dir); err != nil {
return false, nil
}
return isDir, nil
}
return true, nil
}
// cleans the input path and queries S3 to see if it's a directory or file key. Will return an error if it cannot find
// either a file or directory key (with a trailing slash)
func (d *S3Driver) getS3Key(path string) (string, error) {
var err error
s3Key := path
// join paths and keep relative to root dir.
if strings.HasPrefix(s3Key, "/") {
if s3Key, err = filepath.Rel("/", path); err != nil {
return "", err
}
}
// "/" relative to "/" is "." but in S3 this should be ""
if s3Key == "." || s3Key == "./" {
s3Key = ""
}
if d.rootPrefix != "" {
s3Key = d.rootPrefix + s3Key
}
// directories in S3 must always have a trailing slash (filepath.Rel removes this)
if strings.HasSuffix(path, "/") && len(s3Key) > 0 && !strings.HasSuffix(s3Key, "/") {
s3Key += "/"
}
return s3Key, nil
}
func NewS3Driver(s3Session *session.Session, s3BucketName, rootPrefix string, ftpPort int, ftpUser, ftpPasswd string) *S3Driver {
var client *s3.S3
if s3Session != nil {
client = s3.New(s3Session)
}
driver := &S3Driver{
s3Client: client,
s3Session: s3Session,
s3BucketName: s3BucketName,
rootPrefix: rootPrefix,
ftpPort: ftpPort,
ftpUser: ftpUser,
ftpPasswd: ftpPasswd,
}
return driver
}