forked from tus/tusd
/
gcsstore.go
308 lines (251 loc) · 6.15 KB
/
gcsstore.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
// Package gcsstore provides a Google cloud storage based backend.
//
// GCSStore is a storage backend that uses the GCSAPI interface in order to store uploads
// on GCS. Uploads will be represented by two files in GCS; the data file will be stored
// as an extensionless object [uid] and the JSON info file will stored as [uid].info.
// In order to store uploads on GCS, make sure to specifiy the appropriate Google service
// account file path in the GCS_SERVICE_ACCOUNT_FILE environment variable. Also make sure that
// this service account file has the "https://www.googleapis.com/auth/devstorage.read_write"
// scope enabled so you can read and write data to the storage buckets associated with the
// service account file.
package gcsstore
import (
"bytes"
"encoding/json"
"fmt"
"golang.org/x/net/context"
"io"
"strconv"
"strings"
"sync"
"sync/atomic"
"cloud.google.com/go/storage"
"github.com/brianshepanek/tusd"
"github.com/tus/tusd/uid"
)
// See the tusd.DataStore interface for documentation about the different
// methods.
type GCSStore struct {
// Specifies the GCS bucket that uploads will be stored in
Bucket string
// Service specifies an interface used to communicate with the Google
// cloud storage backend. Implementation can be seen in gcsservice file.
Service GCSAPI
}
// New constructs a new GCS storage backend using the supplied GCS bucket name
// and service object.
func New(bucket string, service GCSAPI) GCSStore {
return GCSStore{
Bucket: bucket,
Service: service,
}
}
func (store GCSStore) UseIn(composer *tusd.StoreComposer) {
composer.UseCore(store)
composer.UseTerminater(store)
composer.UseFinisher(store)
composer.UseGetReader(store)
}
func (store GCSStore) NewUpload(info tusd.FileInfo) (id string, err error) {
if info.ID == "" {
info.ID = uid.Uid()
}
ctx := context.Background()
err = store.writeInfo(ctx, info.ID, info)
if err != nil {
return info.ID, err
}
return info.ID, nil
}
func (store GCSStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
prefix := fmt.Sprintf("%s_", id)
filterParams := GCSFilterParams{
Bucket: store.Bucket,
Prefix: prefix,
}
ctx := context.Background()
names, err := store.Service.FilterObjects(ctx, filterParams)
if err != nil {
return 0, err
}
maxIdx := -1
for _, name := range names {
split := strings.Split(name, "_")
idx, err := strconv.Atoi(split[len(split)-1])
if err != nil {
return 0, err
}
if idx > maxIdx {
maxIdx = idx
}
}
cid := fmt.Sprintf("%s_%d", id, maxIdx+1)
objectParams := GCSObjectParams{
Bucket: store.Bucket,
ID: cid,
}
n, err := store.Service.WriteObject(ctx, objectParams, src)
if err != nil {
return 0, err
}
return n, err
}
const CONCURRENT_SIZE_REQUESTS = 32
func (store GCSStore) GetInfo(id string) (tusd.FileInfo, error) {
info := tusd.FileInfo{}
i := fmt.Sprintf("%s.info", id)
params := GCSObjectParams{
Bucket: store.Bucket,
ID: i,
}
ctx := context.Background()
r, err := store.Service.ReadObject(ctx, params)
if err != nil {
if err == storage.ErrObjectNotExist {
return info, tusd.ErrNotFound
}
return info, err
}
buf := make([]byte, r.Size())
_, err = r.Read(buf)
if err != nil {
return info, err
}
if err := json.Unmarshal(buf, &info); err != nil {
return info, err
}
prefix := fmt.Sprintf("%s", id)
filterParams := GCSFilterParams{
Bucket: store.Bucket,
Prefix: prefix,
}
names, err := store.Service.FilterObjects(ctx, filterParams)
if err != nil {
return info, err
}
var offset int64 = 0
var firstError error = nil
var wg sync.WaitGroup
sem := make(chan struct{}, CONCURRENT_SIZE_REQUESTS)
errChan := make(chan error)
ctxCancel, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
for err := range errChan {
if err != context.Canceled && firstError == nil {
firstError = err
cancel()
}
}
}()
for _, name := range names {
sem <- struct{}{}
wg.Add(1)
params = GCSObjectParams{
Bucket: store.Bucket,
ID: name,
}
go func(params GCSObjectParams) {
defer func() {
<-sem
wg.Done()
}()
size, err := store.Service.GetObjectSize(ctxCancel, params)
if err != nil {
errChan <- err
return
}
atomic.AddInt64(&offset, size)
}(params)
}
wg.Wait()
close(errChan)
if firstError != nil {
return info, firstError
}
info.Offset = offset
err = store.writeInfo(ctx, id, info)
if err != nil {
return info, err
}
return info, nil
}
func (store GCSStore) writeInfo(ctx context.Context, id string, info tusd.FileInfo) error {
data, err := json.Marshal(info)
if err != nil {
return err
}
r := bytes.NewReader(data)
i := fmt.Sprintf("%s.info", id)
params := GCSObjectParams{
Bucket: store.Bucket,
ID: i,
}
_, err = store.Service.WriteObject(ctx, params, r)
if err != nil {
return err
}
return nil
}
func (store GCSStore) FinishUpload(id string) error {
prefix := fmt.Sprintf("%s_", id)
filterParams := GCSFilterParams{
Bucket: store.Bucket,
Prefix: prefix,
}
ctx := context.Background()
names, err := store.Service.FilterObjects(ctx, filterParams)
if err != nil {
return err
}
composeParams := GCSComposeParams{
Bucket: store.Bucket,
Destination: id,
Sources: names,
}
err = store.Service.ComposeObjects(ctx, composeParams)
if err != nil {
return err
}
err = store.Service.DeleteObjectsWithFilter(ctx, filterParams)
if err != nil {
return err
}
info, err := store.GetInfo(id)
if err != nil {
return err
}
objectParams := GCSObjectParams{
Bucket: store.Bucket,
ID: id,
}
err = store.Service.SetObjectMetadata(ctx, objectParams, info.MetaData)
if err != nil {
return err
}
return nil
}
func (store GCSStore) Terminate(id string) error {
filterParams := GCSFilterParams{
Bucket: store.Bucket,
Prefix: id,
}
ctx := context.Background()
err := store.Service.DeleteObjectsWithFilter(ctx, filterParams)
if err != nil {
return err
}
return nil
}
func (store GCSStore) GetReader(id string) (io.Reader, error) {
params := GCSObjectParams{
Bucket: store.Bucket,
ID: id,
}
ctx := context.Background()
r, err := store.Service.ReadObject(ctx, params)
if err != nil {
return nil, err
}
return r, nil
}