-
Notifications
You must be signed in to change notification settings - Fork 342
/
postobject.go
174 lines (163 loc) · 6.67 KB
/
postobject.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
package operations
import (
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/treeverse/lakefs/pkg/block"
gatewayErrors "github.com/treeverse/lakefs/pkg/gateway/errors"
"github.com/treeverse/lakefs/pkg/gateway/multipart"
"github.com/treeverse/lakefs/pkg/gateway/path"
"github.com/treeverse/lakefs/pkg/gateway/serde"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/httputil"
"github.com/treeverse/lakefs/pkg/logging"
"github.com/treeverse/lakefs/pkg/permissions"
)
const (
CreateMultipartUploadQueryParam = "uploads"
CompleteMultipartUploadQueryParam = "uploadId"
)
type PostObject struct{}
func (controller *PostObject) RequiredPermissions(_ *http.Request, repoID, _, path string) (permissions.Node, error) {
return permissions.Node{
Permission: permissions.Permission{
Action: permissions.WriteObjectAction,
Resource: permissions.ObjectArn(repoID, path),
},
}, nil
}
func (controller *PostObject) HandleCreateMultipartUpload(w http.ResponseWriter, req *http.Request, o *PathOperation) {
o.Incr("create_mpu", o.Principal, o.Repository.Name, o.Reference)
branchExists, err := o.Catalog.BranchExists(req.Context(), o.Repository.Name, o.Reference)
if err != nil {
o.Log(req).WithError(err).Error("could not check if branch exists")
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrInternalError))
return
}
if !branchExists {
o.Log(req).Debug("branch not found")
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrNoSuchBucket))
return
}
address := o.PathProvider.NewPath()
storageClass := StorageClassFromHeader(req.Header)
opts := block.CreateMultiPartUploadOpts{StorageClass: storageClass}
resp, err := o.BlockStore.CreateMultiPartUpload(req.Context(), block.ObjectPointer{StorageNamespace: o.Repository.StorageNamespace, Identifier: address}, req, opts)
if err != nil {
o.Log(req).WithError(err).Error("could not create multipart upload")
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrInternalError))
return
}
mpu := multipart.Upload{
UploadID: resp.UploadID,
Path: o.Path,
CreationDate: time.Now(),
PhysicalAddress: address,
Metadata: map[string]string(amzMetaAsMetadata(req)),
ContentType: req.Header.Get("Content-Type"),
}
err = o.MultipartTracker.Create(req.Context(), mpu)
if err != nil {
o.Log(req).WithError(err).Error("could not write multipart upload to DB")
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrInternalError))
return
}
o.SetHeaders(w, resp.ServerSideHeader)
o.EncodeResponse(w, req, &serde.InitiateMultipartUploadResult{
Bucket: o.Repository.Name,
Key: path.WithRef(o.Path, o.Reference),
UploadID: resp.UploadID,
}, http.StatusOK)
}
func (controller *PostObject) HandleCompleteMultipartUpload(w http.ResponseWriter, req *http.Request, o *PathOperation) {
o.Incr("complete_mpu", o.Principal, o.Repository.Name, o.Reference)
uploadID := req.URL.Query().Get(CompleteMultipartUploadQueryParam)
req = req.WithContext(logging.AddFields(req.Context(), logging.Fields{logging.UploadIDFieldKey: uploadID}))
multiPart, err := o.MultipartTracker.Get(req.Context(), uploadID)
if err != nil {
o.Log(req).WithError(err).Error("could not read multipart record")
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrInternalError))
return
}
objName := multiPart.PhysicalAddress
req = req.WithContext(logging.AddFields(req.Context(), logging.Fields{logging.PhysicalAddressFieldKey: objName}))
xmlMultipartComplete, err := io.ReadAll(req.Body)
if err != nil {
o.Log(req).WithError(err).Error("could not read request body")
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrInternalError))
return
}
var multipartList block.MultipartUploadCompletion
err = xml.Unmarshal(xmlMultipartComplete, &multipartList)
if err != nil {
o.Log(req).WithError(err).Error("could not parse multipart XML on complete multipart")
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrInternalError))
return
}
normalizeMultipartUploadCompletion(&multipartList)
resp, err := o.BlockStore.CompleteMultiPartUpload(req.Context(),
block.ObjectPointer{StorageNamespace: o.Repository.StorageNamespace, Identifier: objName},
uploadID,
&multipartList)
if err != nil {
o.Log(req).WithError(err).Error("could not complete multipart upload")
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrInternalError))
return
}
checksum := strings.Split(resp.ETag, "-")[0]
err = o.finishUpload(req, checksum, objName, resp.ContentLength, true, multiPart.Metadata, multiPart.ContentType)
if errors.Is(err, graveler.ErrWriteToProtectedBranch) {
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrWriteToProtectedBranch))
return
}
if err != nil {
_ = o.EncodeError(w, req, gatewayErrors.Codes.ToAPIErr(gatewayErrors.ErrInternalError))
return
}
err = o.MultipartTracker.Delete(req.Context(), uploadID)
if err != nil {
o.Log(req).WithError(err).Warn("could not delete multipart record")
}
scheme := httputil.RequestScheme(req)
var location string
if o.MatchedHost {
location = fmt.Sprintf("%s://%s/%s/%s", scheme, req.Host, o.Reference, o.Path)
} else {
location = fmt.Sprintf("%s://%s/%s/%s/%s", scheme, req.Host, o.Repository.Name, o.Reference, o.Path)
}
o.SetHeaders(w, resp.ServerSideHeader)
o.EncodeResponse(w, req, &serde.CompleteMultipartUploadResult{
Location: location,
Bucket: o.Repository.Name,
Key: path.WithRef(o.Path, o.Reference),
ETag: httputil.ETag(resp.ETag),
}, http.StatusOK)
}
// normalizeMultipartUploadCompletion normalization incoming multipart upload completion list.
// we make sure that each part's ETag will be without the wrapping quotes
func normalizeMultipartUploadCompletion(list *block.MultipartUploadCompletion) {
for i := range list.Part {
list.Part[i].ETag = strings.Trim(list.Part[i].ETag, `"`)
}
}
func (controller *PostObject) Handle(w http.ResponseWriter, req *http.Request, o *PathOperation) {
// POST is only supported for CreateMultipartUpload/CompleteMultipartUpload
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html
_, mpuCreateParamExist := req.URL.Query()[CreateMultipartUploadQueryParam]
if mpuCreateParamExist {
controller.HandleCreateMultipartUpload(w, req, o)
return
}
_, mpuCompleteParamExist := req.URL.Query()[CompleteMultipartUploadQueryParam]
if mpuCompleteParamExist {
controller.HandleCompleteMultipartUpload(w, req, o)
return
}
// otherwise
w.WriteHeader(http.StatusMethodNotAllowed)
}