use of com.google.cloud.storage.BucketInfo.LifecycleRule in project google-cloud-java by GoogleCloudPlatform.
the class EnableLifecycleManagement method enableLifecycleManagement.
public static void enableLifecycleManagement(String projectId, String bucketName) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);
// See the LifecycleRule documentation for additional info on what you can do with lifecycle
// management rules. This one deletes objects that are over 100 days old.
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/BucketInfo.LifecycleRule.html
bucket.toBuilder().setLifecycleRules(ImmutableList.of(new LifecycleRule(LifecycleRule.LifecycleAction.newDeleteAction(), LifecycleRule.LifecycleCondition.newBuilder().setAge(100).build()))).build().update();
System.out.println("Lifecycle management was enabled and configured for bucket " + bucketName);
}
use of com.google.cloud.storage.BucketInfo.LifecycleRule in project java-storage by googleapis.
the class ITStorageTest method testGetBucketLifecycleRules.
@Test
public void testGetBucketLifecycleRules() {
String lifecycleTestBucketName = RemoteStorageHelper.generateBucketName();
storage.create(BucketInfo.newBuilder(lifecycleTestBucketName).setLocation("us").setLifecycleRules(ImmutableList.of(new LifecycleRule(LifecycleAction.newSetStorageClassAction(StorageClass.COLDLINE), LifecycleCondition.newBuilder().setAge(1).setNumberOfNewerVersions(3).setIsLive(false).setCreatedBefore(new DateTime(System.currentTimeMillis())).setMatchesStorageClass(ImmutableList.of(StorageClass.COLDLINE)).setDaysSinceNoncurrentTime(30).setNoncurrentTimeBefore(new DateTime(System.currentTimeMillis())).setCustomTimeBefore(new DateTime(System.currentTimeMillis())).setDaysSinceCustomTime(30).build()))).build());
Bucket remoteBucket = storage.get(lifecycleTestBucketName, Storage.BucketGetOption.fields(BucketField.LIFECYCLE));
LifecycleRule lifecycleRule = remoteBucket.getLifecycleRules().get(0);
try {
assertTrue(lifecycleRule.getAction().getActionType().equals(LifecycleRule.SetStorageClassLifecycleAction.TYPE));
assertEquals(3, lifecycleRule.getCondition().getNumberOfNewerVersions().intValue());
assertNotNull(lifecycleRule.getCondition().getCreatedBefore());
assertFalse(lifecycleRule.getCondition().getIsLive());
assertEquals(1, lifecycleRule.getCondition().getAge().intValue());
assertEquals(1, lifecycleRule.getCondition().getMatchesStorageClass().size());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceNoncurrentTime().intValue());
assertNotNull(lifecycleRule.getCondition().getNoncurrentTimeBefore());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceCustomTime().intValue());
assertNotNull(lifecycleRule.getCondition().getCustomTimeBefore());
} finally {
storage.delete(lifecycleTestBucketName);
}
}
use of com.google.cloud.storage.BucketInfo.LifecycleRule in project terra-workspace-manager by DataBiosphere.
the class ControlledGcsBucketLifecycle method verifyUpdatedLifecycleRules.
private void verifyUpdatedLifecycleRules(List<? extends LifecycleRule> lifecycleRules) {
final LifecycleRule rule = lifecycleRules.get(0);
assertEquals(SetStorageClassLifecycleAction.TYPE, rule.getAction().getActionType());
final SetStorageClassLifecycleAction setStorageClassLifecycleAction = (SetStorageClassLifecycleAction) rule.getAction();
assertEquals(StorageClass.ARCHIVE, setStorageClassLifecycleAction.getStorageClass());
final LifecycleCondition condition = rule.getCondition();
assertEquals(30, condition.getAge());
// The datetime gets simplified to midnight UTC somewhere along the line
assertEquals(DateTime.parseRfc3339("1981-04-21"), condition.getCreatedBefore());
assertTrue(condition.getIsLive());
assertEquals(3, condition.getNumberOfNewerVersions());
final List<StorageClass> matchesStorageClass = condition.getMatchesStorageClass();
assertThat(matchesStorageClass, hasSize(1));
assertEquals(StorageClass.ARCHIVE, matchesStorageClass.get(0));
}
use of com.google.cloud.storage.BucketInfo.LifecycleRule in project terra-workspace-manager by DataBiosphere.
the class GcsApiConversionsTest method testLifecycleRuleConversions.
@Test
public void testLifecycleRuleConversions() {
final ApiGcpGcsBucketLifecycleRule wsmRule1 = toWsmApi(GCS_LIFECYCLE_RULE_1);
assertEquals(ApiGcpGcsBucketLifecycleRuleActionType.DELETE, wsmRule1.getAction().getType());
assertNull(wsmRule1.getAction().getStorageClass());
assertEquals(42, wsmRule1.getCondition().getAge());
assertFalse(wsmRule1.getCondition().isLive());
assertEquals(2, wsmRule1.getCondition().getNumNewerVersions());
// not empty list
assertNull(wsmRule1.getCondition().getMatchesStorageClass());
final LifecycleRule gcsRule1 = toGcsApi(wsmRule1);
// Direct equality comparison with LifecycleAction and LifecycleCondition
// types doesn't work for some reason. Hence, comparing fields.
assertEquals(GCS_LIFECYCLE_RULE_1.getAction().getActionType(), gcsRule1.getAction().getActionType());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getAge(), gcsRule1.getCondition().getAge());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getCreatedBefore(), gcsRule1.getCondition().getCreatedBefore());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getMatchesStorageClass(), gcsRule1.getCondition().getMatchesStorageClass());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getNumberOfNewerVersions(), gcsRule1.getCondition().getNumberOfNewerVersions());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getIsLive(), gcsRule1.getCondition().getIsLive());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getDaysSinceNoncurrentTime(), gcsRule1.getCondition().getDaysSinceNoncurrentTime());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getNoncurrentTimeBefore(), gcsRule1.getCondition().getNoncurrentTimeBefore());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getCustomTimeBefore(), gcsRule1.getCondition().getCustomTimeBefore());
assertEquals(GCS_LIFECYCLE_RULE_1.getCondition().getDaysSinceCustomTime(), gcsRule1.getCondition().getDaysSinceCustomTime());
}
use of com.google.cloud.storage.BucketInfo.LifecycleRule in project terra-workspace-manager by DataBiosphere.
the class GcsApiConversionsTest method testToBucketInfo.
@Test
public void testToBucketInfo() {
final String bucketName = uniqueBucketName();
final BucketInfo bucketInfo1 = toBucketInfo(bucketName, ControlledResourceFixtures.BUCKET_UPDATE_PARAMETERS_1);
assertEquals(bucketName, bucketInfo1.getName());
assertEquals(StorageClass.STANDARD, bucketInfo1.getStorageClass());
assertThat(bucketInfo1.getLifecycleRules(), hasSize(2));
final LifecycleRule gcsRule1 = bucketInfo1.getLifecycleRules().get(0);
assertEquals(DeleteLifecycleAction.TYPE, gcsRule1.getAction().getActionType());
assertEquals(31, gcsRule1.getCondition().getAge());
assertEquals(toGoogleDateTime(OFFSET_DATE_TIME_2), gcsRule1.getCondition().getCreatedBefore());
assertTrue(gcsRule1.getCondition().getIsLive());
assertThat(gcsRule1.getCondition().getMatchesStorageClass(), hasSize(2));
}
Aggregations