/
BlobInfo.java
1155 lines (1023 loc) · 37.6 KB
/
BlobInfo.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 LLC
*
* 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.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.client.util.Data;
import com.google.api.client.util.DateTime;
import com.google.api.core.BetaApi;
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.api.services.storage.model.StorageObject;
import com.google.api.services.storage.model.StorageObject.Owner;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.BaseEncoding;
import java.io.Serializable;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* Google Storage object metadata.
*
* @see <a href="https://cloud.google.com/storage/docs/concepts-techniques#concepts">Concepts and
* Terminology</a>
*/
public class BlobInfo implements Serializable {
static final Function<BlobInfo, StorageObject> INFO_TO_PB_FUNCTION =
new Function<BlobInfo, StorageObject>() {
@Override
public StorageObject apply(BlobInfo blobInfo) {
return blobInfo.toPb();
}
};
private static final long serialVersionUID = -5625857076205028976L;
private final BlobId blobId;
private final String generatedId;
private final String selfLink;
private final String cacheControl;
private final List<Acl> acl;
private final Acl.Entity owner;
private final Long size;
private final String etag;
private final String md5;
private final String crc32c;
private final String mediaLink;
private final Map<String, String> metadata;
private final Long metageneration;
private final Long deleteTime;
private final Long updateTime;
private final Long createTime;
private final String contentType;
private final String contentEncoding;
private final String contentDisposition;
private final String contentLanguage;
private final StorageClass storageClass;
private final Integer componentCount;
private final boolean isDirectory;
private final CustomerEncryption customerEncryption;
private final String kmsKeyName;
private final Boolean eventBasedHold;
private final Boolean temporaryHold;
private final Long retentionExpirationTime;
/** This class is meant for internal use only. Users are discouraged from using this class. */
public static final class ImmutableEmptyMap<K, V> extends AbstractMap<K, V> {
@Override
public Set<Entry<K, V>> entrySet() {
return ImmutableSet.of();
}
}
/**
* Objects of this class hold information on the customer-supplied encryption key, if the blob is
* encrypted using such a key.
*/
public static class CustomerEncryption implements Serializable {
private static final long serialVersionUID = -2133042982786959351L;
private final String encryptionAlgorithm;
private final String keySha256;
CustomerEncryption(String encryptionAlgorithm, String keySha256) {
this.encryptionAlgorithm = encryptionAlgorithm;
this.keySha256 = keySha256;
}
/** Returns the algorithm used to encrypt the blob. */
public String getEncryptionAlgorithm() {
return encryptionAlgorithm;
}
/** Returns the SHA256 hash of the encryption key. */
public String getKeySha256() {
return keySha256;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("encryptionAlgorithm", getEncryptionAlgorithm())
.add("keySha256", getKeySha256())
.toString();
}
@Override
public final int hashCode() {
return Objects.hash(encryptionAlgorithm, keySha256);
}
@Override
public final boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(CustomerEncryption.class)
&& Objects.equals(toPb(), ((CustomerEncryption) obj).toPb());
}
StorageObject.CustomerEncryption toPb() {
return new StorageObject.CustomerEncryption()
.setEncryptionAlgorithm(encryptionAlgorithm)
.setKeySha256(keySha256);
}
static CustomerEncryption fromPb(StorageObject.CustomerEncryption customerEncryptionPb) {
return new CustomerEncryption(
customerEncryptionPb.getEncryptionAlgorithm(), customerEncryptionPb.getKeySha256());
}
}
/** Builder for {@code BlobInfo}. */
public abstract static class Builder {
/** Sets the blob identity. */
public abstract Builder setBlobId(BlobId blobId);
abstract Builder setGeneratedId(String generatedId);
/**
* Sets the blob's data content type.
*
* @see <a href="https://tools.ietf.org/html/rfc2616#section-14.17">Content-Type</a>
*/
public abstract Builder setContentType(String contentType);
/**
* Sets the blob's data content disposition.
*
* @see <a href="https://tools.ietf.org/html/rfc6266">Content-Disposition</a>
*/
public abstract Builder setContentDisposition(String contentDisposition);
/**
* Sets the blob's data content language.
*
* @see <a href="http://tools.ietf.org/html/bcp47">Content-Language</a>
*/
public abstract Builder setContentLanguage(String contentLanguage);
/**
* Sets the blob's data content encoding.
*
* @see <a href="https://tools.ietf.org/html/rfc7231#section-3.1.2.2">Content-Encoding</a>
*/
public abstract Builder setContentEncoding(String contentEncoding);
abstract Builder setComponentCount(Integer componentCount);
/**
* Sets the blob's data cache control.
*
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2">Cache-Control</a>
*/
public abstract Builder setCacheControl(String cacheControl);
/**
* Sets the blob's access control configuration.
*
* @see <a
* href="https://cloud.google.com/storage/docs/access-control#About-Access-Control-Lists">
* About Access Control Lists</a>
*/
public abstract Builder setAcl(List<Acl> acl);
abstract Builder setOwner(Acl.Entity owner);
abstract Builder setSize(Long size);
abstract Builder setEtag(String etag);
abstract Builder setSelfLink(String selfLink);
/**
* Sets the MD5 hash of blob's data. MD5 value must be encoded in base64.
*
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
* Best Practices</a>
*/
public abstract Builder setMd5(String md5);
/**
* Sets the MD5 hash of blob's data from hex string.
*
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
* Best Practices</a>
* @throws IllegalArgumentException when given an invalid hexadecimal value.
*/
public abstract Builder setMd5FromHexString(String md5HexString);
/**
* Sets the CRC32C checksum of blob's data as described in <a
* href="http://tools.ietf.org/html/rfc4960#appendix-B">RFC 4960, Appendix B;</a> encoded in
* base64 in big-endian order.
*
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
* Best Practices</a>
*/
public abstract Builder setCrc32c(String crc32c);
/**
* Sets the CRC32C checksum of blob's data as described in <a
* href="http://tools.ietf.org/html/rfc4960#appendix-B">RFC 4960, Appendix B;</a> from hex
* string.
*
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
* Best Practices</a>
* @throws IllegalArgumentException when given an invalid hexadecimal value.
*/
public abstract Builder setCrc32cFromHexString(String crc32cHexString);
abstract Builder setMediaLink(String mediaLink);
/** Sets the blob's storage class. */
public abstract Builder setStorageClass(StorageClass storageClass);
/** Sets the blob's user provided metadata. */
public abstract Builder setMetadata(Map<String, String> metadata);
abstract Builder setMetageneration(Long metageneration);
abstract Builder setDeleteTime(Long deleteTime);
abstract Builder setUpdateTime(Long updateTime);
abstract Builder setCreateTime(Long createTime);
abstract Builder setIsDirectory(boolean isDirectory);
abstract Builder setCustomerEncryption(CustomerEncryption customerEncryption);
abstract Builder setKmsKeyName(String kmsKeyName);
/** Sets the blob's event-based hold. */
@BetaApi
public abstract Builder setEventBasedHold(Boolean eventBasedHold);
/** Sets the blob's temporary hold. */
@BetaApi
public abstract Builder setTemporaryHold(Boolean temporaryHold);
@BetaApi
abstract Builder setRetentionExpirationTime(Long retentionExpirationTime);
/** Creates a {@code BlobInfo} object. */
public abstract BlobInfo build();
}
static final class BuilderImpl extends Builder {
private final String hexDecimalValues = "0123456789abcdef";
private BlobId blobId;
private String generatedId;
private String contentType;
private String contentEncoding;
private String contentDisposition;
private String contentLanguage;
private Integer componentCount;
private String cacheControl;
private List<Acl> acl;
private Acl.Entity owner;
private Long size;
private String etag;
private String selfLink;
private String md5;
private String crc32c;
private String mediaLink;
private Map<String, String> metadata;
private Long metageneration;
private Long deleteTime;
private Long updateTime;
private Long createTime;
private Boolean isDirectory;
private CustomerEncryption customerEncryption;
private StorageClass storageClass;
private String kmsKeyName;
private Boolean eventBasedHold;
private Boolean temporaryHold;
private Long retentionExpirationTime;
BuilderImpl(BlobId blobId) {
this.blobId = blobId;
}
BuilderImpl(BlobInfo blobInfo) {
blobId = blobInfo.blobId;
generatedId = blobInfo.generatedId;
cacheControl = blobInfo.cacheControl;
contentEncoding = blobInfo.contentEncoding;
contentType = blobInfo.contentType;
contentDisposition = blobInfo.contentDisposition;
contentLanguage = blobInfo.contentLanguage;
componentCount = blobInfo.componentCount;
customerEncryption = blobInfo.customerEncryption;
acl = blobInfo.acl;
owner = blobInfo.owner;
size = blobInfo.size;
etag = blobInfo.etag;
selfLink = blobInfo.selfLink;
md5 = blobInfo.md5;
crc32c = blobInfo.crc32c;
mediaLink = blobInfo.mediaLink;
metadata = blobInfo.metadata;
metageneration = blobInfo.metageneration;
deleteTime = blobInfo.deleteTime;
updateTime = blobInfo.updateTime;
createTime = blobInfo.createTime;
isDirectory = blobInfo.isDirectory;
storageClass = blobInfo.storageClass;
kmsKeyName = blobInfo.kmsKeyName;
eventBasedHold = blobInfo.eventBasedHold;
temporaryHold = blobInfo.temporaryHold;
retentionExpirationTime = blobInfo.retentionExpirationTime;
}
@Override
public Builder setBlobId(BlobId blobId) {
this.blobId = checkNotNull(blobId);
return this;
}
@Override
Builder setGeneratedId(String generatedId) {
this.generatedId = generatedId;
return this;
}
@Override
public Builder setContentType(String contentType) {
this.contentType = firstNonNull(contentType, Data.<String>nullOf(String.class));
return this;
}
@Override
public Builder setContentDisposition(String contentDisposition) {
this.contentDisposition = firstNonNull(contentDisposition, Data.<String>nullOf(String.class));
return this;
}
@Override
public Builder setContentLanguage(String contentLanguage) {
this.contentLanguage = firstNonNull(contentLanguage, Data.<String>nullOf(String.class));
return this;
}
@Override
public Builder setContentEncoding(String contentEncoding) {
this.contentEncoding = firstNonNull(contentEncoding, Data.<String>nullOf(String.class));
return this;
}
@Override
Builder setComponentCount(Integer componentCount) {
this.componentCount = componentCount;
return this;
}
@Override
public Builder setCacheControl(String cacheControl) {
this.cacheControl = firstNonNull(cacheControl, Data.<String>nullOf(String.class));
return this;
}
@Override
public Builder setAcl(List<Acl> acl) {
this.acl = acl != null ? ImmutableList.copyOf(acl) : null;
return this;
}
@Override
Builder setOwner(Acl.Entity owner) {
this.owner = owner;
return this;
}
@Override
Builder setSize(Long size) {
this.size = size;
return this;
}
@Override
Builder setEtag(String etag) {
this.etag = etag;
return this;
}
@Override
Builder setSelfLink(String selfLink) {
this.selfLink = selfLink;
return this;
}
@Override
public Builder setMd5(String md5) {
this.md5 = firstNonNull(md5, Data.<String>nullOf(String.class));
return this;
}
@Override
public Builder setMd5FromHexString(String md5HexString) {
if (md5HexString == null) {
return this;
}
if (md5HexString.length() % 2 != 0) {
throw new IllegalArgumentException(
"each byte must be represented by 2 valid hexadecimal characters");
}
String md5HexStringLower = md5HexString.toLowerCase();
ByteBuffer md5ByteBuffer = ByteBuffer.allocate(md5HexStringLower.length() / 2);
for (int charIndex = 0; charIndex < md5HexStringLower.length(); charIndex += 2) {
int higherOrderBits = this.hexDecimalValues.indexOf(md5HexStringLower.charAt(charIndex));
int lowerOrderBits = this.hexDecimalValues.indexOf(md5HexStringLower.charAt(charIndex + 1));
if (higherOrderBits == -1 || lowerOrderBits == -1) {
throw new IllegalArgumentException(
"each byte must be represented by 2 valid hexadecimal characters");
}
md5ByteBuffer.put((byte) (higherOrderBits << 4 | lowerOrderBits));
}
this.md5 = BaseEncoding.base64().encode(md5ByteBuffer.array());
return this;
}
@Override
public Builder setCrc32c(String crc32c) {
this.crc32c = firstNonNull(crc32c, Data.<String>nullOf(String.class));
return this;
}
@Override
public Builder setCrc32cFromHexString(String crc32cHexString) {
if (crc32cHexString == null) {
return this;
}
if (crc32cHexString.length() % 2 != 0) {
throw new IllegalArgumentException(
"each byte must be represented by 2 valid hexadecimal characters");
}
String crc32cHexStringLower = crc32cHexString.toLowerCase();
ByteBuffer crc32cByteBuffer = ByteBuffer.allocate(crc32cHexStringLower.length() / 2);
for (int charIndex = 0; charIndex < crc32cHexStringLower.length(); charIndex += 2) {
int higherOrderBits = this.hexDecimalValues.indexOf(crc32cHexStringLower.charAt(charIndex));
int lowerOrderBits =
this.hexDecimalValues.indexOf(crc32cHexStringLower.charAt(charIndex + 1));
if (higherOrderBits == -1 || lowerOrderBits == -1) {
throw new IllegalArgumentException(
"each byte must be represented by 2 valid hexadecimal characters");
}
crc32cByteBuffer.put((byte) (higherOrderBits << 4 | lowerOrderBits));
}
this.crc32c = BaseEncoding.base64().encode(crc32cByteBuffer.array());
return this;
}
@Override
Builder setMediaLink(String mediaLink) {
this.mediaLink = mediaLink;
return this;
}
@Override
public Builder setMetadata(Map<String, String> metadata) {
if (metadata != null) {
this.metadata = new HashMap<>(metadata);
} else {
this.metadata = (Map<String, String>) Data.nullOf(ImmutableEmptyMap.class);
}
return this;
}
@Override
public Builder setStorageClass(StorageClass storageClass) {
this.storageClass = storageClass;
return this;
}
@Override
Builder setMetageneration(Long metageneration) {
this.metageneration = metageneration;
return this;
}
@Override
Builder setDeleteTime(Long deleteTime) {
this.deleteTime = deleteTime;
return this;
}
@Override
Builder setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
return this;
}
@Override
Builder setCreateTime(Long createTime) {
this.createTime = createTime;
return this;
}
@Override
Builder setIsDirectory(boolean isDirectory) {
this.isDirectory = isDirectory;
return this;
}
@Override
Builder setCustomerEncryption(CustomerEncryption customerEncryption) {
this.customerEncryption = customerEncryption;
return this;
}
@Override
Builder setKmsKeyName(String kmsKeyName) {
this.kmsKeyName = kmsKeyName;
return this;
}
@Override
public Builder setEventBasedHold(Boolean eventBasedHold) {
this.eventBasedHold = eventBasedHold;
return this;
}
@Override
public Builder setTemporaryHold(Boolean temporaryHold) {
this.temporaryHold = temporaryHold;
return this;
}
@Override
Builder setRetentionExpirationTime(Long retentionExpirationTime) {
this.retentionExpirationTime = retentionExpirationTime;
return this;
}
@Override
public BlobInfo build() {
checkNotNull(blobId);
return new BlobInfo(this);
}
}
BlobInfo(BuilderImpl builder) {
blobId = builder.blobId;
generatedId = builder.generatedId;
cacheControl = builder.cacheControl;
contentEncoding = builder.contentEncoding;
contentType = builder.contentType;
contentDisposition = builder.contentDisposition;
contentLanguage = builder.contentLanguage;
componentCount = builder.componentCount;
customerEncryption = builder.customerEncryption;
acl = builder.acl;
owner = builder.owner;
size = builder.size;
etag = builder.etag;
selfLink = builder.selfLink;
md5 = builder.md5;
crc32c = builder.crc32c;
mediaLink = builder.mediaLink;
metadata = builder.metadata;
metageneration = builder.metageneration;
deleteTime = builder.deleteTime;
updateTime = builder.updateTime;
createTime = builder.createTime;
isDirectory = firstNonNull(builder.isDirectory, Boolean.FALSE);
storageClass = builder.storageClass;
kmsKeyName = builder.kmsKeyName;
eventBasedHold = builder.eventBasedHold;
temporaryHold = builder.temporaryHold;
retentionExpirationTime = builder.retentionExpirationTime;
}
/** Returns the blob's identity. */
public BlobId getBlobId() {
return blobId;
}
/** Returns the name of the containing bucket. */
public String getBucket() {
return getBlobId().getBucket();
}
/** Returns the service-generated for the blob. */
public String getGeneratedId() {
return generatedId;
}
/** Returns the blob's name. */
public String getName() {
return getBlobId().getName();
}
/**
* Returns the blob's data cache control.
*
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2">Cache-Control</a>
*/
public String getCacheControl() {
return Data.isNull(cacheControl) ? null : cacheControl;
}
/**
* Returns the blob's access control configuration.
*
* @see <a href="https://cloud.google.com/storage/docs/access-control#About-Access-Control-Lists">
* About Access Control Lists</a>
*/
public List<Acl> getAcl() {
return acl;
}
/** Returns the blob's owner. This will always be the uploader of the blob. */
public Acl.Entity getOwner() {
return owner;
}
/**
* Returns the content length of the data in bytes.
*
* @see <a href="https://tools.ietf.org/html/rfc2616#section-14.13">Content-Length</a>
*/
public Long getSize() {
return size;
}
/**
* Returns the blob's data content type.
*
* @see <a href="https://tools.ietf.org/html/rfc2616#section-14.17">Content-Type</a>
*/
public String getContentType() {
return Data.isNull(contentType) ? null : contentType;
}
/**
* Returns the blob's data content encoding.
*
* @see <a href="https://tools.ietf.org/html/rfc7231#section-3.1.2.2">Content-Encoding</a>
*/
public String getContentEncoding() {
return Data.isNull(contentEncoding) ? null : contentEncoding;
}
/**
* Returns the blob's data content disposition.
*
* @see <a href="https://tools.ietf.org/html/rfc6266">Content-Disposition</a>
*/
public String getContentDisposition() {
return Data.isNull(contentDisposition) ? null : contentDisposition;
}
/**
* Returns the blob's data content language.
*
* @see <a href="http://tools.ietf.org/html/bcp47">Content-Language</a>
*/
public String getContentLanguage() {
return Data.isNull(contentLanguage) ? null : contentLanguage;
}
/**
* Returns the number of components that make up this blob. Components are accumulated through the
* {@link Storage#compose(Storage.ComposeRequest)} operation and are limited to a count of 1024,
* counting 1 for each non-composite component blob and componentCount for each composite
* component blob. This value is set only for composite blobs.
*
* @see <a href="https://cloud.google.com/storage/docs/composite-objects#_Count">Component Count
* Property</a>
*/
public Integer getComponentCount() {
return componentCount;
}
/**
* Returns HTTP 1.1 Entity tag for the blob.
*
* @see <a href="http://tools.ietf.org/html/rfc2616#section-3.11">Entity Tags</a>
*/
public String getEtag() {
return etag;
}
/** Returns the URI of this blob as a string. */
public String getSelfLink() {
return selfLink;
}
/**
* Returns the MD5 hash of blob's data encoded in base64.
*
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
* Best Practices</a>
*/
public String getMd5() {
return Data.isNull(md5) ? null : md5;
}
/**
* Returns the MD5 hash of blob's data decoded to string.
*
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
* Best Practices</a>
*/
public String getMd5ToHexString() {
if (md5 == null) {
return null;
}
byte[] decodedMd5 = BaseEncoding.base64().decode(md5);
StringBuilder stringBuilder = new StringBuilder();
for (byte b : decodedMd5) {
stringBuilder.append(String.format("%02x", b & 0xff));
}
return stringBuilder.toString();
}
/**
* Returns the CRC32C checksum of blob's data as described in <a
* href="http://tools.ietf.org/html/rfc4960#appendix-B">RFC 4960, Appendix B;</a> encoded in
* base64 in big-endian order.
*
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
* Best Practices</a>
*/
public String getCrc32c() {
return Data.isNull(crc32c) ? null : crc32c;
}
/**
* Returns the CRC32C checksum of blob's data as described in <a
* href="http://tools.ietf.org/html/rfc4960#appendix-B">RFC 4960, Appendix B;</a> decoded to
* string.
*
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
* Best Practices</a>
*/
public String getCrc32cToHexString() {
if (crc32c == null) {
return null;
}
byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32c);
StringBuilder stringBuilder = new StringBuilder();
for (byte b : decodeCrc32c) {
stringBuilder.append(String.format("%02x", b & 0xff));
}
return stringBuilder.toString();
}
/** Returns the blob's media download link. */
public String getMediaLink() {
return mediaLink;
}
/** Returns blob's user provided metadata. */
public Map<String, String> getMetadata() {
return metadata == null || Data.isNull(metadata) ? null : Collections.unmodifiableMap(metadata);
}
/** Returns blob's data generation. Used for blob versioning. */
public Long getGeneration() {
return getBlobId().getGeneration();
}
/**
* Returns blob's metageneration. 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 blob.
*/
public Long getMetageneration() {
return metageneration;
}
/**
* Returns the deletion time of the blob expressed as the number of milliseconds since the Unix
* epoch.
*/
public Long getDeleteTime() {
return deleteTime;
}
/**
* Returns the last modification time of the blob's metadata expressed as the number of
* milliseconds since the Unix epoch.
*/
public Long getUpdateTime() {
return updateTime;
}
/**
* Returns the creation time of the blob expressed as the number of milliseconds since the Unix
* epoch.
*/
public Long getCreateTime() {
return createTime;
}
/**
* Returns {@code true} if the current blob represents a directory. This can only happen if the
* blob is returned by {@link Storage#list(String, Storage.BlobListOption...)} when the {@link
* Storage.BlobListOption#currentDirectory()} option is used. When this is the case only {@link
* #getBlobId()} and {@link #getSize()} are set for the current blob: {@link BlobId#getName()}
* ends with the '/' character, {@link BlobId#getGeneration()} returns {@code null} and {@link
* #getSize()} is {@code 0}.
*/
public boolean isDirectory() {
return isDirectory;
}
/**
* Returns information on the customer-supplied encryption key, if the blob is encrypted using
* such a key.
*/
public CustomerEncryption getCustomerEncryption() {
return customerEncryption;
}
/** Returns the storage class of the blob. */
public StorageClass getStorageClass() {
return storageClass;
}
/** Returns the Cloud KMS key used to encrypt the blob, if any. */
public String getKmsKeyName() {
return kmsKeyName;
}
/**
* Returns a {@code Boolean} with either {@code true}, {@code null} and in certain cases {@code
* false}.
*
* <p>Case 1: {@code true} the field {@link
* com.google.cloud.storage.Storage.BlobField#EVENT_BASED_HOLD} is selected in a {@link
* Storage#get(BlobId, Storage.BlobGetOption...)} and event-based hold for the blob is enabled.
*
* <p>Case 2.1: {@code null} the field {@link
* com.google.cloud.storage.Storage.BlobField#EVENT_BASED_HOLD} is selected in a {@link
* Storage#get(BlobId, Storage.BlobGetOption...)}, but event-based hold for the blob is not
* enabled. This case can be considered implicitly {@code false}.
*
* <p>Case 2.2: {@code null} the field {@link
* com.google.cloud.storage.Storage.BlobField#EVENT_BASED_HOLD} is not selected in a {@link
* Storage#get(BlobId, Storage.BlobGetOption...)}, and the state for this field is unknown.
*
* <p>Case 3: {@code false} event-based hold is explicitly set to false using in a {@link
* Builder#setEventBasedHold(Boolean)} client side for a follow-up request e.g. {@link
* Storage#update(BlobInfo, Storage.BlobTargetOption...)} in which case the value of event-based
* hold will remain {@code false} for the given instance.
*/
@BetaApi
public Boolean getEventBasedHold() {
return Data.<Boolean>isNull(eventBasedHold) ? null : eventBasedHold;
}
/**
* Returns a {@code Boolean} with either {@code true}, {@code null} and in certain cases {@code
* false}.
*
* <p>Case 1: {@code true} the field {@link
* com.google.cloud.storage.Storage.BlobField#TEMPORARY_HOLD} is selected in a {@link
* Storage#get(BlobId, Storage.BlobGetOption...)} and temporary hold for the blob is enabled.
*
* <p>Case 2.1: {@code null} the field {@link
* com.google.cloud.storage.Storage.BlobField#TEMPORARY_HOLD} is selected in a {@link
* Storage#get(BlobId, Storage.BlobGetOption...)}, but temporary hold for the blob is not enabled.
* This case can be considered implicitly {@code false}.
*
* <p>Case 2.2: {@code null} the field {@link
* com.google.cloud.storage.Storage.BlobField#TEMPORARY_HOLD} is not selected in a {@link
* Storage#get(BlobId, Storage.BlobGetOption...)}, and the state for this field is unknown.
*
* <p>Case 3: {@code false} event-based hold is explicitly set to false using in a {@link
* Builder#setEventBasedHold(Boolean)} client side for a follow-up request e.g. {@link
* Storage#update(BlobInfo, Storage.BlobTargetOption...)} in which case the value of temporary
* hold will remain {@code false} for the given instance.
*/
@BetaApi
public Boolean getTemporaryHold() {
return Data.<Boolean>isNull(temporaryHold) ? null : temporaryHold;
}
/**
* Returns the retention expiration time of the blob as {@code Long}, if a retention period is
* defined. If retention period is not defined this value returns {@code null}
*/
@BetaApi
public Long getRetentionExpirationTime() {
return Data.<Long>isNull(retentionExpirationTime) ? null : retentionExpirationTime;
}
/** Returns a builder for the current blob. */
public Builder toBuilder() {
return new BuilderImpl(this);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("bucket", getBucket())
.add("name", getName())
.add("generation", getGeneration())
.add("size", getSize())
.add("content-type", getContentType())
.add("metadata", getMetadata())
.toString();
}
@Override
public int hashCode() {
return Objects.hash(blobId);
}
@Override
public boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(BlobInfo.class)
&& Objects.equals(toPb(), ((BlobInfo) obj).toPb());
}
StorageObject toPb() {
StorageObject storageObject = blobId.toPb();
if (acl != null) {
storageObject.setAcl(
Lists.transform(
acl,
new Function<Acl, ObjectAccessControl>() {
@Override
public ObjectAccessControl apply(Acl acl) {
return acl.toObjectPb();
}
}));
}
if (deleteTime != null) {
storageObject.setTimeDeleted(new DateTime(deleteTime));
}
if (updateTime != null) {
storageObject.setUpdated(new DateTime(updateTime));
}
if (createTime != null) {
storageObject.setTimeCreated(new DateTime(createTime));
}
if (size != null) {
storageObject.setSize(BigInteger.valueOf(size));
}
if (owner != null) {
storageObject.setOwner(new Owner().setEntity(owner.toPb()));
}
if (storageClass != null) {
storageObject.setStorageClass(storageClass.toString());
}