diff --git a/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java b/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java index e1db27ab02..5cdf01b1b6 100644 --- a/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java +++ b/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java @@ -35,8 +35,7 @@ public static void main(String[] args) { static void deleteBlob(String bucketName, String blobName) { // Customize retry behavior RetrySettings retrySettings = - StorageOptions.getDefaultRetrySettings() - .toBuilder() + StorageOptions.getDefaultRetrySettings().toBuilder() // Set the max number of attempts to 10 (initial attempt plus 9 retries) .setMaxAttempts(10) // Set the backoff multiplier to 3.0 diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/DisableRequesterPays.java b/samples/snippets/src/main/java/com/example/storage/bucket/DisableRequesterPays.java index 9cb40b1b65..aaa9694c9a 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/DisableRequesterPays.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/DisableRequesterPays.java @@ -31,8 +31,7 @@ public static void disableRequesterPays(String projectId, String bucketName) { Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); Bucket bucket = storage.get(bucketName, Storage.BucketGetOption.userProject(projectId)); - bucket - .toBuilder() + bucket.toBuilder() .setRequesterPays(false) .build() .update(Storage.BucketTargetOption.userProject(projectId)); diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/DisableSoftDelete.java b/samples/snippets/src/main/java/com/example/storage/bucket/DisableSoftDelete.java new file mode 100644 index 0000000000..97d6571469 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/DisableSoftDelete.java @@ -0,0 +1,48 @@ +/* + * Copyright 2024 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.example.storage.bucket; + +// [START storage_disable_soft_delete] +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.time.Duration; + +public class DisableSoftDelete { + public static void disableSoftDelete(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); + bucket.toBuilder() + .setSoftDeletePolicy( + // Setting the retention duration to 0 disables Soft Delete. + BucketInfo.SoftDeletePolicy.newBuilder() + .setRetentionDuration(Duration.ofSeconds(0)) + .build()) + .build() + .update(); + + System.out.println("Soft delete for " + bucketName + " was disabled"); + } +} +// [END storage_disable_soft_delete] diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/EnableLifecycleManagement.java b/samples/snippets/src/main/java/com/example/storage/bucket/EnableLifecycleManagement.java index 5c8699f5ee..bf2767321e 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/EnableLifecycleManagement.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/EnableLifecycleManagement.java @@ -40,8 +40,7 @@ public static void enableLifecycleManagement(String projectId, String 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() + bucket.toBuilder() .setLifecycleRules( ImmutableList.of( new LifecycleRule( diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/EnableUniformBucketLevelAccess.java b/samples/snippets/src/main/java/com/example/storage/bucket/EnableUniformBucketLevelAccess.java index 4c70b7b2c7..a8ae606bd0 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/EnableUniformBucketLevelAccess.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/EnableUniformBucketLevelAccess.java @@ -43,8 +43,7 @@ public static void enableUniformBucketLevelAccess(String projectId, String bucke BucketInfo.IamConfiguration.newBuilder().setIsUniformBucketLevelAccessEnabled(true).build(); storage.update( - bucket - .toBuilder() + bucket.toBuilder() .setIamConfiguration(iamConfiguration) .setAcl(null) .setDefaultAcl(null) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java b/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java new file mode 100644 index 0000000000..32f4277a05 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java @@ -0,0 +1,44 @@ +/* + * Copyright 2024 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.example.storage.bucket; + +// [START storage_get_soft_delete_policy] +import com.google.cloud.storage.BucketInfo.SoftDeletePolicy; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.time.Duration; + +public class GetSoftDeletePolicy { + public static void getSoftDeletePolicy(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(); + SoftDeletePolicy policy = storage.get(bucketName).getSoftDeletePolicy(); + + if (Duration.ofSeconds(0).equals(policy.getRetentionDuration())) { + System.out.println("Soft delete is disabled for " + bucketName); + } else { + System.out.println("The soft delete policy for " + bucketName + " is:"); + System.out.println(policy); + } + } +} +// [END storage_get_soft_delete_policy] diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/MakeBucketPublic.java b/samples/snippets/src/main/java/com/example/storage/bucket/MakeBucketPublic.java index a77a1d99cf..09e9b32074 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/MakeBucketPublic.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/MakeBucketPublic.java @@ -35,8 +35,7 @@ public static void makeBucketPublic(String projectId, String bucketName) { Policy originalPolicy = storage.getIamPolicy(bucketName); storage.setIamPolicy( bucketName, - originalPolicy - .toBuilder() + originalPolicy.toBuilder() .addIdentity(StorageRoles.objectViewer(), Identity.allUsers()) // All users can view .build()); diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/SetBucketAutoclass.java b/samples/snippets/src/main/java/com/example/storage/bucket/SetBucketAutoclass.java index 0f9ae0f4ef..395acc023f 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/SetBucketAutoclass.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/SetBucketAutoclass.java @@ -49,8 +49,7 @@ public static void setBucketAutoclass( Bucket bucket = storage.get(bucketName); Bucket toUpdate = - bucket - .toBuilder() + bucket.toBuilder() .setAutoclass( Autoclass.newBuilder() .setEnabled(enabled) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/SetPublicAccessPreventionEnforced.java b/samples/snippets/src/main/java/com/example/storage/bucket/SetPublicAccessPreventionEnforced.java index 8d679838a5..c959dce210 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/SetPublicAccessPreventionEnforced.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/SetPublicAccessPreventionEnforced.java @@ -34,8 +34,7 @@ public static void setPublicAccessPreventionEnforced(String projectId, String bu Bucket bucket = storage.get(bucketName); // Enforces public access prevention for the bucket - bucket - .toBuilder() + bucket.toBuilder() .setIamConfiguration( BucketInfo.IamConfiguration.newBuilder() .setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/SetPublicAccessPreventionInherited.java b/samples/snippets/src/main/java/com/example/storage/bucket/SetPublicAccessPreventionInherited.java index 57938b8c48..0208f70824 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/SetPublicAccessPreventionInherited.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/SetPublicAccessPreventionInherited.java @@ -34,8 +34,7 @@ public static void setPublicAccessPreventionInherited(String projectId, String b Bucket bucket = storage.get(bucketName); // Sets public access prevention to 'inherited' for the bucket - bucket - .toBuilder() + bucket.toBuilder() .setIamConfiguration( BucketInfo.IamConfiguration.newBuilder() .setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED) diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/SetRetentionPolicy.java b/samples/snippets/src/main/java/com/example/storage/bucket/SetRetentionPolicy.java index eca89fbb44..4491ed3897 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/SetRetentionPolicy.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/SetRetentionPolicy.java @@ -43,8 +43,7 @@ public static void setRetentionPolicy( Bucket bucket = storage.get(bucketName); Bucket bucketWithRetentionPolicy = storage.update( - bucket - .toBuilder() + bucket.toBuilder() .setRetentionPeriodDuration(Duration.ofSeconds(retentionPeriodSeconds)) .build(), BucketTargetOption.metagenerationMatch()); diff --git a/samples/snippets/src/main/java/com/example/storage/bucket/SetSoftDeletePolicy.java b/samples/snippets/src/main/java/com/example/storage/bucket/SetSoftDeletePolicy.java new file mode 100644 index 0000000000..e923cb2faa --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/SetSoftDeletePolicy.java @@ -0,0 +1,48 @@ +/* + * Copyright 2024 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.example.storage.bucket; + +// [START storage_set_soft_delete_policy] +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.time.Duration; + +public class SetSoftDeletePolicy { + public static void setSoftDeletePolicy(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); + bucket.toBuilder() + .setSoftDeletePolicy( + BucketInfo.SoftDeletePolicy.newBuilder() + .setRetentionDuration(Duration.ofDays(10)) + .build()) + .build() + .update(); + + System.out.println( + "Soft delete policy for " + bucketName + " was set to a 10-day retention period"); + } +} +// [END storage_set_soft_delete_policy] diff --git a/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedObjects.java b/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedObjects.java new file mode 100644 index 0000000000..eb3b5d158b --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedObjects.java @@ -0,0 +1,41 @@ +/* + * Copyright 2024 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.example.storage.object; + +// [START storage_list_soft_deleted_objects] +import com.google.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +public class ListSoftDeletedObjects { + public static void listSoftDeletedObjects(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(); + Page blobs = storage.list(bucketName, Storage.BlobListOption.softDeleted(true)); + + for (Blob blob : blobs.iterateAll()) { + System.out.println(blob.getName()); + } + } +} +// [END storage_list_soft_deleted_objects] diff --git a/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedVersionsOfObject.java b/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedVersionsOfObject.java new file mode 100644 index 0000000000..87532cdc8e --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedVersionsOfObject.java @@ -0,0 +1,51 @@ +/* + * Copyright 2024 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.example.storage.object; + +// [START storage_list_soft_deleted_object_versions] +import com.google.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +public class ListSoftDeletedVersionsOfObject { + + public static void listSoftDeletedVersionOfObject( + String projectId, String bucketName, String objectName) { + // The ID of your GCP project + // String projectId = "your-project-id"; + + // The ID of your GCS bucket + // String bucketName = "your-unique-bucket-name"; + + // The name of your GCS object + // String objectName = "your-object-name"; + + Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); + Page blobs = + storage.list( + bucketName, + Storage.BlobListOption.softDeleted(true), + // See https://cloud.google.com/storage/docs/json_api/v1/objects/list#matchGlob + Storage.BlobListOption.matchGlob(objectName)); + + for (Blob blob : blobs.iterateAll()) { + System.out.println(blob.getName()); + } + } +} +// [END storage_list_soft_deleted_object_versions] diff --git a/samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java b/samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java new file mode 100644 index 0000000000..86f09a39ec --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java @@ -0,0 +1,43 @@ +/* + * Copyright 2024 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.example.storage.object; + +// [START storage_restore_object] +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobId; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +public class RestoreSoftDeletedObject { + public static void restoreSoftDeletedObject( + String projectId, String bucketName, String objectName, long generation) { + // The ID of your GCP project + // String projectId = "your-project-id"; + + // The ID of your GCS bucket + // String bucketName = "your-unique-bucket-name"; + + // The name of your GCS object + // String objectName = "your-object-name"; + + Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); + Blob blob = storage.restore(BlobId.of(bucketName, objectName, generation)); + + System.out.println("Restored previously soft-deleted object " + blob.getName()); + } +} +// [END storage_restore_object] diff --git a/samples/snippets/src/test/java/com/example/storage/ITBucketSnippets.java b/samples/snippets/src/test/java/com/example/storage/ITBucketSnippets.java index 2c10e71ff7..58444c6eca 100644 --- a/samples/snippets/src/test/java/com/example/storage/ITBucketSnippets.java +++ b/samples/snippets/src/test/java/com/example/storage/ITBucketSnippets.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -38,6 +39,7 @@ import com.example.storage.bucket.DisableDefaultEventBasedHold; import com.example.storage.bucket.DisableLifecycleManagement; import com.example.storage.bucket.DisableRequesterPays; +import com.example.storage.bucket.DisableSoftDelete; import com.example.storage.bucket.DisableUniformBucketLevelAccess; import com.example.storage.bucket.EnableBucketVersioning; import com.example.storage.bucket.EnableDefaultEventBasedHold; @@ -68,6 +70,7 @@ import com.example.storage.bucket.SetPublicAccessPreventionEnforced; import com.example.storage.bucket.SetPublicAccessPreventionInherited; import com.example.storage.bucket.SetRetentionPolicy; +import com.example.storage.bucket.SetSoftDeletePolicy; import com.example.storage.object.DownloadRequesterPaysObject; import com.example.storage.object.ReleaseEventBasedHold; import com.example.storage.object.ReleaseTemporaryHold; @@ -132,14 +135,9 @@ public class ITBucketSnippets { public static void beforeClass() { RemoteStorageHelper helper = RemoteStorageHelper.create(); storage = - helper - .getOptions() - .toBuilder() + helper.getOptions().toBuilder() .setRetrySettings( - helper - .getOptions() - .getRetrySettings() - .toBuilder() + helper.getOptions().getRetrySettings().toBuilder() .setRetryDelayMultiplier(3.0) .build()) .build() @@ -223,8 +221,7 @@ public void testGetBucketMetadata() { Bucket bucket = storage.get(BUCKET, Storage.BucketGetOption.fields(Storage.BucketField.values())); bucket = - bucket - .toBuilder() + bucket.toBuilder() .setLabels(ImmutableMap.of("k", "v")) .setLifecycleRules( ImmutableList.of( @@ -291,9 +288,7 @@ public void testEnableLifecycleManagement() throws Throwable { @Test public void testDisableLifecycleManagement() throws Throwable { - storage - .get(BUCKET) - .toBuilder() + storage.get(BUCKET).toBuilder() .setLifecycleRules( ImmutableList.of( new BucketInfo.LifecycleRule( @@ -313,9 +308,7 @@ public void testGetPublicAccessPrevention() throws Throwable { try { // By default a bucket PAP state is INHERITED and we are changing the state to validate // non-default state. - storage - .get(BUCKET) - .toBuilder() + storage.get(BUCKET).toBuilder() .setIamConfiguration( BucketInfo.IamConfiguration.newBuilder() .setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED) @@ -332,9 +325,7 @@ public void testGetPublicAccessPrevention() throws Throwable { assertTrue(snippetOutput.contains("enforced")); } finally { // No matter what happens make sure test set bucket back to INHERITED - storage - .get(BUCKET) - .toBuilder() + storage.get(BUCKET).toBuilder() .setIamConfiguration( BucketInfo.IamConfiguration.newBuilder() .setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED) @@ -356,9 +347,7 @@ public void testSetPublicAccessPreventionEnforced() throws Throwable { BucketInfo.PublicAccessPrevention.ENFORCED)); } finally { // No matter what happens make sure test set bucket back to INHERITED - storage - .get(BUCKET) - .toBuilder() + storage.get(BUCKET).toBuilder() .setIamConfiguration( BucketInfo.IamConfiguration.newBuilder() .setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED) @@ -371,9 +360,7 @@ public void testSetPublicAccessPreventionEnforced() throws Throwable { @Test public void testSetPublicAccessPreventionInherited() throws Throwable { try { - storage - .get(BUCKET) - .toBuilder() + storage.get(BUCKET).toBuilder() .setIamConfiguration( BucketInfo.IamConfiguration.newBuilder() .setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED) @@ -395,9 +382,7 @@ public void testSetPublicAccessPreventionInherited() throws Throwable { BucketInfo.PublicAccessPrevention.INHERITED)); } finally { // No matter what happens make sure test set bucket back to INHERITED - storage - .get(BUCKET) - .toBuilder() + storage.get(BUCKET).toBuilder() .setIamConfiguration( BucketInfo.IamConfiguration.newBuilder() .setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.INHERITED) @@ -456,9 +441,7 @@ public void testMakeBucketPublic() throws Throwable { @Test public void deleteBucketDefaultKmsKey() throws Throwable { - storage - .get(BUCKET) - .toBuilder() + storage.get(BUCKET).toBuilder() .setDefaultKmsKeyName( "projects/cloud-java-ci-sample/locations/us/keyRings/" + "gcs_test_kms_key_ring/cryptoKeys/gcs_kms_key_one") @@ -517,9 +500,7 @@ public void testConfigureBucketCors() throws Throwable { @Test public void testRemoveBucketCors() throws Throwable { - storage - .get(BUCKET) - .toBuilder() + storage.get(BUCKET).toBuilder() .setCors( ImmutableList.of( Cors.newBuilder() @@ -697,4 +678,36 @@ public void testCreateBucketWithObjectRetention() { storage.delete(tempBucket); } } + + @Test + public void testSetSoftDeletePolicy() { + String tempBucket = RemoteStorageHelper.generateBucketName(); + Bucket bucket = storage.create(BucketInfo.of(tempBucket)); + try { + assertNotEquals( + java.time.Duration.ofDays(10), bucket.getSoftDeletePolicy().getRetentionDuration()); + SetSoftDeletePolicy.setSoftDeletePolicy(PROJECT_ID, tempBucket); + assertEquals( + java.time.Duration.ofDays(10), + storage.get(tempBucket).getSoftDeletePolicy().getRetentionDuration()); + } finally { + storage.delete(tempBucket); + } + } + + @Test + public void testDisableSoftDelete() { + String tempBucket = RemoteStorageHelper.generateBucketName(); + Bucket bucket = storage.create(BucketInfo.of(tempBucket)); + try { + assertNotEquals( + java.time.Duration.ofDays(0), bucket.getSoftDeletePolicy().getRetentionDuration()); + DisableSoftDelete.disableSoftDelete(PROJECT_ID, tempBucket); + assertEquals( + java.time.Duration.ofSeconds(0), + storage.get(tempBucket).getSoftDeletePolicy().getRetentionDuration()); + } finally { + storage.delete(tempBucket); + } + } } diff --git a/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java b/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java index 3fdb0cfe88..aee96517dd 100644 --- a/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java +++ b/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java @@ -43,8 +43,11 @@ import com.example.storage.object.ListObjects; import com.example.storage.object.ListObjectsWithOldVersions; import com.example.storage.object.ListObjectsWithPrefix; +import com.example.storage.object.ListSoftDeletedObjects; +import com.example.storage.object.ListSoftDeletedVersionsOfObject; import com.example.storage.object.MakeObjectPublic; import com.example.storage.object.MoveObject; +import com.example.storage.object.RestoreSoftDeletedObject; import com.example.storage.object.RotateObjectEncryptionKey; import com.example.storage.object.SetObjectMetadata; import com.example.storage.object.SetObjectRetentionPolicy; @@ -73,6 +76,7 @@ import java.io.OutputStream; import java.net.URL; import java.nio.file.Files; +import java.time.Duration; import java.util.Date; import java.util.Map; import java.util.Random; @@ -441,9 +445,7 @@ public void testSetObjectRetentionPolicy() { assertNotNull(storage.get(tempBucket, retentionBlob).getRetention()); } finally { - storage - .get(tempBucket, retentionBlob) - .toBuilder() + storage.get(tempBucket, retentionBlob).toBuilder() .setRetention(null) .build() .update(Storage.BlobTargetOption.overrideUnlockedRetention(true)); @@ -451,4 +453,86 @@ public void testSetObjectRetentionPolicy() { storage.delete(tempBucket); } } + + @Test + public void testListSoftDeletedObjects() { + // This is already the default, but we set it here in case the default ever changes + storage.get(BUCKET).toBuilder() + .setSoftDeletePolicy( + BucketInfo.SoftDeletePolicy.newBuilder() + .setRetentionDuration(Duration.ofDays(7)) + .build()) + .build() + .update(); + + String blob = "softdelobj"; + storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build()); + storage.delete(BlobId.of(BUCKET, blob)); + + ListSoftDeletedObjects.listSoftDeletedObjects(PROJECT_ID, BUCKET); + + String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); + + assertTrue(snippetOutput.contains(blob)); + } + + @Test + public void testListSoftDeletedVersionsOfObject() { + // This is already the default, but we set it here in case the default ever changes + storage.get(BUCKET).toBuilder() + .setSoftDeletePolicy( + BucketInfo.SoftDeletePolicy.newBuilder() + .setRetentionDuration(Duration.ofDays(7)) + .build()) + .build() + .update(); + + System.out.println(storage.get(BUCKET).getSoftDeletePolicy().toString()); + + String blob = "softdelobj1"; + storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build()); + storage.delete(BlobId.of(BUCKET, blob)); + + String blob2 = "softdelobj2"; + storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob2)).build()); + storage.delete(BlobId.of(BUCKET, blob2)); + + ListSoftDeletedVersionsOfObject.listSoftDeletedVersionOfObject(PROJECT_ID, BUCKET, blob); + + String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); + + assertTrue(snippetOutput.contains(blob)); + assertFalse(snippetOutput.contains(blob2)); + } + + @Test + public void testRestoreSoftDeletedObject() { + String bucket = RemoteStorageHelper.generateBucketName(); + + storage.create(BucketInfo.of(bucket)); + try { + // This is already the default, but we set it here in case the default ever changes + storage.get(bucket).toBuilder() + .setSoftDeletePolicy( + BucketInfo.SoftDeletePolicy.newBuilder() + .setRetentionDuration(Duration.ofDays(7)) + .build()) + .build() + .update(); + + String blob = "restorableobj"; + + long gen = + storage.create(BlobInfo.newBuilder(BlobId.of(bucket, blob)).build()).getGeneration(); + storage.delete(BlobId.of(bucket, blob)); + + assertNull(storage.get(BlobId.of(bucket, blob))); + + RestoreSoftDeletedObject.restoreSoftDeletedObject(PROJECT_ID, bucket, blob, gen); + + assertNotNull(storage.get(BlobId.of(bucket, blob))); + } finally { + RemoteStorageHelper.forceDelete(storage, bucket); + } + } }