1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package storage
16
17 import (
18 "context"
19 "io"
20 "time"
21
22 "cloud.google.com/go/iam/apiv1/iampb"
23 gax "github.com/googleapis/gax-go/v2"
24 "google.golang.org/api/option"
25 )
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 type storageClient interface {
43
44
45
46 GetServiceAccount(ctx context.Context, project string, opts ...storageOption) (string, error)
47 CreateBucket(ctx context.Context, project, bucket string, attrs *BucketAttrs, enableObjectRetention *bool, opts ...storageOption) (*BucketAttrs, error)
48 ListBuckets(ctx context.Context, project string, opts ...storageOption) *BucketIterator
49 Close() error
50
51
52
53 DeleteBucket(ctx context.Context, bucket string, conds *BucketConditions, opts ...storageOption) error
54 GetBucket(ctx context.Context, bucket string, conds *BucketConditions, opts ...storageOption) (*BucketAttrs, error)
55 UpdateBucket(ctx context.Context, bucket string, uattrs *BucketAttrsToUpdate, conds *BucketConditions, opts ...storageOption) (*BucketAttrs, error)
56 LockBucketRetentionPolicy(ctx context.Context, bucket string, conds *BucketConditions, opts ...storageOption) error
57 ListObjects(ctx context.Context, bucket string, q *Query, opts ...storageOption) *ObjectIterator
58
59
60
61 DeleteObject(ctx context.Context, bucket, object string, gen int64, conds *Conditions, opts ...storageOption) error
62 GetObject(ctx context.Context, params *getObjectParams, opts ...storageOption) (*ObjectAttrs, error)
63 UpdateObject(ctx context.Context, params *updateObjectParams, opts ...storageOption) (*ObjectAttrs, error)
64 RestoreObject(ctx context.Context, params *restoreObjectParams, opts ...storageOption) (*ObjectAttrs, error)
65
66
67
68 DeleteDefaultObjectACL(ctx context.Context, bucket string, entity ACLEntity, opts ...storageOption) error
69 ListDefaultObjectACLs(ctx context.Context, bucket string, opts ...storageOption) ([]ACLRule, error)
70 UpdateDefaultObjectACL(ctx context.Context, bucket string, entity ACLEntity, role ACLRole, opts ...storageOption) error
71
72
73
74 DeleteBucketACL(ctx context.Context, bucket string, entity ACLEntity, opts ...storageOption) error
75 ListBucketACLs(ctx context.Context, bucket string, opts ...storageOption) ([]ACLRule, error)
76 UpdateBucketACL(ctx context.Context, bucket string, entity ACLEntity, role ACLRole, opts ...storageOption) error
77
78
79
80 DeleteObjectACL(ctx context.Context, bucket, object string, entity ACLEntity, opts ...storageOption) error
81 ListObjectACLs(ctx context.Context, bucket, object string, opts ...storageOption) ([]ACLRule, error)
82 UpdateObjectACL(ctx context.Context, bucket, object string, entity ACLEntity, role ACLRole, opts ...storageOption) error
83
84
85
86 ComposeObject(ctx context.Context, req *composeObjectRequest, opts ...storageOption) (*ObjectAttrs, error)
87 RewriteObject(ctx context.Context, req *rewriteObjectRequest, opts ...storageOption) (*rewriteObjectResponse, error)
88
89 NewRangeReader(ctx context.Context, params *newRangeReaderParams, opts ...storageOption) (*Reader, error)
90 OpenWriter(params *openWriterParams, opts ...storageOption) (*io.PipeWriter, error)
91
92
93
94 GetIamPolicy(ctx context.Context, resource string, version int32, opts ...storageOption) (*iampb.Policy, error)
95 SetIamPolicy(ctx context.Context, resource string, policy *iampb.Policy, opts ...storageOption) error
96 TestIamPermissions(ctx context.Context, resource string, permissions []string, opts ...storageOption) ([]string, error)
97
98
99
100 GetHMACKey(ctx context.Context, project, accessID string, opts ...storageOption) (*HMACKey, error)
101 ListHMACKeys(ctx context.Context, project, serviceAccountEmail string, showDeletedKeys bool, opts ...storageOption) *HMACKeysIterator
102 UpdateHMACKey(ctx context.Context, project, serviceAccountEmail, accessID string, attrs *HMACKeyAttrsToUpdate, opts ...storageOption) (*HMACKey, error)
103 CreateHMACKey(ctx context.Context, project, serviceAccountEmail string, opts ...storageOption) (*HMACKey, error)
104 DeleteHMACKey(ctx context.Context, project, accessID string, opts ...storageOption) error
105
106
107 ListNotifications(ctx context.Context, bucket string, opts ...storageOption) (map[string]*Notification, error)
108 CreateNotification(ctx context.Context, bucket string, n *Notification, opts ...storageOption) (*Notification, error)
109 DeleteNotification(ctx context.Context, bucket string, id string, opts ...storageOption) error
110 }
111
112
113
114
115 type settings struct {
116
117
118 retry *retryConfig
119
120
121
122 gax []gax.CallOption
123
124
125
126 idempotent bool
127
128
129
130
131 clientOption []option.ClientOption
132
133
134 userProject string
135 }
136
137 func initSettings(opts ...storageOption) *settings {
138 s := &settings{}
139 resolveOptions(s, opts...)
140 return s
141 }
142
143 func resolveOptions(s *settings, opts ...storageOption) {
144 for _, o := range opts {
145 o.Apply(s)
146 }
147 }
148
149
150
151
152
153
154 func callSettings(defaults *settings, opts ...storageOption) *settings {
155 if defaults == nil {
156 return nil
157 }
158
159
160
161 cs := *defaults
162 resolveOptions(&cs, opts...)
163 return &cs
164 }
165
166
167
168
169 func makeStorageOpts(isIdempotent bool, retry *retryConfig, userProject string) []storageOption {
170 opts := []storageOption{idempotent(isIdempotent)}
171 if retry != nil {
172 opts = append(opts, withRetryConfig(retry))
173 }
174 if userProject != "" {
175 opts = append(opts, withUserProject(userProject))
176 }
177 return opts
178 }
179
180
181
182 type storageOption interface {
183 Apply(s *settings)
184 }
185
186 func withRetryConfig(rc *retryConfig) storageOption {
187 return &retryOption{rc}
188 }
189
190 type retryOption struct {
191 rc *retryConfig
192 }
193
194 func (o *retryOption) Apply(s *settings) { s.retry = o.rc }
195
196 func idempotent(i bool) storageOption {
197 return &idempotentOption{i}
198 }
199
200 type idempotentOption struct {
201 idempotency bool
202 }
203
204 func (o *idempotentOption) Apply(s *settings) { s.idempotent = o.idempotency }
205
206 func withClientOptions(opts ...option.ClientOption) storageOption {
207 return &clientOption{opts: opts}
208 }
209
210 type clientOption struct {
211 opts []option.ClientOption
212 }
213
214 func (o *clientOption) Apply(s *settings) { s.clientOption = o.opts }
215
216 func withUserProject(project string) storageOption {
217 return &userProjectOption{project}
218 }
219
220 type userProjectOption struct {
221 project string
222 }
223
224 func (o *userProjectOption) Apply(s *settings) { s.userProject = o.project }
225
226 type openWriterParams struct {
227
228
229
230
231
232 ctx context.Context
233
234
235 chunkSize int
236
237
238 chunkRetryDeadline time.Duration
239
240
241
242
243
244 bucket string
245
246
247 attrs *ObjectAttrs
248
249
250 forceEmptyContentType bool
251
252
253 conds *Conditions
254
255
256 encryptionKey []byte
257
258
259 sendCRC32C bool
260
261
262
263
264
265 donec chan struct{}
266
267
268 setError func(error)
269
270
271 progress func(int64)
272
273
274 setObj func(*ObjectAttrs)
275 }
276
277 type newRangeReaderParams struct {
278 bucket string
279 conds *Conditions
280 encryptionKey []byte
281 gen int64
282 length int64
283 object string
284 offset int64
285 readCompressed bool
286 }
287
288 type getObjectParams struct {
289 bucket, object string
290 gen int64
291 encryptionKey []byte
292 conds *Conditions
293 softDeleted bool
294 }
295
296 type updateObjectParams struct {
297 bucket, object string
298 uattrs *ObjectAttrsToUpdate
299 gen int64
300 encryptionKey []byte
301 conds *Conditions
302 overrideRetention *bool
303 }
304
305 type restoreObjectParams struct {
306 bucket, object string
307 gen int64
308 encryptionKey []byte
309 conds *Conditions
310 copySourceACL bool
311 }
312
313 type composeObjectRequest struct {
314 dstBucket string
315 dstObject destinationObject
316 srcs []sourceObject
317 predefinedACL string
318 sendCRC32C bool
319 }
320
321 type sourceObject struct {
322 name string
323 bucket string
324 gen int64
325 conds *Conditions
326 encryptionKey []byte
327 }
328
329 type destinationObject struct {
330 name string
331 bucket string
332 conds *Conditions
333 attrs *ObjectAttrs
334 encryptionKey []byte
335 keyName string
336 }
337
338 type rewriteObjectRequest struct {
339 srcObject sourceObject
340 dstObject destinationObject
341 predefinedACL string
342 token string
343 maxBytesRewrittenPerCall int64
344 }
345
346 type rewriteObjectResponse struct {
347 resource *ObjectAttrs
348 done bool
349 written int64
350 size int64
351 token string
352 }
353
View as plain text