-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
150 lines (118 loc) · 3.04 KB
/
s3.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
package drivers
import (
"fmt"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"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/aws/aws-sdk-go/service/sts"
"github.com/sirupsen/logrus"
)
func init() {
AddDriver("s3", &S3Driver{})
}
type S3Driver struct {
BaseDriver
bucket string
}
func (d *S3Driver) getSession() *session.Session {
return session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
}
func (d *S3Driver) Init() error {
d.bucket = os.Getenv("GRB_S3_BUCKET")
if d.bucket == "" {
return fmt.Errorf("you need to set 'GRB_S3_BUCKET' for a target bucket")
}
svc := sts.New(d.getSession())
identity, err := svc.GetCallerIdentity(&sts.GetCallerIdentityInput{})
logrus.Debugf("Connected to s3 as %s", identity.String())
return err
}
func (d *S3Driver) ListDirs(path string) ([]string, error) {
res := []string{}
if !strings.HasSuffix(path, "/") {
path += "/"
}
items, err := d.listRaw(path)
if err != nil {
return res, err
}
return s3ItemsToFolders(path, items), err
}
func s3ItemsToFolders(path string, items []string) []string {
if !strings.HasSuffix(path, "/") {
path += "/"
}
res := []string{}
for _, i := range items {
res = append(res, strings.Split(strings.TrimPrefix(i, path), "/")[0])
}
return removeDuplicateStr(res)
}
func removeDuplicateStr(strSlice []string) []string {
allKeys := make(map[string]bool)
list := []string{}
for _, item := range strSlice {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}
func (d *S3Driver) listRaw(path string) ([]string, error) {
res := []string{}
svc := s3.New(d.getSession())
err := svc.ListObjectsV2Pages(&s3.ListObjectsV2Input{
Bucket: aws.String(d.bucket),
Prefix: aws.String(path),
MaxKeys: aws.Int64(20),
}, func(page *s3.ListObjectsV2Output, lastPage bool) bool {
for _, i := range page.Contents {
res = append(res, *i.Key)
}
return true
})
logrus.Tracef("Listing %s:%s -> %v", d.bucket, path, res)
return res, err
}
func (d *S3Driver) Mkdir(path string) error {
// Not needed in s3
return nil
}
func (d *S3Driver) Delete(src string) error {
items, err := d.listRaw(src)
if err != nil {
return err
}
svc := s3.New(d.getSession())
for _, item := range items {
logrus.Tracef("Deleting %s:%s", d.bucket, item)
if _, err := svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(d.bucket),
Key: aws.String(item),
}); err != nil {
return err
}
}
return err
}
func (d *S3Driver) Copy(src, dst string) (int64, error) {
uploader := s3manager.NewUploader(d.getSession())
f, err := os.Open(src)
if err != nil {
return 0, fmt.Errorf("failed to open file %q, %v", src, err)
}
logrus.Tracef("Uploading %s to %s:%s", src, d.bucket, dst)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(d.bucket),
Key: aws.String(dst),
Body: f,
})
info, _ := f.Stat()
return info.Size(), err
}