-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathStorageService.java
More file actions
430 lines (326 loc) · 11.7 KB
/
StorageService.java
File metadata and controls
430 lines (326 loc) · 11.7 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* Copyright 2015 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 com.google.gcloud.storage;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableList;
import com.google.gcloud.Service;
import com.google.gcloud.spi.StorageRpc;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public interface StorageService extends Service<StorageServiceOptions> {
// todo: provide way for construct signed URLs -
// https://cloud.google.com/storage/docs/access-control#Signed-URLs
enum PredefinedAcl {
AUTHENTICATED_READ("authenticatedRead"),
ALL_AUTHENTICATED_USERS("allAuthenticatedUsers"),
PRIVATE("private"),
PROJECT_PRIVATE("projectPrivate"),
PUBLIC_READ("publicRead"),
PUBLIC_READ_WRITE("publicReadWrite"),
BUCKET_OWNER_READ("bucketOwnerRead"),
BUCKET_OWNER_FULL_CONTROL("bucketOwnerFullControl");
private final String entry;
PredefinedAcl(String entry) {
this.entry = entry;
}
String entry() {
return entry;
}
}
class BucketTargetOption extends Option {
private static final long serialVersionUID = -5880204616982900975L;
private BucketTargetOption(StorageRpc.Option rpcOption, Object value) {
super(rpcOption, value);
}
public static BucketTargetOption predefinedAcl(PredefinedAcl acl) {
return new BucketTargetOption(StorageRpc.Option.PREDEFINED_ACL, acl.entry());
}
public static BucketTargetOption predefinedDefaultObjectAcl(PredefinedAcl acl) {
return new BucketTargetOption(StorageRpc.Option.PREDEFINED_DEFAULT_OBJECT_ACL, acl.entry());
}
public static BucketTargetOption metagenerationMatch(boolean match) {
return new BucketTargetOption(StorageRpc.Option.IF_METAGENERATION_MATCH, match);
}
}
class BucketSourceOption extends Option {
private static final long serialVersionUID = 5185657617120212117L;
private BucketSourceOption(StorageRpc.Option rpcOption, Object value) {
super(rpcOption, value);
}
public static BucketSourceOption metagenerationMatch(boolean match) {
return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH, match);
}
}
class BlobTargetOption extends Option {
private static final long serialVersionUID = 214616862061934846L;
private BlobTargetOption(StorageRpc.Option rpcOption, Object value) {
super(rpcOption, value);
}
public static BlobTargetOption predefinedAcl(PredefinedAcl acl) {
return new BlobTargetOption(StorageRpc.Option.PREDEFINED_ACL, acl.entry());
}
public static BlobTargetOption generationMath(boolean match) {
return new BlobTargetOption(StorageRpc.Option.IF_GENERATION_MATCH, match);
}
public static BlobTargetOption metagenerationMatch(boolean match) {
return new BlobTargetOption(StorageRpc.Option.IF_METAGENERATION_MATCH, match);
}
}
class BlobSourceOption extends Option {
private static final long serialVersionUID = -3712768261070182991L;
private BlobSourceOption(StorageRpc.Option rpcOption, Object value) {
super(rpcOption, value);
}
public static BlobSourceOption generationMath(boolean match) {
return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH, match);
}
public static BlobSourceOption metagenerationMatch(boolean match) {
return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH, match);
}
}
class BucketListOption extends Option {
private static final long serialVersionUID = 8754017079673290353L;
private BucketListOption(StorageRpc.Option option, Object value) {
super(option, value);
}
public static BucketListOption maxResults(long maxResults) {
return new BucketListOption(StorageRpc.Option.MAX_RESULTS, maxResults);
}
public static BucketListOption startPageToken(String pageToken) {
return new BucketListOption(StorageRpc.Option.PAGE_TOKEN, pageToken);
}
public static BucketListOption prefix(String prefix) {
return new BucketListOption(StorageRpc.Option.PREFIX, prefix);
}
}
class BlobListOption extends Option {
private static final long serialVersionUID = 9083383524788661294L;
private BlobListOption(StorageRpc.Option option, Object value) {
super(option, value);
}
public static BlobListOption maxResults(long maxResults) {
return new BlobListOption(StorageRpc.Option.MAX_RESULTS, maxResults);
}
public static BlobListOption startPageToken(String pageToken) {
return new BlobListOption(StorageRpc.Option.PAGE_TOKEN, pageToken);
}
public static BlobListOption prefix(String prefix) {
return new BlobListOption(StorageRpc.Option.PREFIX, prefix);
}
public static BlobListOption recursive(boolean recursive) {
return new BlobListOption(StorageRpc.Option.DELIMITER, recursive);
}
}
class ComposeRequest implements Serializable {
private static final long serialVersionUID = -7385681353748590911L;
private final List<SourceBlob> sourceBlobs;
private final Blob target;
private final List<BlobTargetOption> targetOptions;
public static class SourceBlob implements Serializable {
private static final long serialVersionUID = 4094962795951990439L;
final String name;
final Long generation;
SourceBlob(String name) {
this(name, null);
}
SourceBlob(String name, Long generation) {
this.name = name;
this.generation = generation;
}
public String name() {
return name;
}
public Long generation() {
return generation;
}
}
public static class Builder {
private final List<SourceBlob> sourceBlobs = new LinkedList<>();
private Blob target;
private final Set<BlobTargetOption> targetOptions = new LinkedHashSet<>();
public Builder addSource(Iterable<String> blobs) {
for (String blob : blobs) {
sourceBlobs.add(new SourceBlob(blob));
}
return this;
}
public Builder addSource(String... blobs) {
return addSource(Arrays.asList(blobs));
}
public Builder addSource(String blob, long matchGeneration) {
sourceBlobs.add(new SourceBlob(blob, matchGeneration));
return this;
}
public Builder target(Blob target) {
this.target = target;
return this;
}
public Builder targetOptions(BlobTargetOption... options) {
Collections.addAll(targetOptions, options);
return this;
}
public ComposeRequest build() {
checkNotNull(target);
return new ComposeRequest(this);
}
}
private ComposeRequest(Builder builder) {
sourceBlobs = ImmutableList.copyOf(builder.sourceBlobs);
target = builder.target;
targetOptions = ImmutableList.copyOf(builder.targetOptions);
}
public List<SourceBlob> sourceBlobs() {
return sourceBlobs;
}
public Blob target() {
return target;
}
public List<BlobTargetOption> targetOptions() {
return targetOptions;
}
public static ComposeRequest of(Iterable<String> sources, Blob target) {
return builder().target(target).addSource(sources).build();
}
public static Builder builder() {
return new Builder();
}
}
class CopyRequest implements Serializable {
private static final long serialVersionUID = -2606508373751748775L;
private final Blob source;
private final List<BlobSourceOption> sourceOptions;
private final Blob target;
private final List<BlobTargetOption> targetOptions;
public static class Builder {
private Blob source;
private final Set<BlobSourceOption> sourceOptions = new LinkedHashSet<>();
private Blob target;
private final Set<BlobTargetOption> targetOptions = new LinkedHashSet<>();
public Builder source(Blob source) {
this.source = source;
return this;
}
public Builder sourceOptions(BlobSourceOption... options) {
Collections.addAll(sourceOptions, options);
return this;
}
public Builder target(Blob target) {
this.target = target;
return this;
}
public Builder targetOptions(BlobTargetOption... options) {
Collections.addAll(targetOptions, options);
return this;
}
public CopyRequest build() {
checkNotNull(source);
checkNotNull(target);
return new CopyRequest(this);
}
}
private CopyRequest(Builder builder) {
source = checkNotNull(builder.source);
sourceOptions = ImmutableList.copyOf(builder.sourceOptions);
target = checkNotNull(builder.target);
targetOptions = ImmutableList.copyOf(builder.targetOptions);
}
public Blob source() {
return source;
}
public List<BlobSourceOption> sourceOptions() {
return sourceOptions;
}
public Blob target() {
return target;
}
public List<BlobTargetOption> targetOptions() {
return targetOptions;
}
public static CopyRequest of(Blob source, Blob target) {
return builder().source(source).target(target).build();
}
public static Builder builder() {
return new Builder();
}
}
/**
* @throws StorageServiceException upon failure
*/
Bucket create(Bucket bucket, BucketTargetOption... options);
/**
* @throws StorageServiceException upon failure
*/
Blob create(Blob blob, byte[] content, BlobTargetOption... options);
/**
* @throws StorageServiceException upon failure
*/
Bucket get(Bucket bucket, BucketSourceOption... options);
/**
* @throws StorageServiceException upon failure
*/
Blob get(Blob blob, BlobSourceOption... options);
/**
* @throws StorageServiceException upon failure
*/
ListResult<Bucket> list(BucketListOption... options);
/**
* Lists blobs for a bucket.
* @throws StorageServiceException upon failure
*/
ListResult<Blob> list(String bucket, BlobListOption... options);
/**
* @throws StorageServiceException upon failure
*/
Bucket update(Bucket bucket, BucketTargetOption... options);
/**
* @throws StorageServiceException upon failure
*/
Blob update(Blob blob, BlobTargetOption... options);
/**
* @throws StorageServiceException upon failure
*/
boolean delete(Bucket bucket, BucketSourceOption... options);
/**
* @throws StorageServiceException upon failure
*/
boolean delete(Blob blob, BlobSourceOption... options);
/**
* @throws StorageServiceException upon failure
*/
Blob compose(ComposeRequest composeRequest);
/**
* @throws StorageServiceException upon failure
*/
Blob copy(CopyRequest copyRequest);
/**
* @throws StorageServiceException upon failure
*/
byte[] load(Blob blob, BlobSourceOption... options);
BatchResponse apply(BatchRequest batchRequest);
/**
* @throws StorageServiceException upon failure
*/
BlobReadChannel reader(Blob blob, BlobSourceOption... options);
/**
* @throws StorageServiceException upon failure
*/
BlobWriteChannel writer(Blob blob, BlobTargetOption... options);
}