Search in sources :

Example 1 with LifecycleRule

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);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule)

Example 2 with LifecycleRule

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);
    }
}
Also used : Bucket(com.google.cloud.storage.Bucket) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule) DateTime(com.google.api.client.util.DateTime) Test(org.junit.Test)

Example 3 with LifecycleRule

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));
}
Also used : LifecycleCondition(com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleCondition) SetStorageClassLifecycleAction(com.google.cloud.storage.BucketInfo.LifecycleRule.SetStorageClassLifecycleAction) GcpGcsBucketDefaultStorageClass(bio.terra.workspace.model.GcpGcsBucketDefaultStorageClass) StorageClass(com.google.cloud.storage.StorageClass) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule)

Example 4 with LifecycleRule

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());
}
Also used : ApiGcpGcsBucketLifecycleRule(bio.terra.workspace.generated.model.ApiGcpGcsBucketLifecycleRule) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule) ApiGcpGcsBucketLifecycleRule(bio.terra.workspace.generated.model.ApiGcpGcsBucketLifecycleRule) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Example 5 with LifecycleRule

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));
}
Also used : GcsApiConversions.toBucketInfo(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.GcsApiConversions.toBucketInfo) BucketInfo(com.google.cloud.storage.BucketInfo) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule) ApiGcpGcsBucketLifecycleRule(bio.terra.workspace.generated.model.ApiGcpGcsBucketLifecycleRule) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

LifecycleRule (com.google.cloud.storage.BucketInfo.LifecycleRule)13 BaseUnitTest (bio.terra.workspace.common.BaseUnitTest)5 Test (org.junit.jupiter.api.Test)5 StepResult (bio.terra.stairway.StepResult)4 SetStorageClassLifecycleAction (com.google.cloud.storage.BucketInfo.LifecycleRule.SetStorageClassLifecycleAction)4 StorageClass (com.google.cloud.storage.StorageClass)4 LifecycleCondition (com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleCondition)3 Test (org.junit.Test)3 ApiGcpGcsBucketLifecycleRule (bio.terra.workspace.generated.model.ApiGcpGcsBucketLifecycleRule)2 DateTime (com.google.api.client.util.DateTime)2 Rule (com.google.api.services.storage.model.Bucket.Lifecycle.Rule)2 Bucket (com.google.cloud.storage.Bucket)2 BucketInfo (com.google.cloud.storage.BucketInfo)2 AgeDeleteRule (com.google.cloud.storage.BucketInfo.AgeDeleteRule)2 CreatedBeforeDeleteRule (com.google.cloud.storage.BucketInfo.CreatedBeforeDeleteRule)2 DeleteRule (com.google.cloud.storage.BucketInfo.DeleteRule)2 IsLiveDeleteRule (com.google.cloud.storage.BucketInfo.IsLiveDeleteRule)2 DeleteLifecycleAction (com.google.cloud.storage.BucketInfo.LifecycleRule.DeleteLifecycleAction)2 LifecycleAction (com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleAction)2 NumNewerVersionsDeleteRule (com.google.cloud.storage.BucketInfo.NumNewerVersionsDeleteRule)2