This repository has been archived by the owner on May 7, 2021. It is now read-only.
/
bucket.go
388 lines (330 loc) · 8.84 KB
/
bucket.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
// Copyright 2016 CoreOS, Inc.
//
// 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 storage
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"sync"
"golang.org/x/net/context"
"google.golang.org/api/googleapi"
"google.golang.org/api/storage/v1"
)
var (
UnknownScheme = errors.New("storage: URL missing gs:// scheme")
UnknownBucket = errors.New("storage: URL missing bucket name")
)
type Bucket struct {
service *storage.Service
name string
prefix string
mu sync.RWMutex
prefixes map[string]struct{}
objects map[string]*storage.Object
// writeAlways enables overwriting of objects that appear up-to-date
writeAlways bool
// writeDryRun blocks any changes, merely logging them instead
writeDryRun bool
}
func NewBucket(client *http.Client, bucketURL string) (*Bucket, error) {
service, err := storage.New(client)
if err != nil {
return nil, err
}
parsedURL, err := url.Parse(bucketURL)
if err != nil {
return nil, err
}
if parsedURL.Scheme != "gs" {
return nil, UnknownScheme
}
if parsedURL.Host == "" {
return nil, UnknownBucket
}
return &Bucket{
service: service,
name: parsedURL.Host,
prefix: FixPrefix(parsedURL.Path),
prefixes: make(map[string]struct{}),
objects: make(map[string]*storage.Object),
}, nil
}
func (b *Bucket) Name() string {
return b.name
}
func (b *Bucket) Prefix() string {
return b.prefix
}
func (b *Bucket) URL() *url.URL {
return &url.URL{Scheme: "gs", Host: b.name, Path: b.prefix}
}
func (b *Bucket) WriteAlways(always bool) {
b.writeAlways = always
}
func (b *Bucket) WriteDryRun(dryrun bool) {
b.writeDryRun = dryrun
}
func (b *Bucket) Object(objName string) *storage.Object {
b.mu.RLock()
defer b.mu.RUnlock()
return b.objects[objName]
}
func (b *Bucket) Objects() []*storage.Object {
b.mu.RLock()
defer b.mu.RUnlock()
objs := make([]*storage.Object, 0, len(b.objects))
for _, obj := range b.objects {
objs = append(objs, obj)
}
return objs
}
func (b *Bucket) Prefixes() []string {
seen := make(map[string]bool)
list := make([]string, 0)
add := func(prefix string) {
for !seen[prefix] {
seen[prefix] = true
list = append(list, prefix)
prefix = NextPrefix(prefix)
}
}
b.mu.RLock()
defer b.mu.RUnlock()
for prefix := range b.prefixes {
add(prefix)
}
for objName := range b.objects {
add(NextPrefix(objName))
}
return list
}
func (b *Bucket) Len() int {
b.mu.RLock()
defer b.mu.RUnlock()
return len(b.objects)
}
func (b *Bucket) addObject(obj *storage.Object) {
if obj.Bucket != b.name {
panic(fmt.Errorf("adding gs://%s/%s to bucket %s", obj.Bucket, obj.Name, b.name))
}
b.mu.Lock()
defer b.mu.Unlock()
b.objects[obj.Name] = obj
}
func (b *Bucket) addObjects(objs *storage.Objects) {
b.mu.Lock()
defer b.mu.Unlock()
for _, obj := range objs.Items {
if obj.Bucket != b.name {
panic(fmt.Errorf("adding gs://%s/%s to bucket %s", obj.Bucket, obj.Name, b.name))
}
b.objects[obj.Name] = obj
}
for _, pfx := range objs.Prefixes {
b.prefixes[pfx] = struct{}{}
}
}
func (b *Bucket) delObject(objName string) {
b.mu.Lock()
defer b.mu.Unlock()
delete(b.objects, objName)
}
func (b *Bucket) mkURL(obj interface{}) *url.URL {
switch v := obj.(type) {
case string:
u := b.URL()
u.Path = v
return u
case *storage.Object:
u := b.URL()
u.Path = v.Name
if v.Bucket != "" {
u.Host = v.Bucket
}
return u
case *url.URL:
return v
case nil:
return b.URL()
default:
panic(fmt.Errorf("unknown type %T", obj))
}
}
func (b *Bucket) apiErr(op string, obj interface{}, e error) error {
if _, ok := e.(*googleapi.Error); ok {
return &Error{Op: op, URL: b.mkURL(obj).String(), Err: e}
}
return e
}
func (b *Bucket) Fetch(ctx context.Context) error {
return b.FetchPrefix(ctx, b.prefix, true)
}
func (b *Bucket) FetchPrefix(ctx context.Context, prefix string, recursive bool) error {
prefix = FixPrefix(prefix)
req := b.service.Objects.List(b.name)
if prefix != "" {
req.Prefix(prefix)
}
if !recursive {
req.Delimiter("/")
}
n := 0
p := 0
u := b.URL()
u.Path = prefix
add := func(objs *storage.Objects) error {
b.addObjects(objs)
n += len(objs.Items)
plog.Infof("Found %d objects under %s", n, u)
if len(objs.Prefixes) > 0 {
p += len(objs.Prefixes)
plog.Infof("Found %d directories under %s", p, u)
}
return nil
}
plog.Noticef("Fetching %s", u)
if err := req.Pages(ctx, add); err != nil {
return b.apiErr("storage.objects.list", nil, err)
}
if prefix == "" {
return nil
}
// In order to pair well with HTML indexing we need to check for
// a redirect object (prefix minus trailing slash). The list
// request needs the slash get foo/bar/* but not foo/barbaz.
redirName := strings.TrimSuffix(prefix, "/")
if b.Object(redirName) != nil {
return nil
}
redirReq := b.service.Objects.Get(b.name, redirName)
redirReq.Context(ctx)
redirObj, err := redirReq.Do()
if e, ok := err.(*googleapi.Error); ok && e.Code == 404 {
return nil // missing is perfectly valid
} else if err != nil {
return b.apiErr("storage.objects.get", redirName, err)
}
b.addObject(redirObj)
return nil
}
func (b *Bucket) Upload(ctx context.Context, obj *storage.Object, media io.ReaderAt) error {
// Calculate the checksum to enable upload integrity checking.
if obj.Crc32c == "" {
obj = dupObj(obj) // avoid editing the original
if err := crcSum(obj, media); err != nil {
return err
}
}
old := b.Object(obj.Name)
if !b.writeAlways && crcEq(old, obj) {
return nil // up to date!
}
if b.writeDryRun {
plog.Noticef("Would write %s", b.mkURL(obj))
return nil
}
req := b.service.Objects.Insert(b.name, obj)
// ResumableMedia is documented as deprecated in favor of Media
// but Media's retry support was bad and got temporarily removed.
// https://github.com/google/google-api-go-client/commit/9737cc9e103c00d06a8f3993361dec083df3d252
req.ResumableMedia(ctx, media, int64(obj.Size), obj.ContentType)
// Watch out for unexpected conflicting updates.
if old != nil {
req.IfGenerationMatch(old.Generation)
}
plog.Noticef("Writing %s", b.mkURL(obj))
inserted, err := req.Do()
if err != nil {
return b.apiErr("storage.objects.insert", obj, err)
}
b.addObject(inserted)
return nil
}
func (b *Bucket) Copy(ctx context.Context, src *storage.Object, dstName string) error {
if src.Bucket == "" {
panic(fmt.Errorf("src.Bucket is blank: %#v", src))
}
old := b.Object(dstName)
if !b.writeAlways && crcEq(old, src) {
return nil // up to date!
}
// It does work to pass src directly to the Rewrite API call, the
// name and bucket values don't really matter, they just cannot be
// blank for whatever reason. We make a copy just to get consistent
// results, e.g. always use the destination bucket's default ACL.
dst := dupObj(src)
dst.Name = dstName
dst.Bucket = b.name
if b.writeDryRun {
plog.Noticef("Would copy %s to %s", b.mkURL(src), b.mkURL(dst))
return nil
}
req := b.service.Objects.Rewrite(
src.Bucket, src.Name, dst.Bucket, dst.Name, src)
req.Context(ctx)
// Watch out for unexpected conflicting updates.
if old != nil {
req.IfGenerationMatch(old.Generation)
}
if src.Generation != 0 {
req.IfSourceGenerationMatch(src.Generation)
}
plog.Noticef("Copying %s to %s", b.mkURL(src), b.mkURL(dst))
for {
resp, err := req.Do()
if err != nil {
return b.apiErr("storage.objects.rewrite", dst, err)
}
if resp.Done {
b.addObject(resp.Resource)
return nil
}
req.RewriteToken(resp.RewriteToken)
}
}
func (b *Bucket) Delete(ctx context.Context, objName string) error {
if b.writeDryRun {
plog.Noticef("Would delete %s", b.mkURL(objName))
return nil
}
req := b.service.Objects.Delete(b.name, objName)
req.Context(ctx)
// Watch out for unexpected conflicting updates.
if old := b.Object(objName); old != nil {
req.IfGenerationMatch(old.Generation)
req.IfMetagenerationMatch(old.Metageneration)
}
plog.Noticef("Deleting %s", b.mkURL(objName))
if err := req.Do(); err != nil {
return b.apiErr("storage.objects.delete", objName, err)
}
b.delObject(objName)
return nil
}
// FixPrefix ensures non-empty paths end in a slash but never start with one.
func FixPrefix(p string) string {
if p != "" && !strings.HasSuffix(p, "/") {
p += "/"
}
return strings.TrimPrefix(p, "/")
}
// NextPrefix chops off the final component of an object name or prefix.
func NextPrefix(name string) string {
prefix, _ := path.Split(strings.TrimSuffix(name, "/"))
return prefix
}