/
BucketInfo.java
2061 lines (1808 loc) · 70.2 KB
/
BucketInfo.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 static com.google.common.collect.Lists.transform;
import com.google.api.client.json.jackson2.JacksonFactory;
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.Bucket;
import com.google.api.services.storage.model.Bucket.Encryption;
import com.google.api.services.storage.model.Bucket.Lifecycle;
import com.google.api.services.storage.model.Bucket.Lifecycle.Rule;
import com.google.api.services.storage.model.Bucket.Owner;
import com.google.api.services.storage.model.Bucket.Versioning;
import com.google.api.services.storage.model.Bucket.Website;
import com.google.api.services.storage.model.BucketAccessControl;
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.cloud.storage.Acl.Entity;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Logger;
/**
* Google Storage bucket metadata;
*
* @see <a href="https://cloud.google.com/storage/docs/concepts-techniques#concepts">Concepts and
* Terminology</a>
*/
public class BucketInfo implements Serializable {
static final Function<com.google.api.services.storage.model.Bucket, BucketInfo> FROM_PB_FUNCTION =
new Function<com.google.api.services.storage.model.Bucket, BucketInfo>() {
@Override
public BucketInfo apply(com.google.api.services.storage.model.Bucket pb) {
return BucketInfo.fromPb(pb);
}
};
static final Function<BucketInfo, com.google.api.services.storage.model.Bucket> TO_PB_FUNCTION =
new Function<BucketInfo, com.google.api.services.storage.model.Bucket>() {
@Override
public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo) {
return bucketInfo.toPb();
}
};
private static final long serialVersionUID = -4712013629621638459L;
private final String generatedId;
private final String name;
private final Acl.Entity owner;
private final String selfLink;
private final Boolean requesterPays;
private final Boolean versioningEnabled;
private final String indexPage;
private final String notFoundPage;
private final List<DeleteRule> deleteRules;
private final List<LifecycleRule> lifecycleRules;
private final String etag;
private final Long createTime;
private final Long updateTime;
private final Long metageneration;
private final List<Cors> cors;
private final List<Acl> acl;
private final List<Acl> defaultAcl;
private final String location;
private final StorageClass storageClass;
private final Map<String, String> labels;
private final String defaultKmsKeyName;
private final Boolean defaultEventBasedHold;
private final Long retentionEffectiveTime;
private final Boolean retentionPolicyIsLocked;
private final Long retentionPeriod;
private final IamConfiguration iamConfiguration;
private final String locationType;
private final Logging logging;
private static final Logger log = Logger.getLogger(BucketInfo.class.getName());
/**
* The Bucket's IAM Configuration.
*
* @see <a href="https://cloud.google.com/storage/docs/uniform-bucket-level-access">uniform
* bucket-level access</a>
*/
public static class IamConfiguration implements Serializable {
private static final long serialVersionUID = -8671736104909424616L;
private Boolean isUniformBucketLevelAccessEnabled;
private Long uniformBucketLevelAccessLockedTime;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) {
return false;
}
IamConfiguration other = (IamConfiguration) o;
return Objects.equals(toPb(), other.toPb());
}
@Override
public int hashCode() {
return Objects.hash(isUniformBucketLevelAccessEnabled, uniformBucketLevelAccessLockedTime);
}
private IamConfiguration(Builder builder) {
this.isUniformBucketLevelAccessEnabled = builder.isUniformBucketLevelAccessEnabled;
this.uniformBucketLevelAccessLockedTime = builder.uniformBucketLevelAccessLockedTime;
}
public static Builder newBuilder() {
return new Builder();
}
public Builder toBuilder() {
Builder builder = new Builder();
builder.isUniformBucketLevelAccessEnabled = isUniformBucketLevelAccessEnabled;
builder.uniformBucketLevelAccessLockedTime = uniformBucketLevelAccessLockedTime;
return builder;
}
/** Deprecated in favor of isUniformBucketLevelAccessEnabled(). */
@Deprecated
public Boolean isBucketPolicyOnlyEnabled() {
return isUniformBucketLevelAccessEnabled;
}
/** Deprecated in favor of uniformBucketLevelAccessLockedTime(). */
@Deprecated
public Long getBucketPolicyOnlyLockedTime() {
return uniformBucketLevelAccessLockedTime;
}
public Boolean isUniformBucketLevelAccessEnabled() {
return isUniformBucketLevelAccessEnabled;
}
public Long getUniformBucketLevelAccessLockedTime() {
return uniformBucketLevelAccessLockedTime;
}
Bucket.IamConfiguration toPb() {
Bucket.IamConfiguration iamConfiguration = new Bucket.IamConfiguration();
Bucket.IamConfiguration.UniformBucketLevelAccess uniformBucketLevelAccess =
new Bucket.IamConfiguration.UniformBucketLevelAccess();
uniformBucketLevelAccess.setEnabled(isUniformBucketLevelAccessEnabled);
uniformBucketLevelAccess.setLockedTime(
uniformBucketLevelAccessLockedTime == null
? null
: new DateTime(uniformBucketLevelAccessLockedTime));
iamConfiguration.setUniformBucketLevelAccess(uniformBucketLevelAccess);
return iamConfiguration;
}
static IamConfiguration fromPb(Bucket.IamConfiguration iamConfiguration) {
Bucket.IamConfiguration.UniformBucketLevelAccess uniformBucketLevelAccess =
iamConfiguration.getUniformBucketLevelAccess();
DateTime lockedTime = uniformBucketLevelAccess.getLockedTime();
return newBuilder()
.setIsUniformBucketLevelAccessEnabled(uniformBucketLevelAccess.getEnabled())
.setUniformBucketLevelAccessLockedTime(lockedTime == null ? null : lockedTime.getValue())
.build();
}
/** Builder for {@code IamConfiguration} */
public static class Builder {
private Boolean isUniformBucketLevelAccessEnabled;
private Long uniformBucketLevelAccessLockedTime;
/** Deprecated in favor of setIsUniformBucketLevelAccessEnabled(). */
@Deprecated
public Builder setIsBucketPolicyOnlyEnabled(Boolean isBucketPolicyOnlyEnabled) {
this.isUniformBucketLevelAccessEnabled = isBucketPolicyOnlyEnabled;
return this;
}
/** Deprecated in favor of setUniformBucketLevelAccessLockedTime(). */
@Deprecated
Builder setBucketPolicyOnlyLockedTime(Long bucketPolicyOnlyLockedTime) {
this.uniformBucketLevelAccessLockedTime = bucketPolicyOnlyLockedTime;
return this;
}
/**
* Sets whether uniform bucket-level access is enabled for this bucket. When this is enabled,
* access to the bucket will be configured through IAM, and legacy ACL policies will not work.
* When this is first enabled, {@code uniformBucketLevelAccess.lockedTime} will be set by the
* API automatically. This field can then be disabled until the time specified, after which it
* will become immutable and calls to change it will fail. If this is enabled, calls to access
* legacy ACL information will fail.
*/
public Builder setIsUniformBucketLevelAccessEnabled(
Boolean isUniformBucketLevelAccessEnabled) {
this.isUniformBucketLevelAccessEnabled = isUniformBucketLevelAccessEnabled;
return this;
}
/**
* Sets the deadline for switching {@code uniformBucketLevelAccess.enabled} back to false.
* After this time passes, calls to do so will fail. This is package-private, since in general
* this field should never be set by a user--it's automatically set by the backend when {@code
* enabled} is set to true.
*/
Builder setUniformBucketLevelAccessLockedTime(Long uniformBucketLevelAccessLockedTime) {
this.uniformBucketLevelAccessLockedTime = uniformBucketLevelAccessLockedTime;
return this;
}
/** Builds an {@code IamConfiguration} object */
public IamConfiguration build() {
return new IamConfiguration(this);
}
}
}
/**
* The bucket's logging configuration, which defines the destination bucket and optional name
* prefix for the current bucket's logs.
*/
public static class Logging implements Serializable {
private static final long serialVersionUID = -708892101216778492L;
private String logBucket;
private String logObjectPrefix;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) {
return false;
}
Logging other = (Logging) o;
return Objects.equals(toPb(), other.toPb());
}
@Override
public int hashCode() {
return Objects.hash(logBucket, logObjectPrefix);
}
public static Builder newBuilder() {
return new Builder();
}
public Builder toBuilder() {
Builder builder = new Builder();
builder.logBucket = logBucket;
builder.logObjectPrefix = logObjectPrefix;
return builder;
}
public String getLogBucket() {
return logBucket;
}
public String getLogObjectPrefix() {
return logObjectPrefix;
}
Bucket.Logging toPb() {
Bucket.Logging logging;
if (logBucket != null || logObjectPrefix != null) {
logging = new Bucket.Logging();
logging.setLogBucket(logBucket);
logging.setLogObjectPrefix(logObjectPrefix);
} else {
logging = Data.nullOf(Bucket.Logging.class);
}
return logging;
}
static Logging fromPb(Bucket.Logging logging) {
return newBuilder()
.setLogBucket(logging.getLogBucket())
.setLogObjectPrefix(logging.getLogObjectPrefix())
.build();
}
private Logging(Builder builder) {
this.logBucket = builder.logBucket;
this.logObjectPrefix = builder.logObjectPrefix;
}
public static class Builder {
private String logBucket;
private String logObjectPrefix;
/** The destination bucket where the current bucket's logs should be placed. */
public Builder setLogBucket(String logBucket) {
this.logBucket = logBucket;
return this;
}
/** A prefix for log object names. */
public Builder setLogObjectPrefix(String logObjectPrefix) {
this.logObjectPrefix = logObjectPrefix;
return this;
}
/** Builds an {@code Logging} object */
public Logging build() {
return new Logging(this);
}
}
}
/**
* Lifecycle rule for a bucket. Allows supported Actions, such as deleting and changing storage
* class, to be executed when certain Conditions are met.
*
* @see <a href="https://cloud.google.com/storage/docs/lifecycle#actions">Object Lifecycle
* Management</a>
*/
public static class LifecycleRule implements Serializable {
private static final long serialVersionUID = -5739807320148748613L;
private final LifecycleAction lifecycleAction;
private final LifecycleCondition lifecycleCondition;
public LifecycleRule(LifecycleAction action, LifecycleCondition condition) {
if (condition.getIsLive() == null
&& condition.getAge() == null
&& condition.getCreatedBefore() == null
&& condition.getMatchesStorageClass() == null
&& condition.getNumberOfNewerVersions() == null
&& condition.getDaysSinceNoncurrentTime() == null
&& condition.getNoncurrentTimeBefore() == null
&& condition.getCustomTimeBefore() == null
&& condition.getDaysSinceCustomTime() == null) {
log.warning(
"Creating a lifecycle condition with no supported conditions:\n"
+ this
+ "\nAttempting to update with this rule may cause errors. Please update "
+ " to the latest version of google-cloud-storage");
}
this.lifecycleAction = action;
this.lifecycleCondition = condition;
}
public LifecycleAction getAction() {
return lifecycleAction;
}
public LifecycleCondition getCondition() {
return lifecycleCondition;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("lifecycleAction", lifecycleAction)
.add("lifecycleCondition", lifecycleCondition)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(lifecycleAction, lifecycleCondition);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final LifecycleRule other = (LifecycleRule) obj;
return Objects.equals(toPb(), other.toPb());
}
Rule toPb() {
Rule rule = new Rule();
Rule.Action action = new Rule.Action().setType(lifecycleAction.getActionType());
if (lifecycleAction.getActionType().equals(SetStorageClassLifecycleAction.TYPE)) {
action.setStorageClass(
((SetStorageClassLifecycleAction) lifecycleAction).getStorageClass().toString());
}
rule.setAction(action);
Rule.Condition condition =
new Rule.Condition()
.setAge(lifecycleCondition.getAge())
.setCreatedBefore(
lifecycleCondition.getCreatedBefore() == null
? null
: new DateTime(true, lifecycleCondition.getCreatedBefore().getValue(), 0))
.setIsLive(lifecycleCondition.getIsLive())
.setNumNewerVersions(lifecycleCondition.getNumberOfNewerVersions())
.setMatchesStorageClass(
lifecycleCondition.getMatchesStorageClass() == null
? null
: transform(
lifecycleCondition.getMatchesStorageClass(),
Functions.toStringFunction()))
.setDaysSinceNoncurrentTime(lifecycleCondition.getDaysSinceNoncurrentTime())
.setNoncurrentTimeBefore(
lifecycleCondition.getNoncurrentTimeBefore() == null
? null
: new DateTime(
true, lifecycleCondition.getNoncurrentTimeBefore().getValue(), 0))
.setCustomTimeBefore(
lifecycleCondition.getCustomTimeBefore() == null
? null
: new DateTime(true, lifecycleCondition.getCustomTimeBefore().getValue(), 0))
.setDaysSinceCustomTime(lifecycleCondition.getDaysSinceCustomTime());
rule.setCondition(condition);
return rule;
}
static LifecycleRule fromPb(Rule rule) {
LifecycleAction lifecycleAction;
Rule.Action action = rule.getAction();
switch (action.getType()) {
case DeleteLifecycleAction.TYPE:
lifecycleAction = LifecycleAction.newDeleteAction();
break;
case SetStorageClassLifecycleAction.TYPE:
lifecycleAction =
LifecycleAction.newSetStorageClassAction(
StorageClass.valueOf(action.getStorageClass()));
break;
default:
throw new UnsupportedOperationException(
"The specified lifecycle action " + action.getType() + " is not currently supported");
}
Rule.Condition condition = rule.getCondition();
LifecycleCondition.Builder conditionBuilder =
LifecycleCondition.newBuilder()
.setAge(condition.getAge())
.setCreatedBefore(condition.getCreatedBefore())
.setIsLive(condition.getIsLive())
.setNumberOfNewerVersions(condition.getNumNewerVersions())
.setMatchesStorageClass(
condition.getMatchesStorageClass() == null
? null
: transform(
condition.getMatchesStorageClass(),
new Function<String, StorageClass>() {
public StorageClass apply(String storageClass) {
return StorageClass.valueOf(storageClass);
}
}))
.setDaysSinceNoncurrentTime(condition.getDaysSinceNoncurrentTime())
.setNoncurrentTimeBefore(condition.getNoncurrentTimeBefore())
.setCustomTimeBefore(condition.getCustomTimeBefore())
.setDaysSinceCustomTime(condition.getDaysSinceCustomTime());
return new LifecycleRule(lifecycleAction, conditionBuilder.build());
}
/**
* Condition for a Lifecycle rule, specifies under what criteria an Action should be executed.
*
* @see <a href="https://cloud.google.com/storage/docs/lifecycle#conditions">Object Lifecycle
* Management</a>
*/
public static class LifecycleCondition implements Serializable {
private static final long serialVersionUID = -6482314338394768785L;
private final Integer age;
private final DateTime createdBefore;
private final Integer numberOfNewerVersions;
private final Boolean isLive;
private final List<StorageClass> matchesStorageClass;
private final Integer daysSinceNoncurrentTime;
private final DateTime noncurrentTimeBefore;
private final DateTime customTimeBefore;
private final Integer daysSinceCustomTime;
private LifecycleCondition(Builder builder) {
this.age = builder.age;
this.createdBefore = builder.createdBefore;
this.numberOfNewerVersions = builder.numberOfNewerVersions;
this.isLive = builder.isLive;
this.matchesStorageClass = builder.matchesStorageClass;
this.daysSinceNoncurrentTime = builder.daysSinceNoncurrentTime;
this.noncurrentTimeBefore = builder.noncurrentTimeBefore;
this.customTimeBefore = builder.customTimeBefore;
this.daysSinceCustomTime = builder.daysSinceCustomTime;
}
public Builder toBuilder() {
return newBuilder()
.setAge(this.age)
.setCreatedBefore(this.createdBefore)
.setNumberOfNewerVersions(this.numberOfNewerVersions)
.setIsLive(this.isLive)
.setMatchesStorageClass(this.matchesStorageClass)
.setDaysSinceNoncurrentTime(this.daysSinceNoncurrentTime)
.setNoncurrentTimeBefore(this.noncurrentTimeBefore)
.setCustomTimeBefore(this.customTimeBefore)
.setDaysSinceCustomTime(this.daysSinceCustomTime);
}
public static Builder newBuilder() {
return new Builder();
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("age", age)
.add("createBefore", createdBefore)
.add("numberofNewerVersions", numberOfNewerVersions)
.add("isLive", isLive)
.add("matchesStorageClass", matchesStorageClass)
.add("daysSinceNoncurrentTime", daysSinceNoncurrentTime)
.add("noncurrentTimeBefore", noncurrentTimeBefore)
.add("customTimeBefore", customTimeBefore)
.add("daysSinceCustomTime", daysSinceCustomTime)
.toString();
}
public Integer getAge() {
return age;
}
public DateTime getCreatedBefore() {
return createdBefore;
}
public Integer getNumberOfNewerVersions() {
return numberOfNewerVersions;
}
public Boolean getIsLive() {
return isLive;
}
public List<StorageClass> getMatchesStorageClass() {
return matchesStorageClass;
}
/** Returns the number of days elapsed since the noncurrent timestamp of an object. */
public Integer getDaysSinceNoncurrentTime() {
return daysSinceNoncurrentTime;
}
/**
* Returns the date in RFC 3339 format with only the date part (for instance, "2013-01-15").
*/
public DateTime getNoncurrentTimeBefore() {
return noncurrentTimeBefore;
}
/* Returns the date in RFC 3339 format with only the date part (for instance, "2013-01-15").*/
public DateTime getCustomTimeBefore() {
return customTimeBefore;
}
/** Returns the number of days elapsed since the user-specified timestamp set on an object. */
public Integer getDaysSinceCustomTime() {
return daysSinceCustomTime;
}
/** Builder for {@code LifecycleCondition}. */
public static class Builder {
private Integer age;
private DateTime createdBefore;
private Integer numberOfNewerVersions;
private Boolean isLive;
private List<StorageClass> matchesStorageClass;
private Integer daysSinceNoncurrentTime;
private DateTime noncurrentTimeBefore;
private DateTime customTimeBefore;
private Integer daysSinceCustomTime;
private Builder() {}
/**
* Sets the age in days. This condition is satisfied when a Blob reaches the specified age
* (in days). When you specify the Age condition, you are specifying a Time to Live (TTL)
* for objects in a bucket with lifecycle management configured. The time when the Age
* condition is considered to be satisfied is calculated by adding the specified value to
* the object creation time.
*/
public Builder setAge(Integer age) {
this.age = age;
return this;
}
/**
* Sets the date a Blob should be created before for an Action to be executed. Note that
* only the date will be considered, if the time is specified it will be truncated. This
* condition is satisfied when an object is created before midnight of the specified date in
* UTC. *
*/
public Builder setCreatedBefore(DateTime createdBefore) {
this.createdBefore = createdBefore;
return this;
}
/**
* Sets the number of newer versions a Blob should have for an Action to be executed.
* Relevant only when versioning is enabled on a bucket. *
*/
public Builder setNumberOfNewerVersions(Integer numberOfNewerVersions) {
this.numberOfNewerVersions = numberOfNewerVersions;
return this;
}
/**
* Sets an isLive Boolean condition. If the value is true, this lifecycle condition matches
* only live Blobs; if the value is false, it matches only archived objects. For the
* purposes of this condition, Blobs in non-versioned buckets are considered live.
*/
public Builder setIsLive(Boolean live) {
this.isLive = live;
return this;
}
/**
* Sets a list of Storage Classes for a objects that satisfy the condition to execute the
* Action. *
*/
public Builder setMatchesStorageClass(List<StorageClass> matchesStorageClass) {
this.matchesStorageClass = matchesStorageClass;
return this;
}
/**
* Sets the number of days elapsed since the noncurrent timestamp of an object. The
* condition is satisfied if the days elapsed is at least this number. This condition is
* relevant only for versioned objects. The value of the field must be a nonnegative
* integer. If it's zero, the object version will become eligible for Lifecycle action as
* soon as it becomes noncurrent.
*/
public Builder setDaysSinceNoncurrentTime(Integer daysSinceNoncurrentTime) {
this.daysSinceNoncurrentTime = daysSinceNoncurrentTime;
return this;
}
/**
* Sets the date in RFC 3339 format with only the date part (for instance, "2013-01-15").
* Note that only date part will be considered, if the time is specified it will be
* truncated. This condition is satisfied when the noncurrent time on an object is before
* this date. This condition is relevant only for versioned objects.
*/
public Builder setNoncurrentTimeBefore(DateTime noncurrentTimeBefore) {
this.noncurrentTimeBefore = noncurrentTimeBefore;
return this;
}
/**
* Sets the date in RFC 3339 format with only the date part (for instance, "2013-01-15").
* Note that only date part will be considered, if the time is specified it will be
* truncated. This condition is satisfied when the custom time on an object is before this
* date in UTC.
*/
public Builder setCustomTimeBefore(DateTime customTimeBefore) {
this.customTimeBefore = customTimeBefore;
return this;
}
/**
* Sets the number of days elapsed since the user-specified timestamp set on an object. The
* condition is satisfied if the days elapsed is at least this number. If no custom
* timestamp is specified on an object, the condition does not apply.
*/
public Builder setDaysSinceCustomTime(Integer daysSinceCustomTime) {
this.daysSinceCustomTime = daysSinceCustomTime;
return this;
}
/** Builds a {@code LifecycleCondition} object. * */
public LifecycleCondition build() {
return new LifecycleCondition(this);
}
}
}
/**
* Base class for the Action to take when a Lifecycle Condition is met. Specific Actions are
* expressed as subclasses of this class, accessed by static factory methods.
*/
public abstract static class LifecycleAction implements Serializable {
private static final long serialVersionUID = 5801228724709173284L;
public abstract String getActionType();
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("actionType", getActionType()).toString();
}
/**
* Creates a new {@code DeleteLifecycleAction}. Blobs that meet the Condition associated with
* this action will be deleted.
*/
public static DeleteLifecycleAction newDeleteAction() {
return new DeleteLifecycleAction();
}
/**
* Creates a new {@code SetStorageClassLifecycleAction}. A Blob's storage class that meets the
* action's conditions will be changed to the specified storage class.
*
* @param storageClass The new storage class to use when conditions are met for this action.
*/
public static SetStorageClassLifecycleAction newSetStorageClassAction(
StorageClass storageClass) {
return new SetStorageClassLifecycleAction(storageClass);
}
}
public static class DeleteLifecycleAction extends LifecycleAction {
public static final String TYPE = "Delete";
private static final long serialVersionUID = -2050986302222644873L;
private DeleteLifecycleAction() {}
@Override
public String getActionType() {
return TYPE;
}
}
public static class SetStorageClassLifecycleAction extends LifecycleAction {
public static final String TYPE = "SetStorageClass";
private static final long serialVersionUID = -62615467186000899L;
private final StorageClass storageClass;
private SetStorageClassLifecycleAction(StorageClass storageClass) {
this.storageClass = storageClass;
}
@Override
public String getActionType() {
return TYPE;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("actionType", getActionType())
.add("storageClass", storageClass.name())
.toString();
}
public StorageClass getStorageClass() {
return storageClass;
}
}
}
/**
* Base class for bucket's delete rules. Allows to configure automatic deletion of blobs and blobs
* versions.
*
* @see <a href="https://cloud.google.com/storage/docs/lifecycle">Object Lifecycle Management</a>
* @deprecated Use a {@code LifecycleRule} with a {@code DeleteLifecycleAction} and a {@code
* LifecycleCondition} which is equivalent to a subclass of DeleteRule instead.
*/
@Deprecated
public abstract static class DeleteRule implements Serializable {
private static final long serialVersionUID = 3137971668395933033L;
private static final String SUPPORTED_ACTION = "Delete";
private final Type type;
public enum Type {
AGE,
CREATE_BEFORE,
NUM_NEWER_VERSIONS,
IS_LIVE,
UNKNOWN
}
DeleteRule(Type type) {
this.type = type;
}
public Type getType() {
return type;
}
@Override
public int hashCode() {
return Objects.hash(type);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final DeleteRule other = (DeleteRule) obj;
return Objects.equals(toPb(), other.toPb());
}
Rule toPb() {
Rule rule = new Rule();
rule.setAction(new Rule.Action().setType(SUPPORTED_ACTION));
Rule.Condition condition = new Rule.Condition();
populateCondition(condition);
rule.setCondition(condition);
return rule;
}
abstract void populateCondition(Rule.Condition condition);
static DeleteRule fromPb(Rule rule) {
if (rule.getAction() != null && SUPPORTED_ACTION.endsWith(rule.getAction().getType())) {
Rule.Condition condition = rule.getCondition();
Integer age = condition.getAge();
if (age != null) {
return new AgeDeleteRule(age);
}
DateTime dateTime = condition.getCreatedBefore();
if (dateTime != null) {
return new CreatedBeforeDeleteRule(dateTime.getValue());
}
Integer numNewerVersions = condition.getNumNewerVersions();
if (numNewerVersions != null) {
return new NumNewerVersionsDeleteRule(numNewerVersions);
}
Boolean isLive = condition.getIsLive();
if (isLive != null) {
return new IsLiveDeleteRule(isLive);
}
}
return new RawDeleteRule(rule);
}
}
/**
* Delete rule class that sets a Time To Live for blobs in the bucket.
*
* @see <a href="https://cloud.google.com/storage/docs/lifecycle">Object Lifecycle Management</a>
* @deprecated Use a {@code LifecycleRule} with a {@code DeleteLifecycleAction} and use {@code
* LifecycleCondition.Builder.setAge} instead.
* <p>For example, {@code new DeleteLifecycleAction(1)} is equivalent to {@code new
* LifecycleRule( LifecycleAction.newDeleteAction(),
* LifecycleCondition.newBuilder().setAge(1).build()))}
*/
@Deprecated
public static class AgeDeleteRule extends DeleteRule {
private static final long serialVersionUID = 5697166940712116380L;
private final int daysToLive;
/**
* Creates an {@code AgeDeleteRule} object.
*
* @param daysToLive blobs' Time To Live expressed in days. The time when the age condition is
* considered to be satisfied is computed by adding {@code daysToLive} days to the midnight
* following blob's creation time in UTC.
*/
public AgeDeleteRule(int daysToLive) {
super(Type.AGE);
this.daysToLive = daysToLive;
}
public int getDaysToLive() {
return daysToLive;
}
@Override
void populateCondition(Rule.Condition condition) {
condition.setAge(daysToLive);
}
}
static class RawDeleteRule extends DeleteRule {
private static final long serialVersionUID = -7166938278642301933L;
private transient Rule rule;
RawDeleteRule(Rule rule) {
super(Type.UNKNOWN);
this.rule = rule;
}
@Override
void populateCondition(Rule.Condition condition) {
throw new UnsupportedOperationException();
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeUTF(rule.toString());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
rule = new JacksonFactory().fromString(in.readUTF(), Rule.class);
}
@Override
Rule toPb() {
return rule;
}
}
/**
* Delete rule class for blobs in the bucket that have been created before a certain date.
*
* @see <a href="https://cloud.google.com/storage/docs/lifecycle">Object Lifecycle Management</a>
* @deprecated Use a {@code LifecycleRule} with an action {@code DeleteLifecycleAction} and a
* condition {@code LifecycleCondition.Builder.setCreatedBefore} instead.
*/
@Deprecated
public static class CreatedBeforeDeleteRule extends DeleteRule {
private static final long serialVersionUID = 881692650279195867L;
private final long timeMillis;
/**
* Creates an {@code CreatedBeforeDeleteRule} object.
*
* @param timeMillis a date in UTC. Blobs that have been created before midnight of the provided
* date meet the delete condition
*/
public CreatedBeforeDeleteRule(long timeMillis) {
super(Type.CREATE_BEFORE);
this.timeMillis = timeMillis;
}
public long getTimeMillis() {
return timeMillis;
}
@Override
void populateCondition(Rule.Condition condition) {
condition.setCreatedBefore(new DateTime(true, timeMillis, 0));
}
}
/**
* Delete rule class for versioned blobs. Specifies when to delete a blob's version according to
* the number of available newer versions for that blob.
*
* @see <a href="https://cloud.google.com/storage/docs/lifecycle">Object Lifecycle Management</a>
* @deprecated Use a {@code LifecycleRule} with a {@code DeleteLifecycleAction} and a condition
* {@code LifecycleCondition.Builder.setNumberOfNewerVersions} instead.
*/
@Deprecated
public static class NumNewerVersionsDeleteRule extends DeleteRule {
private static final long serialVersionUID = -1955554976528303894L;
private final int numNewerVersions;
/**
* Creates an {@code NumNewerVersionsDeleteRule} object.
*
* @param numNewerVersions the number of newer versions. A blob's version meets the delete
* condition when {@code numNewerVersions} newer versions are available.
*/
public NumNewerVersionsDeleteRule(int numNewerVersions) {
super(Type.NUM_NEWER_VERSIONS);
this.numNewerVersions = numNewerVersions;
}