-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy paths3.go
358 lines (284 loc) · 8.07 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
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
package s3
import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"runtime"
"sort"
"strings"
"time"
)
// S3 provides a wrapper around your S3 credentials. It carries no other internal state
// and can be copied freely.
type S3 struct {
bucket string
accessId string
secret string
endpoint string
}
// NewS3 allocates a new S3 with the provided credentials.
func NewS3(bucket, accessId, secret string) *S3 {
return &S3{
bucket: bucket,
accessId: accessId,
secret: secret,
endpoint: fmt.Sprintf("%s.s3.amazonaws.com", bucket),
}
}
func (s3 *S3) signRequest(req *http.Request) {
amzHeaders := ""
resourceUrl, _ := url.Parse("/" + s3.bucket + req.URL.Path)
resource := resourceUrl.String()
/* Ugh, AWS requires us to order the parameters in a specific ordering for
* signing. Makes sense, but is annoying because a map does not have a defined
* ordering (and basically returns elements in a random order) -- so we have
* to sort by hand */
query := req.URL.Query()
if len(query) > 0 {
keys := []string{}
for k := range query {
keys = append(keys, k)
}
sort.Strings(keys)
parts := []string{}
for _, key := range keys {
vals := query[key]
for _, val := range vals {
if val == "" {
parts = append(parts, url.QueryEscape(key))
} else {
part := fmt.Sprintf("%s=%s", url.QueryEscape(key), url.QueryEscape(val))
parts = append(parts, part)
}
}
}
req.URL.RawQuery = strings.Join(parts, "&")
}
if req.URL.RawQuery != "" {
resource += "?" + req.URL.RawQuery
}
if req.Header.Get("Date") == "" {
req.Header.Set("Date", time.Now().Format(time.RFC1123))
}
authStr := strings.Join([]string{
strings.TrimSpace(req.Method),
req.Header.Get("Content-MD5"),
req.Header.Get("Content-Type"),
req.Header.Get("Date"),
amzHeaders + resource,
}, "\n")
h := hmac.New(sha1.New, []byte(s3.secret))
h.Write([]byte(authStr))
h64 := base64.StdEncoding.EncodeToString(h.Sum(nil))
auth := "AWS" + " " + s3.accessId + ":" + h64
req.Header.Set("Authorization", auth)
}
func (s3 *S3) resource(path string, values url.Values) string {
tmp := fmt.Sprintf("https://%s/%s", s3.endpoint, path)
if values != nil {
tmp += "?" + values.Encode()
}
return tmp
}
func (s3 *S3) putMultipart(r io.Reader, size int64, path string, contentType string) (er error) {
mp, er := s3.StartMultipart(path)
if er != nil {
return er
}
defer func() {
if er != nil {
mp.Abort()
}
}()
var chunkSize int64 = 7 * 1024 * 1024
chunk := bytes.NewBuffer(make([]byte, chunkSize))
md5hash := md5.New()
remaining := size
for ; remaining > 0; remaining -= chunkSize {
chunk.Reset()
md5hash.Reset()
if remaining < chunkSize {
chunkSize = remaining
}
wr := io.MultiWriter(chunk, md5hash)
if _, er := io.CopyN(wr, r, chunkSize); er != nil {
return er
}
if er := mp.AddPart(chunk, chunkSize, md5hash.Sum(nil)); er != nil {
return er
}
}
return mp.Complete(contentType)
}
// Put uploads content to S3. The length of r must be passed as size. md5sum optionally contains
// the MD5 hash of the content for end-to-end integrity checking; if omitted no checking is done.
// contentType optionally contains the MIME type to send to S3 as the Content-Type header; when
// files are later accessed, S3 will echo back this in their response headers.
//
// If the passed size exceeds 3GB, the multipart API is used, otherwise the single-request API is used.
// It should be noted that the multipart API uploads in 7MB segments and computes checksums of each
// one -- it does NOT use the passed md5sum, so don't bother with it if you're uploading huge files.
func (s3 *S3) Put(r io.Reader, size int64, path string, md5sum []byte, contentType string) error {
if size > 3*1024*1024*1024 {
return s3.putMultipart(r, size, path, contentType)
}
req, er := http.NewRequest("PUT", s3.resource(path, nil), r)
if er != nil {
return er
}
if md5sum != nil {
md5value := base64.StdEncoding.EncodeToString(md5sum)
req.Header.Set("Content-MD5", md5value)
}
if contentType == "" {
contentType = "application/octet-stream"
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Content-Length", fmt.Sprintf("%d", size))
req.Header.Set("Host", req.URL.Host)
req.ContentLength = size
s3.signRequest(req)
resp, er := http.DefaultClient.Do(req)
if er != nil {
return er
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
er := wrapError(resp)
if newEndpoint := er.newEndpoint(); newEndpoint != "" {
s3.endpoint = newEndpoint
er.ShouldRetry = true
}
return er
}
return nil
}
// Get fetches content from S3, returning both a ReadCloser for the data and the HTTP headers
// returned by S3. You can use the headers to extract the Content-Type that the data was sent
// with.
func (s3 *S3) Get(path string) (io.ReadCloser, http.Header, error) {
req, er := http.NewRequest("GET", s3.resource(path, nil), nil)
if er != nil {
return nil, http.Header{}, er
}
s3.signRequest(req)
resp, er := http.DefaultClient.Do(req)
if er != nil {
return nil, http.Header{}, er
}
if resp.StatusCode != 200 {
er := wrapError(resp)
if newEndpoint := er.newEndpoint(); newEndpoint != "" {
s3.endpoint = newEndpoint
er.ShouldRetry = true
}
return nil, http.Header{}, er
}
return resp.Body, resp.Header, nil
}
// Head is similar to Get, but returns only the response headers. The response body is not
// transferred across the network. This is useful for checking if a file exists remotely,
// and what headers it was configured with.
func (s3 *S3) Head(path string) (http.Header, error) {
req, er := http.NewRequest("HEAD", s3.resource(path, nil), nil)
if er != nil {
return http.Header{}, er
}
s3.signRequest(req)
resp, er := http.DefaultClient.Do(req)
if er != nil {
return http.Header{}, er
}
if resp.StatusCode != 200 {
er := wrapError(resp)
if newEndpoint := er.newEndpoint(); newEndpoint != "" {
s3.endpoint = newEndpoint
er.ShouldRetry = true
}
return http.Header{}, er
}
return resp.Header, nil
}
// Test attempts to write and read back a single, short file from S3. It is intended to be
// used to test runtime configuration to fail quickly when credentials are invalid.
func (s3 *S3) Test() error {
testString := fmt.Sprintf("roundtrip-test-%d", rand.Int())
testReader := strings.NewReader(testString)
if er := s3.Put(testReader, int64(testReader.Len()), "writetest", nil, "text/x-empty"); er != nil {
if s3er, ok := er.(*S3Error); ok {
if s3er.ShouldRetry {
return s3.Test()
}
}
return er
}
actualReader, header, er := s3.Get("writetest")
if er != nil {
if s3er, ok := er.(*S3Error); ok {
if s3er.ShouldRetry {
return s3.Test()
}
}
return er
}
defer actualReader.Close()
actualBytes, er := ioutil.ReadAll(actualReader)
if er != nil {
return er
}
if string(actualBytes) != testString {
return fmt.Errorf("String read back from S3 was different than what we put there.")
}
if header.Get("Content-Type") != "text/x-empty" {
return fmt.Errorf("Content served back from S3 had a different Content-Type than what we put there")
}
return nil
}
// StartMultipart initiates a multipart upload.
func (s3 *S3) StartMultipart(path string) (*S3Multipart, error) {
req, er := http.NewRequest("POST", s3.resource(path, nil)+"?uploads", nil)
if er != nil {
return nil, er
}
req.Header.Set("Host", req.URL.Host)
s3.signRequest(req)
resp, er := http.DefaultClient.Do(req)
if er != nil {
return nil, er
}
defer resp.Body.Close()
xmlBytes, er := ioutil.ReadAll(resp.Body)
if er != nil {
return nil, er
}
if resp.StatusCode != 200 {
er := wrapError(resp)
if newEndpoint := er.newEndpoint(); newEndpoint != "" {
s3.endpoint = newEndpoint
er.ShouldRetry = true
}
return nil, er
}
var xmlResp s3multipartResp
if er := xml.Unmarshal(xmlBytes, &xmlResp); er != nil {
return nil, er
}
mp := &S3Multipart{
uploadId: xmlResp.UploadId,
key: xmlResp.Key,
s3: s3,
}
runtime.SetFinalizer(mp, func(mp *S3Multipart) {
mp.Abort()
})
return mp, nil
}