forked from kahing/goofys
/
backend_gcs3.go
231 lines (192 loc) · 5.55 KB
/
backend_gcs3.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
// Copyright 2019 Ka-Hing Cheung
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
. "github.com/kahing/goofys/api/common"
"fmt"
"io"
"net/url"
"strconv"
"sync"
"sync/atomic"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/jacobsa/fuse"
)
// GCS variant of S3
type GCS3 struct {
*S3Backend
}
type GCSMultipartBlobCommitInput struct {
Size uint64
ETag *string
Prev *MultipartBlobAddInput
}
func NewGCS3(bucket string, flags *FlagStorage, config *S3Config) (*GCS3, error) {
s3Backend, err := NewS3(bucket, flags, config)
if err != nil {
return nil, err
}
s3Backend.Capabilities().Name = "gcs"
s := &GCS3{S3Backend: s3Backend}
s.S3Backend.gcs = true
s.S3Backend.cap.NoParallelMultipart = true
return s, nil
}
func (s *GCS3) Delegate() interface{} {
return s
}
func (s *GCS3) DeleteBlobs(param *DeleteBlobsInput) (*DeleteBlobsOutput, error) {
// GCS does not have multi-delete
var wg sync.WaitGroup
var overallErr error
for _, key := range param.Items {
wg.Add(1)
go func(key string) {
_, err := s.DeleteBlob(&DeleteBlobInput{
Key: key,
})
if err != nil && err != fuse.ENOENT {
overallErr = err
}
wg.Done()
}(key)
}
wg.Wait()
if overallErr != nil {
return nil, mapAwsError(overallErr)
}
return &DeleteBlobsOutput{}, nil
}
func (s *GCS3) MultipartBlobBegin(param *MultipartBlobBeginInput) (*MultipartBlobCommitInput, error) {
mpu := s3.CreateMultipartUploadInput{
Bucket: &s.bucket,
Key: ¶m.Key,
StorageClass: &s.config.StorageClass,
ContentType: param.ContentType,
}
if s.config.UseSSE {
mpu.ServerSideEncryption = &s.sseType
if s.config.UseKMS && s.config.KMSKeyID != "" {
mpu.SSEKMSKeyId = &s.config.KMSKeyID
}
}
if s.config.ACL != "" {
mpu.ACL = &s.config.ACL
}
req, _ := s.CreateMultipartUploadRequest(&mpu)
// v4 signing of this fails
s.setV2Signer(&req.Handlers)
// get rid of ?uploads=
req.HTTPRequest.URL.RawQuery = ""
req.HTTPRequest.Header.Set("x-goog-resumable", "start")
err := req.Send()
if err != nil {
s3Log.Errorf("CreateMultipartUpload %v = %v", param.Key, err)
return nil, mapAwsError(err)
}
location := req.HTTPResponse.Header.Get("Location")
_, err = url.Parse(location)
if err != nil {
s3Log.Errorf("CreateMultipartUpload %v %v = %v", param.Key, location, err)
return nil, mapAwsError(err)
}
return &MultipartBlobCommitInput{
Key: ¶m.Key,
Metadata: param.Metadata,
UploadId: &location,
Parts: make([]*string, 10000), // at most 10K parts
backendData: &GCSMultipartBlobCommitInput{},
}, nil
}
func (s *GCS3) uploadPart(param *MultipartBlobAddInput, totalSize uint64, last bool) (etag *string, err error) {
atomic.AddUint32(¶m.Commit.NumParts, 1)
if closer, ok := param.Body.(io.Closer); ok {
defer closer.Close()
}
// the mpuId serves as authentication token so
// technically we don't need to sign this anymore and
// can just use a plain HTTP request, but going
// through aws-sdk-go anyway to get retry handling
params := &s3.PutObjectInput{
Bucket: &s.bucket,
Key: param.Commit.Key,
Body: param.Body,
}
s3Log.Debug(params)
req, resp := s.PutObjectRequest(params)
req.Handlers.Sign.Clear()
req.HTTPRequest.URL, _ = url.Parse(*param.Commit.UploadId)
start := totalSize - param.Size
end := totalSize - 1
var size string
if last {
size = strconv.FormatUint(totalSize, 10)
} else {
size = "*"
}
contentRange := fmt.Sprintf("bytes %v-%v/%v", start, end, size)
req.HTTPRequest.Header.Set("Content-Length", strconv.FormatUint(param.Size, 10))
req.HTTPRequest.Header.Set("Content-Range", contentRange)
err = req.Send()
if err != nil {
// status indicating that we need more parts to finish this
if req.HTTPResponse.StatusCode == 308 {
err = nil
} else {
err = mapAwsError(err)
return
}
}
etag = resp.ETag
return
}
func (s *GCS3) MultipartBlobAdd(param *MultipartBlobAddInput) (*MultipartBlobAddOutput, error) {
var commitData *GCSMultipartBlobCommitInput
var ok bool
if commitData, ok = param.Commit.backendData.(*GCSMultipartBlobCommitInput); !ok {
panic("Incorrect commit data type")
}
if commitData.Prev != nil {
if commitData.Prev.Size == 0 || commitData.Prev.Size%(256*1024) != 0 {
s3Log.Errorf("size of each block must be multiple of 256KB: %v", param.Size)
return nil, fuse.EINVAL
}
_, err := s.uploadPart(commitData.Prev, commitData.Size, false)
if err != nil {
return nil, err
}
}
commitData.Size += param.Size
copy := *param
commitData.Prev = ©
param.Body = nil
return &MultipartBlobAddOutput{}, nil
}
func (s *GCS3) MultipartBlobCommit(param *MultipartBlobCommitInput) (*MultipartBlobCommitOutput, error) {
var commitData *GCSMultipartBlobCommitInput
var ok bool
if commitData, ok = param.backendData.(*GCSMultipartBlobCommitInput); !ok {
panic("Incorrect commit data type")
}
if commitData.Prev == nil {
panic("commit should include last part")
}
etag, err := s.uploadPart(commitData.Prev, commitData.Size, true)
if err != nil {
return nil, err
}
return &MultipartBlobCommitOutput{
ETag: etag,
}, nil
}