-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Storage.java
1613 lines (1412 loc) · 56.3 KB
/
Storage.java
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.cloud.storage;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.cloud.AuthCredentials;
import com.google.cloud.AuthCredentials.AppEngineAuthCredentials;
import com.google.cloud.AuthCredentials.ServiceAccountAuthCredentials;
import com.google.cloud.FieldSelector;
import com.google.cloud.FieldSelector.Helper;
import com.google.cloud.Page;
import com.google.cloud.ReadChannel;
import com.google.cloud.Service;
import com.google.cloud.ServiceAccountSigner;
import com.google.cloud.ServiceAccountSigner.SigningException;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.spi.StorageRpc;
import com.google.cloud.storage.spi.StorageRpc.Tuple;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* An interface for Google Cloud Storage.
*
* @see <a href="https://cloud.google.com/storage/docs">Google Cloud Storage</a>
*/
public interface Storage extends Service<StorageOptions> {
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;
}
}
enum BucketField implements FieldSelector {
ID("id"),
SELF_LINK("selfLink"),
NAME("name"),
TIME_CREATED("timeCreated"),
METAGENERATION("metageneration"),
ACL("acl"),
DEFAULT_OBJECT_ACL("defaultObjectAcl"),
OWNER("owner"),
LOCATION("location"),
WEBSITE("website"),
VERSIONING("versioning"),
CORS("cors"),
STORAGE_CLASS("storageClass"),
ETAG("etag");
static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(NAME);
private final String selector;
BucketField(String selector) {
this.selector = selector;
}
@Override
public String selector() {
return selector;
}
}
enum BlobField implements FieldSelector {
ACL("acl"),
BUCKET("bucket"),
CACHE_CONTROL("cacheControl"),
COMPONENT_COUNT("componentCount"),
CONTENT_DISPOSITION("contentDisposition"),
CONTENT_ENCODING("contentEncoding"),
CONTENT_LANGUAGE("contentLanguage"),
CONTENT_TYPE("contentType"),
CRC32C("crc32c"),
ETAG("etag"),
GENERATION("generation"),
ID("id"),
KIND("kind"),
MD5HASH("md5Hash"),
MEDIA_LINK("mediaLink"),
METADATA("metadata"),
METAGENERATION("metageneration"),
NAME("name"),
OWNER("owner"),
SELF_LINK("selfLink"),
SIZE("size"),
STORAGE_CLASS("storageClass"),
TIME_DELETED("timeDeleted"),
UPDATED("updated");
static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(BUCKET, NAME);
private final String selector;
BlobField(String selector) {
this.selector = selector;
}
@Override
public String selector() {
return selector;
}
}
/**
* Class for specifying bucket target options.
*/
class BucketTargetOption extends Option {
private static final long serialVersionUID = -5880204616982900975L;
private BucketTargetOption(StorageRpc.Option rpcOption, Object value) {
super(rpcOption, value);
}
private BucketTargetOption(StorageRpc.Option rpcOption) {
this(rpcOption, null);
}
/**
* Returns an option for specifying bucket's predefined ACL configuration.
*/
public static BucketTargetOption predefinedAcl(PredefinedAcl acl) {
return new BucketTargetOption(StorageRpc.Option.PREDEFINED_ACL, acl.entry());
}
/**
* Returns an option for specifying bucket's default ACL configuration for blobs.
*/
public static BucketTargetOption predefinedDefaultObjectAcl(PredefinedAcl acl) {
return new BucketTargetOption(StorageRpc.Option.PREDEFINED_DEFAULT_OBJECT_ACL, acl.entry());
}
/**
* Returns an option for bucket's metageneration match. If this option is used the request will
* fail if metageneration does not match.
*/
public static BucketTargetOption metagenerationMatch() {
return new BucketTargetOption(StorageRpc.Option.IF_METAGENERATION_MATCH);
}
/**
* Returns an option for bucket's metageneration mismatch. If this option is used the request
* will fail if metageneration matches.
*/
public static BucketTargetOption metagenerationNotMatch() {
return new BucketTargetOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH);
}
}
/**
* Class for specifying bucket source options.
*/
class BucketSourceOption extends Option {
private static final long serialVersionUID = 5185657617120212117L;
private BucketSourceOption(StorageRpc.Option rpcOption, long metageneration) {
super(rpcOption, metageneration);
}
/**
* Returns an option for bucket's metageneration match. If this option is used the request will
* fail if bucket's metageneration does not match the provided value.
*/
public static BucketSourceOption metagenerationMatch(long metageneration) {
return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH, metageneration);
}
/**
* Returns an option for bucket's metageneration mismatch. If this option is used the request
* will fail if bucket's metageneration matches the provided value.
*/
public static BucketSourceOption metagenerationNotMatch(long metageneration) {
return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH, metageneration);
}
}
/**
* Class for specifying bucket source options.
*/
class BucketGetOption extends Option {
private static final long serialVersionUID = 1901844869484087395L;
private BucketGetOption(StorageRpc.Option rpcOption, long metageneration) {
super(rpcOption, metageneration);
}
private BucketGetOption(StorageRpc.Option rpcOption, String value) {
super(rpcOption, value);
}
/**
* Returns an option for bucket's metageneration match. If this option is used the request will
* fail if bucket's metageneration does not match the provided value.
*/
public static BucketGetOption metagenerationMatch(long metageneration) {
return new BucketGetOption(StorageRpc.Option.IF_METAGENERATION_MATCH, metageneration);
}
/**
* Returns an option for bucket's metageneration mismatch. If this option is used the request
* will fail if bucket's metageneration matches the provided value.
*/
public static BucketGetOption metagenerationNotMatch(long metageneration) {
return new BucketGetOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH, metageneration);
}
/**
* Returns an option to specify the bucket's fields to be returned by the RPC call. If this
* option is not provided all bucket's fields are returned. {@code BucketGetOption.fields}) can
* be used to specify only the fields of interest. Bucket name is always returned, even if not
* specified.
*/
public static BucketGetOption fields(BucketField... fields) {
return new BucketGetOption(StorageRpc.Option.FIELDS,
Helper.selector(BucketField.REQUIRED_FIELDS, fields));
}
}
/**
* Class for specifying blob target options.
*/
class BlobTargetOption extends Option {
private static final long serialVersionUID = 214616862061934846L;
private BlobTargetOption(StorageRpc.Option rpcOption, Object value) {
super(rpcOption, value);
}
private BlobTargetOption(StorageRpc.Option rpcOption) {
this(rpcOption, null);
}
/**
* Returns an option for specifying blob's predefined ACL configuration.
*/
public static BlobTargetOption predefinedAcl(PredefinedAcl acl) {
return new BlobTargetOption(StorageRpc.Option.PREDEFINED_ACL, acl.entry());
}
/**
* Returns an option that causes an operation to succeed only if the target blob does not exist.
*/
public static BlobTargetOption doesNotExist() {
return new BlobTargetOption(StorageRpc.Option.IF_GENERATION_MATCH, 0L);
}
/**
* Returns an option for blob's data generation match. If this option is used the request will
* fail if generation does not match.
*/
public static BlobTargetOption generationMatch() {
return new BlobTargetOption(StorageRpc.Option.IF_GENERATION_MATCH);
}
/**
* Returns an option for blob's data generation mismatch. If this option is used the request
* will fail if generation matches.
*/
public static BlobTargetOption generationNotMatch() {
return new BlobTargetOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH);
}
/**
* Returns an option for blob's metageneration match. If this option is used the request will
* fail if metageneration does not match.
*/
public static BlobTargetOption metagenerationMatch() {
return new BlobTargetOption(StorageRpc.Option.IF_METAGENERATION_MATCH);
}
/**
* Returns an option for blob's metageneration mismatch. If this option is used the request will
* fail if metageneration matches.
*/
public static BlobTargetOption metagenerationNotMatch() {
return new BlobTargetOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH);
}
static Tuple<BlobInfo, BlobTargetOption[]> convert(BlobInfo info, BlobWriteOption... options) {
BlobInfo.Builder infoBuilder = info.toBuilder().crc32c(null).md5(null);
List<BlobTargetOption> targetOptions = Lists.newArrayListWithCapacity(options.length);
for (BlobWriteOption option : options) {
switch (option.option) {
case IF_CRC32C_MATCH:
infoBuilder.crc32c(info.crc32c());
break;
case IF_MD5_MATCH:
infoBuilder.md5(info.md5());
break;
default:
targetOptions.add(option.toTargetOption());
break;
}
}
return Tuple.of(infoBuilder.build(),
targetOptions.toArray(new BlobTargetOption[targetOptions.size()]));
}
}
/**
* Class for specifying blob write options.
*/
class BlobWriteOption implements Serializable {
private static final long serialVersionUID = -3880421670966224580L;
private final Option option;
private final Object value;
enum Option {
PREDEFINED_ACL, IF_GENERATION_MATCH, IF_GENERATION_NOT_MATCH, IF_METAGENERATION_MATCH,
IF_METAGENERATION_NOT_MATCH, IF_MD5_MATCH, IF_CRC32C_MATCH;
StorageRpc.Option toRpcOption() {
return StorageRpc.Option.valueOf(this.name());
}
}
BlobTargetOption toTargetOption() {
return new BlobTargetOption(this.option.toRpcOption(), this.value);
}
private BlobWriteOption(Option option, Object value) {
this.option = option;
this.value = value;
}
private BlobWriteOption(Option option) {
this(option, null);
}
@Override
public int hashCode() {
return Objects.hash(option, value);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof BlobWriteOption)) {
return false;
}
final BlobWriteOption other = (BlobWriteOption) obj;
return this.option == other.option && Objects.equals(this.value, other.value);
}
/**
* Returns an option for specifying blob's predefined ACL configuration.
*/
public static BlobWriteOption predefinedAcl(PredefinedAcl acl) {
return new BlobWriteOption(Option.PREDEFINED_ACL, acl.entry());
}
/**
* Returns an option that causes an operation to succeed only if the target blob does not exist.
*/
public static BlobWriteOption doesNotExist() {
return new BlobWriteOption(Option.IF_GENERATION_MATCH, 0L);
}
/**
* Returns an option for blob's data generation match. If this option is used the request will
* fail if generation does not match.
*/
public static BlobWriteOption generationMatch() {
return new BlobWriteOption(Option.IF_GENERATION_MATCH);
}
/**
* Returns an option for blob's data generation mismatch. If this option is used the request
* will fail if generation matches.
*/
public static BlobWriteOption generationNotMatch() {
return new BlobWriteOption(Option.IF_GENERATION_NOT_MATCH);
}
/**
* Returns an option for blob's metageneration match. If this option is used the request will
* fail if metageneration does not match.
*/
public static BlobWriteOption metagenerationMatch() {
return new BlobWriteOption(Option.IF_METAGENERATION_MATCH);
}
/**
* Returns an option for blob's metageneration mismatch. If this option is used the request will
* fail if metageneration matches.
*/
public static BlobWriteOption metagenerationNotMatch() {
return new BlobWriteOption(Option.IF_METAGENERATION_NOT_MATCH);
}
/**
* Returns an option for blob's data MD5 hash match. If this option is used the request will
* fail if blobs' data MD5 hash does not match.
*/
public static BlobWriteOption md5Match() {
return new BlobWriteOption(Option.IF_MD5_MATCH, true);
}
/**
* Returns an option for blob's data CRC32C checksum match. If this option is used the request
* will fail if blobs' data CRC32C checksum does not match.
*/
public static BlobWriteOption crc32cMatch() {
return new BlobWriteOption(Option.IF_CRC32C_MATCH, true);
}
}
/**
* Class for specifying blob source options.
*/
class BlobSourceOption extends Option {
private static final long serialVersionUID = -3712768261070182991L;
private BlobSourceOption(StorageRpc.Option rpcOption, Long value) {
super(rpcOption, value);
}
/**
* Returns an option for blob's data generation match. If this option is used the request will
* fail if blob's generation does not match. The generation value to compare with the actual
* blob's generation is taken from a source {@link BlobId} object. When this option is passed
* to a {@link Storage} method and {@link BlobId#generation()} is {@code null} or no
* {@link BlobId} is provided an exception is thrown.
*/
public static BlobSourceOption generationMatch() {
return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH, null);
}
/**
* Returns an option for blob's data generation match. If this option is used the request will
* fail if blob's generation does not match the provided value.
*/
public static BlobSourceOption generationMatch(long generation) {
return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH, generation);
}
/**
* Returns an option for blob's data generation mismatch. If this option is used the request
* will fail if blob's generation matches. The generation value to compare with the actual
* blob's generation is taken from a source {@link BlobId} object. When this option is passed
* to a {@link Storage} method and {@link BlobId#generation()} is {@code null} or no
* {@link BlobId} is provided an exception is thrown.
*/
public static BlobSourceOption generationNotMatch() {
return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH, null);
}
/**
* Returns an option for blob's data generation mismatch. If this option is used the request
* will fail if blob's generation matches the provided value.
*/
public static BlobSourceOption generationNotMatch(long generation) {
return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH, generation);
}
/**
* Returns an option for blob's metageneration match. If this option is used the request will
* fail if blob's metageneration does not match the provided value.
*/
public static BlobSourceOption metagenerationMatch(long metageneration) {
return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH, metageneration);
}
/**
* Returns an option for blob's metageneration mismatch. If this option is used the request will
* fail if blob's metageneration matches the provided value.
*/
public static BlobSourceOption metagenerationNotMatch(long metageneration) {
return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH, metageneration);
}
}
/**
* Class for specifying blob get options.
*/
class BlobGetOption extends Option {
private static final long serialVersionUID = 803817709703661480L;
private BlobGetOption(StorageRpc.Option rpcOption, Long value) {
super(rpcOption, value);
}
private BlobGetOption(StorageRpc.Option rpcOption, String value) {
super(rpcOption, value);
}
/**
* Returns an option for blob's data generation match. If this option is used the request will
* fail if blob's generation does not match. The generation value to compare with the actual
* blob's generation is taken from a source {@link BlobId} object. When this option is passed
* to a {@link Storage} method and {@link BlobId#generation()} is {@code null} or no
* {@link BlobId} is provided an exception is thrown.
*/
public static BlobGetOption generationMatch() {
return new BlobGetOption(StorageRpc.Option.IF_GENERATION_MATCH, (Long) null);
}
/**
* Returns an option for blob's data generation match. If this option is used the request will
* fail if blob's generation does not match the provided value.
*/
public static BlobGetOption generationMatch(long generation) {
return new BlobGetOption(StorageRpc.Option.IF_GENERATION_MATCH, generation);
}
/**
* Returns an option for blob's data generation mismatch. If this option is used the request
* will fail if blob's generation matches. The generation value to compare with the actual
* blob's generation is taken from a source {@link BlobId} object. When this option is passed
* to a {@link Storage} method and {@link BlobId#generation()} is {@code null} or no
* {@link BlobId} is provided an exception is thrown.
*/
public static BlobGetOption generationNotMatch() {
return new BlobGetOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH, (Long) null);
}
/**
* Returns an option for blob's data generation mismatch. If this option is used the request
* will fail if blob's generation matches the provided value.
*/
public static BlobGetOption generationNotMatch(long generation) {
return new BlobGetOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH, generation);
}
/**
* Returns an option for blob's metageneration match. If this option is used the request will
* fail if blob's metageneration does not match the provided value.
*/
public static BlobGetOption metagenerationMatch(long metageneration) {
return new BlobGetOption(StorageRpc.Option.IF_METAGENERATION_MATCH, metageneration);
}
/**
* Returns an option for blob's metageneration mismatch. If this option is used the request will
* fail if blob's metageneration matches the provided value.
*/
public static BlobGetOption metagenerationNotMatch(long metageneration) {
return new BlobGetOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH, metageneration);
}
/**
* Returns an option to specify the blob's fields to be returned by the RPC call. If this option
* is not provided all blob's fields are returned. {@code BlobGetOption.fields}) can be used to
* specify only the fields of interest. Blob name and bucket are always returned, even if not
* specified.
*/
public static BlobGetOption fields(BlobField... fields) {
return new BlobGetOption(StorageRpc.Option.FIELDS,
Helper.selector(BlobField.REQUIRED_FIELDS, fields));
}
}
/**
* Class for specifying bucket list options.
*/
class BucketListOption extends Option {
private static final long serialVersionUID = 8754017079673290353L;
private BucketListOption(StorageRpc.Option option, Object value) {
super(option, value);
}
/**
* Returns an option to specify the maximum number of buckets returned per page.
*/
public static BucketListOption pageSize(long pageSize) {
return new BucketListOption(StorageRpc.Option.MAX_RESULTS, pageSize);
}
/**
* Returns an option to specify the page token from which to start listing buckets.
*/
public static BucketListOption pageToken(String pageToken) {
return new BucketListOption(StorageRpc.Option.PAGE_TOKEN, pageToken);
}
/**
* Returns an option to set a prefix to filter results to buckets whose names begin with this
* prefix.
*/
public static BucketListOption prefix(String prefix) {
return new BucketListOption(StorageRpc.Option.PREFIX, prefix);
}
/**
* Returns an option to specify the bucket's fields to be returned by the RPC call. If this
* option is not provided all bucket's fields are returned. {@code BucketListOption.fields}) can
* be used to specify only the fields of interest. Bucket name is always returned, even if not
* specified.
*/
public static BucketListOption fields(BucketField... fields) {
return new BucketListOption(StorageRpc.Option.FIELDS,
Helper.listSelector("items", BucketField.REQUIRED_FIELDS, fields));
}
}
/**
* Class for specifying blob list options.
*/
class BlobListOption extends Option {
private static final String[] TOP_LEVEL_FIELDS = {"prefixes"};
private static final long serialVersionUID = 9083383524788661294L;
private BlobListOption(StorageRpc.Option option, Object value) {
super(option, value);
}
/**
* Returns an option to specify the maximum number of blobs returned per page.
*/
public static BlobListOption pageSize(long pageSize) {
return new BlobListOption(StorageRpc.Option.MAX_RESULTS, pageSize);
}
/**
* Returns an option to specify the page token from which to start listing blobs.
*/
public static BlobListOption pageToken(String pageToken) {
return new BlobListOption(StorageRpc.Option.PAGE_TOKEN, pageToken);
}
/**
* Returns an option to set a prefix to filter results to blobs whose names begin with this
* prefix.
*/
public static BlobListOption prefix(String prefix) {
return new BlobListOption(StorageRpc.Option.PREFIX, prefix);
}
/**
* If specified, results are returned in a directory-like mode. Blobs whose names, after a
* possible {@link #prefix(String)}, do not contain the '/' delimiter are returned as is. Blobs
* whose names, after a possible {@link #prefix(String)}, contain the '/' delimiter, will have
* their name truncated after the delimiter and will be returned as {@link Blob} objects where
* only {@link Blob#blobId()}, {@link Blob#size()} and {@link Blob#isDirectory()} are set. For
* such directory blobs, ({@link BlobId#generation()} returns {@code null}), {@link Blob#size()}
* returns {@code 0} while {@link Blob#isDirectory()} returns {@code true}. Duplicate directory
* blobs are omitted.
*/
public static BlobListOption currentDirectory() {
return new BlobListOption(StorageRpc.Option.DELIMITER, true);
}
/**
* If set to {@code true}, lists all versions of a blob. The default is {@code false}.
*
* @see <a href ="https://cloud.google.com/storage/docs/object-versioning">Object Versioning</a>
*/
public static BlobListOption versions(boolean versions) {
return new BlobListOption(StorageRpc.Option.VERSIONS, versions);
}
/**
* Returns an option to specify the blob's fields to be returned by the RPC call. If this option
* is not provided all blob's fields are returned. {@code BlobListOption.fields}) can be used to
* specify only the fields of interest. Blob name and bucket are always returned, even if not
* specified.
*/
public static BlobListOption fields(BlobField... fields) {
return new BlobListOption(StorageRpc.Option.FIELDS,
Helper.listSelector(TOP_LEVEL_FIELDS, "items", BlobField.REQUIRED_FIELDS, fields));
}
}
/**
* Class for specifying signed URL options.
*/
class SignUrlOption implements Serializable {
private static final long serialVersionUID = 7850569877451099267L;
private final Option option;
private final Object value;
enum Option {
HTTP_METHOD, CONTENT_TYPE, MD5, SERVICE_ACCOUNT_CRED
}
private SignUrlOption(Option option, Object value) {
this.option = option;
this.value = value;
}
Option option() {
return option;
}
Object value() {
return value;
}
/**
* The HTTP method to be used with the signed URL.
*/
public static SignUrlOption httpMethod(HttpMethod httpMethod) {
return new SignUrlOption(Option.HTTP_METHOD, httpMethod.name());
}
/**
* Use it if signature should include the blob's content-type.
* When used, users of the signed URL should include the blob's content-type with their request.
*/
public static SignUrlOption withContentType() {
return new SignUrlOption(Option.CONTENT_TYPE, true);
}
/**
* Use it if signature should include the blob's md5.
* When used, users of the signed URL should include the blob's md5 with their request.
*/
public static SignUrlOption withMd5() {
return new SignUrlOption(Option.MD5, true);
}
/**
* Provides a service account signer to sign the URL. If not provided an attempt will be made to
* get it from the environment.
*
* @see <a href="https://cloud.google.com/storage/docs/authentication#service_accounts">Service
* account</a>
*/
public static SignUrlOption signWith(ServiceAccountSigner signer) {
return new SignUrlOption(Option.SERVICE_ACCOUNT_CRED, signer);
}
}
/**
* A class to contain all information needed for a Google Cloud Storage Compose operation.
*
* @see <a href="https://cloud.google.com/storage/docs/composite-objects#_Compose">
* Compose Operation</a>
*/
class ComposeRequest implements Serializable {
private static final long serialVersionUID = -7385681353748590911L;
private final List<SourceBlob> sourceBlobs;
private final BlobInfo target;
private final List<BlobTargetOption> targetOptions;
/**
* Class for Compose source blobs.
*/
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 final Set<BlobTargetOption> targetOptions = new LinkedHashSet<>();
private BlobInfo target;
/**
* Add source blobs for compose operation.
*/
public Builder addSource(Iterable<String> blobs) {
for (String blob : blobs) {
sourceBlobs.add(new SourceBlob(blob));
}
return this;
}
/**
* Add source blobs for compose operation.
*/
public Builder addSource(String... blobs) {
return addSource(Arrays.asList(blobs));
}
/**
* Add a source with a specific generation to match.
*/
public Builder addSource(String blob, long generation) {
sourceBlobs.add(new SourceBlob(blob, generation));
return this;
}
/**
* Sets compose operation's target blob.
*/
public Builder target(BlobInfo target) {
this.target = target;
return this;
}
/**
* Sets compose operation's target blob options.
*/
public Builder targetOptions(BlobTargetOption... options) {
Collections.addAll(targetOptions, options);
return this;
}
/**
* Sets compose operation's target blob options.
*/
public Builder targetOptions(Iterable<BlobTargetOption> options) {
Iterables.addAll(targetOptions, options);
return this;
}
/**
* Creates a {@code ComposeRequest} object.
*/
public ComposeRequest build() {
checkArgument(!sourceBlobs.isEmpty());
checkNotNull(target);
return new ComposeRequest(this);
}
}
private ComposeRequest(Builder builder) {
sourceBlobs = ImmutableList.copyOf(builder.sourceBlobs);
target = builder.target;
targetOptions = ImmutableList.copyOf(builder.targetOptions);
}
/**
* Returns compose operation's source blobs.
*/
public List<SourceBlob> sourceBlobs() {
return sourceBlobs;
}
/**
* Returns compose operation's target blob.
*/
public BlobInfo target() {
return target;
}
/**
* Returns compose operation's target blob's options.
*/
public List<BlobTargetOption> targetOptions() {
return targetOptions;
}
/**
* Creates a {@code ComposeRequest} object.
*
* @param sources source blobs names
* @param target target blob
*/
public static ComposeRequest of(Iterable<String> sources, BlobInfo target) {
return builder().target(target).addSource(sources).build();
}
/**
* Creates a {@code ComposeRequest} object.
*
* @param bucket name of the bucket where the compose operation takes place
* @param sources source blobs names
* @param target target blob name
*/
public static ComposeRequest of(String bucket, Iterable<String> sources, String target) {
return of(sources, BlobInfo.builder(BlobId.of(bucket, target)).build());
}
/**
* Returns a {@code ComposeRequest} builder.
*/
public static Builder builder() {
return new Builder();
}
}
/**
* A class to contain all information needed for a Google Cloud Storage Copy operation.
*/
class CopyRequest implements Serializable {
private static final long serialVersionUID = -4498650529476219937L;
private final BlobId source;
private final List<BlobSourceOption> sourceOptions;
private final boolean overrideInfo;
private final BlobInfo target;
private final List<BlobTargetOption> targetOptions;
private final Long megabytesCopiedPerChunk;
public static class Builder {
private final Set<BlobSourceOption> sourceOptions = new LinkedHashSet<>();
private final Set<BlobTargetOption> targetOptions = new LinkedHashSet<>();
private BlobId source;
private boolean overrideInfo;
private BlobInfo target;
private Long megabytesCopiedPerChunk;
/**
* Sets the blob to copy given bucket and blob name.
*
* @return the builder
*/
public Builder source(String bucket, String blob) {
this.source = BlobId.of(bucket, blob);
return this;
}
/**
* Sets the blob to copy given a {@link BlobId}.
*
* @return the builder
*/
public Builder source(BlobId source) {
this.source = source;
return this;
}
/**
* Sets blob's source options.
*
* @return the builder
*/
public Builder sourceOptions(BlobSourceOption... options) {
Collections.addAll(sourceOptions, options);
return this;
}
/**
* Sets blob's source options.