-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
687 lines (585 loc) · 14.3 KB
/
upload.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
package rest
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"sort"
"strings"
"sync"
"time"
)
type UploadInfo struct {
// generic
put string
cmpl string
ctx context.Context
spot SpotClient
MaxPartSize int64 // maximum size of a single part in MB, defaults to 1024 (1GB)
ParallelUploads int // number of parallel uploads to perform (defaults to 3)
// put upload
blocksize int64
// aws upload
awsid string
awskey string
awsregion string
awsname string
awshost string
awsuploadid string // used during upload
awstags []string
awstagsLk sync.Mutex
}
type uploadAuth struct {
Authorization string `json:"authorization"`
}
type uploadAwsResp struct {
Bucket string
Key string
UploadId string
}
func SpotUpload(ctx context.Context, client SpotClient, req, method string, param Param, f io.Reader, mimeType string) (*Response, error) {
var upinfo map[string]any
err := SpotApply(ctx, client, req, method, param, &upinfo)
if err != nil {
return nil, fmt.Errorf("initial upload query failed: %w", err)
}
up, err := PrepareUpload(upinfo)
if err != nil {
return nil, fmt.Errorf("upload prepare failed: %w", err)
}
up.spot = client
ln := int64(-1)
if fs, ok := f.(io.Seeker); ok {
ln, err = fs.Seek(0, io.SeekEnd)
if err != nil {
// seek failed, let's continue in the unknown
ln = -1
} else {
// seek back to the start
fs.Seek(0, io.SeekStart)
}
}
return up.Do(ctx, f, mimeType, ln)
}
func Upload(ctx context.Context, req, method string, param Param, f io.Reader, mimeType string) (*Response, error) {
var upinfo map[string]any
err := Apply(ctx, req, method, param, &upinfo)
if err != nil {
return nil, fmt.Errorf("initial upload query failed: %w", err)
}
up, err := PrepareUpload(upinfo)
if err != nil {
return nil, fmt.Errorf("upload prepare failed: %w", err)
}
ln := int64(-1)
if fs, ok := f.(io.Seeker); ok {
ln, err = fs.Seek(0, io.SeekEnd)
if err != nil {
// seek failed, let's continue in the unknown
ln = -1
} else {
// seek back to the start
fs.Seek(0, io.SeekStart)
}
}
return up.Do(ctx, f, mimeType, ln)
}
// upload for platform files
func PrepareUpload(req map[string]any) (*UploadInfo, error) {
// we have the following parameters:
// * PUT (url to put to)
// * Complete (APÏ to call upon completion)
// we optionally support multipart upload for images over 5GB through extra parameters
up := &UploadInfo{
MaxPartSize: 1024,
ParallelUploads: 3,
}
if err := up.parse(req); err != nil {
return nil, err
}
return up, nil
}
func (u *UploadInfo) String() string {
return u.put
}
func (u *UploadInfo) parse(req map[string]any) error {
var ok bool
//log.Printf("parsing upload response: %+v", req)
// strict minimum: PUT & Complete
u.put, ok = req["PUT"].(string)
if !ok {
return errors.New("required parameter PUT not found")
}
u.cmpl, ok = req["Complete"].(string)
if !ok {
return errors.New("required parameter Complete not found")
}
// vars we care about:
// * Cloud_Aws_Bucket_Upload__
// * Key
// * Bucket_Endpoint.Region
// * Bucket_Endpoint.Name
// * Bucket_Endpoint.Host
// if we can't grab any of these, drop the whole thing and not set u.awsid so it won't be used
id, ok := req["Cloud_Aws_Bucket_Upload__"].(string)
if !ok {
// no id, but we don't care
if bs, ok := req["Blocksize"].(float64); ok {
// we got a blocksize, this uses the new upload method
u.blocksize = int64(bs)
return nil
}
return nil
}
bucket, ok := req["Bucket_Endpoint"].(map[string]any)
if !ok {
return nil
}
u.awskey, ok = req["Key"].(string)
if !ok {
return nil
}
u.awsregion, ok = bucket["Region"].(string)
if !ok {
return nil
}
u.awsname = bucket["Name"].(string)
if !ok {
return nil
}
u.awshost = bucket["Host"].(string)
if !ok {
return nil
}
// all ok, set awsid
u.awsid = id
return nil
}
func (u *UploadInfo) Do(ctx context.Context, f io.Reader, mimeType string, ln int64) (*Response, error) {
u.ctx = ctx
if u.blocksize > 0 {
return u.partUpload(f, mimeType)
}
if u.awsid != "" {
if ln == -1 || ln > 64*1024*1024 {
return u.awsUpload(f, mimeType)
}
}
if ln == -1 || ln > 5*1024*1024*1024 {
return nil, errors.New("cannot upload using PUT method without a known length of less than 5GB")
}
if ln == 0 {
// workaround bug with go http client when ContentLength it set to zero
f = bytes.NewReader([]byte{})
}
// we can use simple PUT
req, err := http.NewRequestWithContext(ctx, http.MethodPut, u.put, f)
if err != nil {
return nil, err
}
req.ContentLength = ln
req.Header.Set("Content-Type", mimeType)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close() // avoid leaking stuff
// read full response, discard (ensures upload completed)
io.Copy(ioutil.Discard, resp.Body)
return u.complete()
}
func (u *UploadInfo) complete() (*Response, error) {
if u.spot != nil {
return SpotDo(u.ctx, u.spot, u.cmpl, "POST", map[string]any{})
}
return Do(u.ctx, u.cmpl, "POST", map[string]any{})
}
func (u *UploadInfo) partUpload(f io.Reader, mimeType string) (*Response, error) {
// partUpload works similar to awsUpload but when uploading to the new kind of PUT server
// let's upload
partNo := 0
errCh := make(chan error, 2) // enough just in case
nwg := newNWG()
eof := false
for !eof {
nwg.Wait(u.ParallelUploads - 1)
partNo += 1
readCh := make(chan error)
nwg.Add(1)
go u.partUploadPart(f, mimeType, partNo, readCh, errCh, nwg)
select {
case err := <-readCh:
if err == io.EOF {
eof = true
} else if err != nil {
// fatal error
return nil, err
}
case err := <-errCh:
// fatal error
return nil, err
}
}
// wait for nwg completion
go func() {
nwg.Wait(0)
// send "no error"
select {
case errCh <- nil:
default:
// do not wait if send fails
}
}()
// read & check error (cause waiting for completion)
err := <-errCh
if err != nil {
// fatal error
return nil, err
}
// finalize
return u.complete()
}
func (u *UploadInfo) partUploadPart(f io.Reader, mimeType string, partNo int, readCh, errCh chan<- error, nwg *numeralWaitGroup) {
// prepare to upload a part
defer nwg.Done()
// we use temp files as to avoid using too much memory
tmpf, err := ioutil.TempFile("", "upload*.bin")
if err != nil {
// failed to create temp file
readCh <- err
return
}
// cleanup
defer func() {
tmpf.Close()
os.Remove(tmpf.Name())
}()
n, err := io.CopyN(tmpf, f, u.blocksize)
if err != nil {
if err != io.EOF {
// fatal error
errCh <- err
return
}
readCh <- err
if n == 0 {
return
}
} else if n == 0 {
// no data to upload, just return EOF
readCh <- io.EOF
return
} else {
// end of read
readCh <- nil
}
// rewind tmpf
tmpf.Seek(0, io.SeekStart)
// we can use simple PUT
req, err := http.NewRequestWithContext(u.ctx, http.MethodPut, u.put, tmpf)
if err != nil {
select {
case errCh <- err:
default:
}
return
}
start := int64(partNo-1) * u.blocksize
end := start + n - 1 // inclusive
req.ContentLength = n // from io.CopyN
req.Header.Set("Content-Type", mimeType)
req.Header.Set("Content-Range", fmt.Sprintf("bytes %d-%d/*", start, end))
// perform upload
resp, err := http.DefaultClient.Do(req)
if err != nil {
select {
case errCh <- err:
default:
}
return
}
defer resp.Body.Close() // avoid leaking stuff
// read full response, discard (ensures upload completed)
_, err = io.Copy(ioutil.Discard, resp.Body)
if err != nil {
select {
case errCh <- err:
default:
}
return
}
}
func (u *UploadInfo) awsUpload(f io.Reader, mimeType string) (*Response, error) {
// awsUpload is a magic method that does not need to know upload length as it will split file into manageable sized pieces.
err := u.awsInit(mimeType)
if err != nil {
return nil, err
}
// let's upload
partNo := 0
errCh := make(chan error, 2) // enough just in case
nwg := newNWG()
eof := false
for !eof {
nwg.Wait(u.ParallelUploads - 1)
partNo += 1
readCh := make(chan error)
nwg.Add(1)
go u.awsUploadPart(f, partNo, readCh, errCh, nwg)
select {
case err := <-readCh:
if err == io.EOF {
eof = true
} else if err != nil {
// fatal error, give up
u.awsAbort()
return nil, err
}
case err := <-errCh:
// fatal error, give up
u.awsAbort()
return nil, err
}
}
// wait for nwg completion
go func() {
nwg.Wait(0)
// send "no error"
select {
case errCh <- nil:
default:
// do not wait if send fails
}
}()
// read & check error (cause waiting for completion)
err = <-errCh
if err != nil {
// fatal error
u.awsAbort()
return nil, err
}
// finalize
err = u.awsFinalize()
if err != nil {
return nil, err
}
return u.complete()
}
func (u *UploadInfo) awsFinalize() error {
// see https://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadComplete.html
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "<CompleteMultipartUpload>")
for n, tag := range u.awstags {
fmt.Fprintf(buf, "<Part><PartNumber>%d</PartNumber><ETag>%s</ETag></Part>", n+1, tag)
}
fmt.Fprintf(buf, "</CompleteMultipartUpload>")
resp, err := u.awsReq("POST", "uploadId="+u.awsuploadid, bytes.NewReader(buf.Bytes()), http.Header{"Content-Type": []string{"text/xml"}})
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(ioutil.Discard, resp.Body)
return err
}
func (u *UploadInfo) awsUploadPart(f io.Reader, partNo int, readCh, errCh chan<- error, nwg *numeralWaitGroup) {
// prepare to upload a part
defer nwg.Done()
// maxLen in MB
maxLen := u.MaxPartSize
tmpf, err := ioutil.TempFile("", "upload*.bin")
if err != nil {
// failed to create temp file
readCh <- err
return
}
// cleanup
defer func() {
tmpf.Close()
os.Remove(tmpf.Name())
}()
n, err := io.CopyN(tmpf, f, maxLen*1024*1024)
if err != nil {
if err != io.EOF {
// fatal error
errCh <- err
return
}
readCh <- err
if n == 0 && partNo != 1 {
return
}
} else if n == 0 && partNo != 1 {
// no data to upload, just return EOF unless we are part #1
readCh <- io.EOF
return
} else {
// end of read
readCh <- nil
}
// need to upload to aws
resp, err := u.awsReq("PUT", fmt.Sprintf("partNumber=%d&uploadId=%s", partNo, u.awsuploadid), tmpf, nil)
if err != nil {
select {
case errCh <- err:
default:
}
return
}
defer resp.Body.Close()
_, err = io.Copy(ioutil.Discard, resp.Body)
if err != nil {
select {
case errCh <- err:
default:
}
return
}
// store etag value
u.setTag(partNo, resp.Header.Get("Etag"))
}
func (u *UploadInfo) setTag(partNo int, tag string) {
u.awstagsLk.Lock()
defer u.awstagsLk.Unlock()
pos := partNo - 1
if cap(u.awstags) <= pos {
// need to increase cap
tmp := make([]string, len(u.awstags), cap(u.awstags)+64)
copy(tmp, u.awstags)
u.awstags = tmp
}
if pos >= len(u.awstags) {
u.awstags = u.awstags[:pos+1]
}
u.awstags[pos] = tag
}
func (u *UploadInfo) awsAbort() error {
resp, err := u.awsReq("DELETE", "uploadId="+u.awsuploadid, nil, nil)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(ioutil.Discard, resp.Body)
return err
}
func (u *UploadInfo) awsInit(mimeType string) error {
// see: https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html
resp, err := u.awsReq("POST", "uploads=", nil, http.Header{"Content-Type": []string{mimeType}, "X-Amz-Acl": []string{"private"}})
if err != nil {
return err
}
defer resp.Body.Close()
dec := xml.NewDecoder(resp.Body)
res := &uploadAwsResp{}
err = dec.Decode(res)
if err != nil {
return err
}
if res.UploadId == "" {
return errors.New("failed to read aws upload id")
}
u.awsuploadid = res.UploadId
return nil
}
func (u *UploadInfo) awsReq(method, query string, body io.ReadSeeker, headers http.Header) (*http.Response, error) {
if headers == nil {
headers = http.Header{}
}
// seek at end to know length
var ln int64
if body != nil {
var err error
ln, err = body.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
body.Seek(0, io.SeekStart)
if ln == 0 {
// this will allow stupid http.Request to generate the right headers
body = bytes.NewReader([]byte{})
}
}
// perform aws request using remote signature
var bodyHash string
if ln == 0 {
bodyHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" // sha256("")
} else {
h := sha256.New()
_, err := io.Copy(h, body)
if err != nil {
return nil, err
}
body.Seek(0, io.SeekStart) // reset to beginning
bodyHash = hex.EncodeToString(h.Sum(nil))
}
ts := time.Now().UTC().Format("20060102T150405Z") // amz format
tsD := ts[:8] // YYYYMMDD
headers.Set("X-Amz-Content-Sha256", bodyHash)
headers.Set("X-Amz-Date", ts)
awsAuthStr := []string{
"AWS4-HMAC-SHA256",
ts,
tsD + "/" + u.awsregion + "/s3/aws4_request",
method,
"/" + u.awsname + "/" + u.awskey,
query,
"host:" + u.awshost,
}
// list headers to sign (host and anything starting with x-)
signHead := []string{"host"}
for k, _ := range headers {
s := strings.ToLower(k)
if strings.HasPrefix(s, "x-") {
signHead = append(signHead, s)
}
}
// sort signHead
sort.Strings(signHead)
// add strings
for _, h := range signHead {
if h == "host" {
// already added
continue
}
awsAuthStr = append(awsAuthStr, h+":"+headers.Get(h))
}
awsAuthStr = append(awsAuthStr, "")
awsAuthStr = append(awsAuthStr, strings.Join(signHead, ";"))
awsAuthStr = append(awsAuthStr, bodyHash)
// generate signature
auth := &uploadAuth{}
err := Apply(u.ctx, "Cloud/Aws/Bucket/Upload/"+u.awsid+":signV4", "POST", Param{"headers": strings.Join(awsAuthStr, "\n")}, auth)
if err != nil {
return nil, err
}
headers.Set("Authorization", auth.Authorization)
// perform the query
target := "https://" + u.awshost + "/" + u.awsname + "/" + u.awskey
if query != "" {
target += "?" + query
}
req, err := http.NewRequestWithContext(u.ctx, method, target, body)
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header[k] = v
}
req.ContentLength = ln
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("request failed: %s\ndetails: %s", resp.Status, body)
}
return resp, err
}