forked from distribution/distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
417 lines (356 loc) · 11.2 KB
/
types.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
// Copyright 2014 Google Inc. All Rights Reserved.
//
// 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 (
"encoding/base64"
"io"
"sync"
"time"
"golang.org/x/net/context"
raw "google.golang.org/api/storage/v1"
)
// Bucket represents a Google Cloud Storage bucket.
type Bucket struct {
// Name is the name of the bucket.
Name string
// ACL is the list of access control rules on the bucket.
ACL []ACLRule
// DefaultObjectACL is the list of access controls to
// apply to new objects when no object ACL is provided.
DefaultObjectACL []ACLRule
// Location is the location of the bucket. It defaults to "US".
Location string
// Metageneration is the metadata generation of the bucket.
// Read-only.
Metageneration int64
// StorageClass is the storage class of the bucket. This defines
// how objects in the bucket are stored and determines the SLA
// and the cost of storage. Typical values are "STANDARD" and
// "DURABLE_REDUCED_AVAILABILITY". Defaults to "STANDARD".
StorageClass string
// Created is the creation time of the bucket.
// Read-only.
Created time.Time
}
func newBucket(b *raw.Bucket) *Bucket {
if b == nil {
return nil
}
bucket := &Bucket{
Name: b.Name,
Location: b.Location,
Metageneration: b.Metageneration,
StorageClass: b.StorageClass,
Created: convertTime(b.TimeCreated),
}
acl := make([]ACLRule, len(b.Acl))
for i, rule := range b.Acl {
acl[i] = ACLRule{
Entity: ACLEntity(rule.Entity),
Role: ACLRole(rule.Role),
}
}
bucket.ACL = acl
objACL := make([]ACLRule, len(b.DefaultObjectAcl))
for i, rule := range b.DefaultObjectAcl {
objACL[i] = ACLRule{
Entity: ACLEntity(rule.Entity),
Role: ACLRole(rule.Role),
}
}
bucket.DefaultObjectACL = objACL
return bucket
}
// ObjectAttrs is the user-editable object attributes.
type ObjectAttrs struct {
// Name is the name of the object.
Name string
// ContentType is the MIME type of the object's content.
// Optional.
ContentType string
// ContentLanguage is the optional RFC 1766 Content-Language of
// the object's content sent in response headers.
ContentLanguage string
// ContentEncoding is the optional Content-Encoding of the object
// sent it the response headers.
ContentEncoding string
// CacheControl is the optional Cache-Control header of the object
// sent in the response headers.
CacheControl string
// ContentDisposition is the optional Content-Disposition header of the object
// sent in the response headers.
ContentDisposition string
// ACL is the list of access control rules for the object.
// Optional. If nil or empty, existing ACL rules are preserved.
ACL []ACLRule
// Metadata represents user-provided metadata, in key/value pairs.
// It can be nil if the current metadata values needs to preserved.
Metadata map[string]string
}
func (o ObjectAttrs) toRawObject(bucket string) *raw.Object {
var acl []*raw.ObjectAccessControl
if len(o.ACL) > 0 {
acl = make([]*raw.ObjectAccessControl, len(o.ACL))
for i, rule := range o.ACL {
acl[i] = &raw.ObjectAccessControl{
Entity: string(rule.Entity),
Role: string(rule.Role),
}
}
}
return &raw.Object{
Bucket: bucket,
Name: o.Name,
ContentType: o.ContentType,
ContentEncoding: o.ContentEncoding,
ContentLanguage: o.ContentLanguage,
CacheControl: o.CacheControl,
ContentDisposition: o.ContentDisposition,
Acl: acl,
Metadata: o.Metadata,
}
}
// Object represents a Google Cloud Storage (GCS) object.
type Object struct {
// Bucket is the name of the bucket containing this GCS object.
Bucket string
// Name is the name of the object within the bucket.
Name string
// ContentType is the MIME type of the object's content.
ContentType string
// ContentLanguage is the content language of the object's content.
ContentLanguage string
// CacheControl is the Cache-Control header to be sent in the response
// headers when serving the object data.
CacheControl string
// ACL is the list of access control rules for the object.
ACL []ACLRule
// Owner is the owner of the object.
//
// If non-zero, it is in the form of "user-<userId>".
Owner string
// Size is the length of the object's content.
Size int64
// ContentEncoding is the encoding of the object's content.
ContentEncoding string
// MD5 is the MD5 hash of the object's content.
MD5 []byte
// CRC32C is the CRC32 checksum of the object's content using
// the Castagnoli93 polynomial.
CRC32C uint32
// MediaLink is an URL to the object's content.
MediaLink string
// Metadata represents user-provided metadata, in key/value pairs.
// It can be nil if no metadata is provided.
Metadata map[string]string
// Generation is the generation number of the object's content.
Generation int64
// MetaGeneration is the version of the metadata for this
// object at this generation. This field is used for preconditions
// and for detecting changes in metadata. A metageneration number
// is only meaningful in the context of a particular generation
// of a particular object.
MetaGeneration int64
// StorageClass is the storage class of the bucket.
// This value defines how objects in the bucket are stored and
// determines the SLA and the cost of storage. Typical values are
// "STANDARD" and "DURABLE_REDUCED_AVAILABILITY".
// It defaults to "STANDARD".
StorageClass string
// Deleted is the time the object was deleted.
// If not deleted, it is the zero value.
Deleted time.Time
// Updated is the creation or modification time of the object.
// For buckets with versioning enabled, changing an object's
// metadata does not change this property.
Updated time.Time
}
// convertTime converts a time in RFC3339 format to time.Time.
// If any error occurs in parsing, the zero-value time.Time is silently returned.
func convertTime(t string) time.Time {
var r time.Time
if t != "" {
r, _ = time.Parse(time.RFC3339, t)
}
return r
}
func newObject(o *raw.Object) *Object {
if o == nil {
return nil
}
acl := make([]ACLRule, len(o.Acl))
for i, rule := range o.Acl {
acl[i] = ACLRule{
Entity: ACLEntity(rule.Entity),
Role: ACLRole(rule.Role),
}
}
owner := ""
if o.Owner != nil {
owner = o.Owner.Entity
}
md5, _ := base64.StdEncoding.DecodeString(o.Md5Hash)
var crc32c uint32
d, err := base64.StdEncoding.DecodeString(o.Crc32c)
if err == nil && len(d) == 4 {
crc32c = uint32(d[0])<<24 + uint32(d[1])<<16 + uint32(d[2])<<8 + uint32(d[3])
}
return &Object{
Bucket: o.Bucket,
Name: o.Name,
ContentType: o.ContentType,
ContentLanguage: o.ContentLanguage,
CacheControl: o.CacheControl,
ACL: acl,
Owner: owner,
ContentEncoding: o.ContentEncoding,
Size: int64(o.Size),
MD5: md5,
CRC32C: crc32c,
MediaLink: o.MediaLink,
Metadata: o.Metadata,
Generation: o.Generation,
MetaGeneration: o.Metageneration,
StorageClass: o.StorageClass,
Deleted: convertTime(o.TimeDeleted),
Updated: convertTime(o.Updated),
}
}
// Query represents a query to filter objects from a bucket.
type Query struct {
// Delimiter returns results in a directory-like fashion.
// Results will contain only objects whose names, aside from the
// prefix, do not contain delimiter. Objects whose names,
// aside from the prefix, contain delimiter will have their name,
// truncated after the delimiter, returned in prefixes.
// Duplicate prefixes are omitted.
// Optional.
Delimiter string
// Prefix is the prefix filter to query objects
// whose names begin with this prefix.
// Optional.
Prefix string
// Versions indicates whether multiple versions of the same
// object will be included in the results.
Versions bool
// Cursor is a previously-returned page token
// representing part of the larger set of results to view.
// Optional.
Cursor string
// MaxResults is the maximum number of items plus prefixes
// to return. As duplicate prefixes are omitted,
// fewer total results may be returned than requested.
// The default page limit is used if it is negative or zero.
MaxResults int
}
// Objects represents a list of objects returned from
// a bucket look-p request and a query to retrieve more
// objects from the next pages.
type Objects struct {
// Results represent a list of object results.
Results []*Object
// Next is the continuation query to retrieve more
// results with the same filtering criteria. If there
// are no more results to retrieve, it is nil.
Next *Query
// Prefixes represents prefixes of objects
// matching-but-not-listed up to and including
// the requested delimiter.
Prefixes []string
}
// contentTyper implements ContentTyper to enable an
// io.ReadCloser to specify its MIME type.
type contentTyper struct {
io.Reader
t string
}
func (c *contentTyper) ContentType() string {
return c.t
}
// A Writer writes a Cloud Storage object.
type Writer struct {
// ObjectAttrs are optional attributes to set on the object. Any attributes
// must be initialized before the first Write call. Nil or zero-valued
// attributes are ignored.
ObjectAttrs
ctx context.Context
bucket string
name string
once sync.Once
opened bool
r io.Reader
pw *io.PipeWriter
donec chan struct{} // closed after err and obj are set.
err error
obj *Object
}
func (w *Writer) open() {
attrs := w.ObjectAttrs
// Always set the name, otherwise the backend
// rejects the request and responds with an HTTP 400.
if attrs.Name == "" {
attrs.Name = w.name
}
pr, pw := io.Pipe()
w.r = &contentTyper{pr, attrs.ContentType}
w.pw = pw
w.opened = true
go func() {
resp, err := rawService(w.ctx).Objects.Insert(
w.bucket, attrs.toRawObject(w.bucket)).Media(w.r).Projection("full").Context(w.ctx).Do()
w.err = err
if err == nil {
w.obj = newObject(resp)
} else {
pr.CloseWithError(w.err)
}
close(w.donec)
}()
}
// Write appends to w.
func (w *Writer) Write(p []byte) (n int, err error) {
if w.err != nil {
return 0, w.err
}
if !w.opened {
w.open()
}
return w.pw.Write(p)
}
// Close completes the write operation and flushes any buffered data.
// If Close doesn't return an error, metadata about the written object
// can be retrieved by calling Object.
func (w *Writer) Close() error {
if !w.opened {
w.open()
}
if err := w.pw.Close(); err != nil {
return err
}
<-w.donec
return w.err
}
// CloseWithError aborts the write operation with the provided error.
// CloseWithError always returns nil.
func (w *Writer) CloseWithError(err error) error {
if !w.opened {
return nil
}
return w.pw.CloseWithError(err)
}
// Object returns metadata about a successfully-written object.
// It's only valid to call it after Close returns nil.
func (w *Writer) Object() *Object {
return w.obj
}