-
Notifications
You must be signed in to change notification settings - Fork 46
/
remote_list.go
63 lines (53 loc) · 1.32 KB
/
remote_list.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
func remotePager(config *Config, svc *s3.S3, uri string, delim bool, pager func(page *s3.ListObjectsV2Output)) error {
u, err := FileURINew(uri)
if err != nil || u.Scheme != "s3" {
return fmt.Errorf("requires buckets to be prefixed with s3://")
}
params := &s3.ListObjectsV2Input{
Bucket: aws.String(u.Bucket), // Required
MaxKeys: aws.Int64(1000),
}
if u.Path != "" && u.Path != "/" {
params.Prefix = u.Key()
}
if delim {
params.Delimiter = aws.String("/")
}
wrapper := func(page *s3.ListObjectsV2Output, lastPage bool) bool {
pager(page)
return true
}
if svc == nil {
svc = SessionNew(config)
}
bsvc, err := SessionForBucket(config, u.Bucket)
if err != nil {
return err
}
if err := bsvc.ListObjectsV2Pages(params, wrapper); err != nil {
return err
}
return nil
}
func remoteList(config *Config, svc *s3.S3, args []string) ([]FileObject, error) {
result := make([]FileObject, 0)
for _, arg := range args {
pager := func(page *s3.ListObjectsV2Output) {
for _, obj := range page.Contents {
result = append(result, FileObject{
Name: *obj.Key,
Size: *obj.Size,
Checksum: *obj.ETag,
})
}
}
remotePager(config, svc, arg, false, pager)
}
return result, nil
}