-
Notifications
You must be signed in to change notification settings - Fork 355
/
upload.go
458 lines (416 loc) · 14 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
// Package helpers provide useful wrappers for clients using the lakeFS OpenAPI.
package helpers
import (
"context"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"path/filepath"
"strings"
"github.com/go-openapi/swag"
"github.com/treeverse/lakefs/pkg/api/apigen"
"github.com/treeverse/lakefs/pkg/api/apiutil"
"github.com/treeverse/lakefs/pkg/httputil"
"golang.org/x/sync/errgroup"
)
// MaxUploadParts is the maximum allowed number of parts in a multipart upload
const MaxUploadParts int32 = 10000
// MinUploadPartSize is the minimum allowed part size when uploading a part
const MinUploadPartSize int64 = 1024 * 1024 * 5
// DefaultUploadPartSize is the default part size to buffer chunks of a payload into
const DefaultUploadPartSize = MinUploadPartSize
// DefaultUploadConcurrency is the default number of goroutines to spin up when uploading a multipart upload
const DefaultUploadConcurrency = 5
// ClientUpload uploads contents as a file via lakeFS
func ClientUpload(ctx context.Context, client apigen.ClientWithResponsesInterface, repoID, branchID, objPath string, metadata map[string]string, contentType string, contents io.ReadSeeker) (*apigen.ObjectStats, error) {
pr, pw := io.Pipe()
defer func() {
_ = pr.Close()
}()
mpw := multipart.NewWriter(pw)
mpContentType := mpw.FormDataContentType()
go func() {
defer func() {
_ = mpw.Close()
_ = pw.Close()
}()
filename := filepath.Base(objPath)
const fieldName = "content"
var err error
var cw io.Writer
// when no content-type is specified, we let 'CreateFromFile' add the part with the default content type.
// otherwise, we add a part and set the content-type.
if contentType != "" {
h := make(textproto.MIMEHeader)
contentDisposition := mime.FormatMediaType("form-data", map[string]string{"name": fieldName, "filename": filename})
h.Set("Content-Disposition", contentDisposition)
h.Set("Content-Type", contentType)
cw, err = mpw.CreatePart(h)
} else {
cw, err = mpw.CreateFormFile(fieldName, filename)
}
if err != nil {
_ = pw.CloseWithError(err)
return
}
if _, err := io.Copy(cw, contents); err != nil {
_ = pw.CloseWithError(err)
return
}
}()
resp, err := client.UploadObjectWithBodyWithResponse(ctx, repoID, branchID, &apigen.UploadObjectParams{
Path: objPath,
}, mpContentType, pr, func(ctx context.Context, req *http.Request) error {
var metaKey string
for k, v := range metadata {
lowerKey := strings.ToLower(k)
if strings.HasPrefix(lowerKey, apiutil.LakeFSMetadataPrefix) {
metaKey = apiutil.LakeFSHeaderInternalPrefix + lowerKey[len(apiutil.LakeFSMetadataPrefix):]
} else {
metaKey = apiutil.LakeFSHeaderMetadataPrefix + lowerKey
}
req.Header.Set(metaKey, v)
}
return nil
})
if err != nil {
return nil, err
}
if resp.JSON201 == nil {
return nil, ResponseAsError(resp)
}
return resp.JSON201, nil
}
// PreSignUploader uploads contents as a file via lakeFS using presigned urls
// It supports both multipart and single part uploads.
type PreSignUploader struct {
Concurrency int
HTTPClient *http.Client
Client apigen.ClientWithResponsesInterface
MultipartSupport bool
}
// presignUpload represents a single upload request
type presignUpload struct {
uploader PreSignUploader
repoID string
branchID string
objectPath string
metadata map[string]string
contentType string
reader io.ReadSeeker
readerAt io.ReaderAt
size int64
partSize int64
numParts int
}
func NewPreSignUploader(client apigen.ClientWithResponsesInterface, multipartSupport bool) *PreSignUploader {
return &PreSignUploader{
Concurrency: DefaultUploadConcurrency,
HTTPClient: http.DefaultClient,
Client: client,
MultipartSupport: multipartSupport,
}
}
func (u *PreSignUploader) Upload(ctx context.Context, repoID string, branchID string, objPath string, content io.ReadSeeker,
contentType string, metadata map[string]string,
) (*apigen.ObjectStats, error) {
// calculate size and rewind content
size, err := content.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
if _, seekErr := content.Seek(0, io.SeekStart); seekErr != nil {
return nil, seekErr
}
// check if content implements io.ReaderAt for multipart upload
readerAt, _ := content.(io.ReaderAt)
// create upload object - represents a single upload request
upload := &presignUpload{
uploader: *u,
repoID: repoID,
branchID: branchID,
objectPath: objPath,
metadata: metadata,
contentType: contentType,
reader: content,
readerAt: readerAt,
size: size,
}
return upload.Upload(ctx)
}
type presignPartReader struct {
Reader *io.SectionReader
URL string
}
func (u *presignUpload) uploadMultipart(ctx context.Context) (*apigen.ObjectStats, error) {
mpu, err := u.initMultipart(ctx)
if err != nil {
return nil, err
}
// prepare readers
parts := make([]presignPartReader, u.numParts)
rem := u.size
off := int64(0)
for i := range parts {
size := min(u.partSize, rem)
parts[i].Reader = io.NewSectionReader(u.readerAt, off, size) // use `readerAt`
parts[i].URL = (*mpu.PresignedUrls)[i]
rem -= size
off += size
}
// upload parts in parallel, fill uploadParts array with results
uploadParts := make([]apigen.UploadPart, u.numParts)
g, grpCtx := errgroup.WithContext(context.Background())
g.SetLimit(u.uploader.Concurrency)
for i := 0; i < u.numParts; i++ {
i := i // pinning
g.Go(func() error {
etag, err := u.uploadPart(grpCtx, parts[i].Reader, parts[i].URL)
if err != nil {
return fmt.Errorf("part %d %w", i+1, err)
}
uploadParts[i] = apigen.UploadPart{
PartNumber: i + 1,
Etag: etag,
}
return nil
})
}
// wait for all parts to be uploaded
if err := g.Wait(); err != nil {
// abort upload using a new context to avoid context cancellation
abortErr := u.abortMultipart(context.Background(), mpu)
if abortErr != nil {
err = errors.Join(err, abortErr)
}
return nil, err
}
return u.completeMultipart(ctx, mpu, uploadParts)
}
func (u *presignUpload) completeMultipart(ctx context.Context, mpu *apigen.PresignMultipartUpload, uploadParts []apigen.UploadPart) (*apigen.ObjectStats, error) {
resp, err := u.uploader.Client.CompletePresignMultipartUploadWithResponse(ctx, u.repoID, u.branchID, mpu.UploadId,
&apigen.CompletePresignMultipartUploadParams{
Path: u.objectPath,
},
apigen.CompletePresignMultipartUploadJSONRequestBody{
Parts: uploadParts,
PhysicalAddress: mpu.PhysicalAddress,
UserMetadata: &apigen.CompletePresignMultipartUpload_UserMetadata{
AdditionalProperties: u.metadata,
},
ContentType: apiutil.Ptr(u.contentType),
},
)
if err != nil {
return nil, fmt.Errorf("complete presign multipart upload: %w", err)
}
if resp.JSON409 != nil {
return nil, ErrConflict
}
if resp.JSON200 == nil {
return nil, fmt.Errorf("complete presign multipart upload: %w", ResponseAsError(resp))
}
return resp.JSON200, nil
}
func (u *presignUpload) abortMultipart(ctx context.Context, mpu *apigen.PresignMultipartUpload) error {
resp, err := u.uploader.Client.AbortPresignMultipartUploadWithResponse(ctx, u.repoID, u.branchID, mpu.UploadId,
&apigen.AbortPresignMultipartUploadParams{
Path: u.objectPath,
},
apigen.AbortPresignMultipartUploadJSONRequestBody{
PhysicalAddress: mpu.PhysicalAddress,
})
if err != nil {
return fmt.Errorf("abort presign multipart upload: %w", err)
}
if resp.StatusCode() != http.StatusNoContent {
return fmt.Errorf("abort presign multipart upload: %w", ResponseAsError(resp))
}
return nil
}
func (u *presignUpload) initMultipart(ctx context.Context) (*apigen.PresignMultipartUpload, error) {
// adjust part size
u.partSize = DefaultUploadPartSize
if u.size/u.partSize >= int64(MaxUploadParts) {
// Add one to the part size to account for remainders
u.partSize = (u.size / int64(MaxUploadParts)) + 1
}
// calculate the number of parts
u.numParts = int(u.size / u.partSize)
if u.size%u.partSize != 0 {
u.numParts++
}
// create presign multipart upload
resp, err := u.uploader.Client.CreatePresignMultipartUploadWithResponse(ctx, u.repoID, u.branchID, &apigen.CreatePresignMultipartUploadParams{
Path: u.objectPath,
Parts: swag.Int(u.numParts),
})
if err != nil {
return nil, fmt.Errorf("create presign multipart upload: %w", err)
}
if resp.JSON201 == nil {
return nil, fmt.Errorf("create presign multipart upload: %w", ResponseAsError(resp))
}
// verify we got the expected number of presigned urls
mpu := resp.JSON201
var presignedUrls []string
if mpu.PresignedUrls != nil {
presignedUrls = *mpu.PresignedUrls
}
if len(presignedUrls) != u.numParts {
return nil, fmt.Errorf("create presign multipart upload: %w, expected %d presigned urls, got %d", ErrRequestFailed, u.numParts, len(presignedUrls))
}
return mpu, nil
}
func (u *presignUpload) uploadPart(ctx context.Context, partReader *io.SectionReader, partURL string) (string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPut, partURL, partReader)
if err != nil {
return "", err
}
req.ContentLength = partReader.Size()
if u.contentType != "" {
req.Header.Set("Content-Type", u.contentType)
}
resp, err := u.uploader.HTTPClient.Do(req)
if err != nil {
return "", err
}
defer func() { _ = resp.Body.Close() }()
if !httputil.IsSuccessStatusCode(resp) {
return "", fmt.Errorf("upload %s part(%s): %w", partURL, resp.Status, ErrRequestFailed)
}
etag := extractEtagFromResponseHeader(resp.Header)
if etag == "" {
return "", fmt.Errorf("upload etag is missing %s: %w", partURL, ErrRequestFailed)
}
return etag, nil
}
func (u *presignUpload) uploadObject(ctx context.Context) (*apigen.ObjectStats, error) {
stagingLocation, err := getPhysicalAddress(ctx, u.uploader.Client, u.repoID, u.branchID, &apigen.GetPhysicalAddressParams{
Path: u.objectPath,
Presign: swag.Bool(true),
})
if err != nil {
return nil, err
}
preSignURL := swag.StringValue(stagingLocation.PresignedUrl)
var body io.ReadSeeker
if u.size > 0 {
// Passing Reader with content length == 0 results in 501 Not Implemented
body = u.reader
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, preSignURL, body)
if err != nil {
return nil, err
}
req.ContentLength = u.size
if u.contentType != "" {
req.Header.Set("Content-Type", u.contentType)
}
if isAzureBlobURL(preSignURL) {
req.Header.Set("x-ms-blob-type", "BlockBlob")
}
putResp, err := u.uploader.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = putResp.Body.Close() }()
if !httputil.IsSuccessStatusCode(putResp) {
return nil, fmt.Errorf("upload %w %s: %s", ErrRequestFailed, preSignURL, putResp.Status)
}
etag := extractEtagFromResponseHeader(putResp.Header)
if etag == "" {
return nil, fmt.Errorf("upload %w %s: etag is missing", ErrRequestFailed, preSignURL)
}
linkReqBody := apigen.LinkPhysicalAddressJSONRequestBody{
Checksum: etag,
SizeBytes: u.size,
Staging: *stagingLocation,
UserMetadata: &apigen.StagingMetadata_UserMetadata{
AdditionalProperties: u.metadata,
},
ContentType: apiutil.Ptr(u.contentType),
}
linkResp, err := u.uploader.Client.LinkPhysicalAddressWithResponse(ctx, u.repoID, u.branchID,
&apigen.LinkPhysicalAddressParams{
Path: u.objectPath,
}, linkReqBody)
if err != nil {
return nil, fmt.Errorf("link object to backing store: %w", err)
}
if linkResp.JSON200 != nil {
return linkResp.JSON200, nil
}
if linkResp.JSON409 != nil {
return nil, ErrConflict
}
return nil, fmt.Errorf("link object to backing store: %w (%s)", ErrRequestFailed, linkResp.Status())
}
func (u *presignUpload) Upload(ctx context.Context) (*apigen.ObjectStats, error) {
// use multipart upload if:
// 1. Multipart upload is supported by the server.
// 2. Reader supports ReaderAt.
// 3. Content size is greater than MinUploadPartSize.
if u.uploader.MultipartSupport && u.size >= MinUploadPartSize && u.readerAt != nil {
return u.uploadMultipart(ctx)
}
return u.uploadObject(ctx)
}
func ClientUploadPreSign(ctx context.Context, client apigen.ClientWithResponsesInterface, repoID, branchID, objPath string, metadata map[string]string, contentType string, contents io.ReadSeeker, presignMultipartSupport bool) (*apigen.ObjectStats, error) {
// upload loop, retry on conflict
uploader := NewPreSignUploader(client, presignMultipartSupport)
for {
stats, err := uploader.Upload(ctx, repoID, branchID, objPath, contents, contentType, metadata)
if err == nil {
return stats, nil
}
// break in case of error other than conflict, otherwise retry
if !errors.Is(err, ErrConflict) {
return nil, err
}
}
}
func isAzureBlobURL(u string) bool {
parsedURL, err := url.Parse(u)
if err != nil {
return false
}
return strings.HasSuffix(parsedURL.Host, ".blob.core.windows.net")
}
// extractEtagFromResponseHeader extracts the ETag from the response header.
// If the response contains a Content-MD5 header, it will be decoded from base64 and returned as hex.
func extractEtagFromResponseHeader(h http.Header) string {
// prefer Content-MD5 if exists
contentMD5 := h.Get("Content-MD5")
if contentMD5 != "" {
// decode base64, return as hex
decodeMD5, err := base64.StdEncoding.DecodeString(contentMD5)
if err == nil {
return hex.EncodeToString(decodeMD5)
}
}
// fallback to ETag
etag := h.Get("ETag")
etag = strings.TrimFunc(etag, func(r rune) bool { return r == '"' || r == ' ' })
return etag
}
func getPhysicalAddress(ctx context.Context, client apigen.ClientWithResponsesInterface, repoID string, branchID string, params *apigen.GetPhysicalAddressParams) (*apigen.StagingLocation, error) {
resp, err := client.GetPhysicalAddressWithResponse(ctx, repoID, branchID, params)
if err != nil {
return nil, fmt.Errorf("get physical address to upload object: %w", err)
}
if resp.JSONDefault != nil {
return nil, fmt.Errorf("%w: %s", ErrRequestFailed, resp.JSONDefault.Message)
}
if resp.JSON200 == nil {
return nil, fmt.Errorf("%w: %s (status code %d)", ErrRequestFailed, resp.Status(), resp.StatusCode())
}
return resp.JSON200, nil
}