-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
188 lines (174 loc) · 3.62 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
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
package sc
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime"
"net/url"
"path"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
type S3KeyValue struct {
defaultBucket, prefix string
returnReadCloser bool
svc *s3.S3
}
type S3Reference struct {
Bucket, Key string
Public bool
}
func (o S3Reference) URI() *url.URL {
var u url.URL
u.Scheme = "s3"
u.Host = o.Bucket
u.Path = o.Key
return &u
}
func (o S3Reference) String() string {
buf, _ := json.Marshal(o)
return string(buf)
}
func NewS3KeyValue(defaultBucket, prefix string, returnReadCloser bool, svc *s3.S3) (*S3KeyValue, error) {
if defaultBucket == "" {
return nil, fmt.Errorf("needs bucket")
}
return &S3KeyValue{
defaultBucket: defaultBucket,
prefix: prefix,
returnReadCloser: returnReadCloser,
svc: svc,
}, nil
}
func removeLeadingSlashes(p string) string {
for {
if strings.HasPrefix(p, "/") {
p = p[1:]
continue
}
break
}
return p
}
func (fs S3KeyValue) s3ref(r Reference) (*S3Reference, error) {
switch t := r.(type) {
case S3Reference:
return &t, nil
case *S3Reference:
return t, nil
default:
return fs.parseS3Reference(r)
}
}
func (fs S3KeyValue) parseS3Reference(r Reference) (*S3Reference, error) {
u := r.URI()
var s3ref S3Reference
if strings.ToLower(u.Scheme) == "s3" && u.Host != "" {
s3ref.Bucket = u.Host
} else {
s3ref.Bucket = fs.defaultBucket
}
var key string
if u.Path == "" {
er, err := encode(r)
if err != nil {
return nil, err
}
key = er.URI().String()
} else {
key = removeLeadingSlashes(u.Path)
}
s3ref.Key = path.Join(fs.prefix, key)
return &s3ref, nil
}
func (fs S3KeyValue) Get(r Reference) (interface{}, error) {
s3ref, err := fs.s3ref(r)
if err != nil {
return nil, err
}
resp, err := fs.svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s3ref.Bucket),
Key: aws.String(s3ref.Key),
})
if err != nil {
return nil, wrapNotFound(r, err)
}
if fs.returnReadCloser {
return resp.Body, nil
}
defer resp.Body.Close()
w := new(bytes.Buffer)
if _, err := io.Copy(w, resp.Body); err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (fs S3KeyValue) Put(r Reference, i interface{}) error {
s3ref, err := fs.s3ref(r)
if err != nil {
return err
}
var rs io.ReadSeeker
cp := func(r io.Reader) error {
w := new(bytes.Buffer)
if _, err := io.Copy(w, r); err != nil {
return err
}
rs = bytes.NewReader(w.Bytes())
return nil
}
switch t := i.(type) {
case string:
rs = strings.NewReader(t)
case []byte:
rs = bytes.NewReader(t)
case fmt.Stringer:
rs = strings.NewReader(t.String())
case io.ReadSeeker:
rs = t
case io.Reader:
if err := cp(t); err != nil {
return err
}
case io.ReadCloser:
defer t.Close()
if err := cp(t); err != nil {
return err
}
default:
return fmt.Errorf("don't know how to handle object type %T", t)
}
poi := s3.PutObjectInput{
Bucket: aws.String(s3ref.Bucket),
Key: aws.String(s3ref.Key),
Body: rs,
}
if mt := mime.TypeByExtension(path.Ext(s3ref.Key)); mt != "" {
poi.ContentType = aws.String(mt)
}
if s3ref.Public {
poi.ACL = aws.String("public-read")
}
if _, err := fs.svc.PutObject(&poi); err != nil {
return err
}
return nil
}
func (fs S3KeyValue) Merge(r Reference, i interface{}) error {
return unimplemented(fs, "Merge")
}
func (fs S3KeyValue) Delete(r Reference) error {
s3ref, err := fs.s3ref(r)
if err != nil {
return err
}
if _, err := fs.svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(s3ref.Bucket),
Key: aws.String(s3ref.Key),
}); err != nil {
return err
}
return nil
}