From 8724af67352f5f879a89bd34a376c9d44ff7c273 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Mon, 7 Oct 2024 11:33:08 -0700 Subject: [PATCH 1/7] feat: add samples for soft delete (objects) --- .../storage/bucket/DisableSoftDelete.java | 44 ++++++++++++++ .../storage/bucket/GetSoftDeletePolicy.java | 30 ++++++++++ .../storage/bucket/SetSoftDeletePolicy.java | 43 +++++++++++++ .../object/ListSoftDeletedObjects.java | 45 ++++++++++++++ .../ListSoftDeletedVersionsOfObject.java | 50 ++++++++++++++++ .../object/RestoreSoftDeletedObject.java | 42 +++++++++++++ .../com/example/storage/ITBucketSnippets.java | 30 ++++++++++ .../com/example/storage/ITObjectSnippets.java | 60 +++++++++++++++++++ 8 files changed, 344 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/storage/bucket/DisableSoftDelete.java create mode 100644 samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java create mode 100644 samples/snippets/src/main/java/com/example/storage/bucket/SetSoftDeletePolicy.java create mode 100644 samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedObjects.java create mode 100644 samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedVersionsOfObject.java create mode 100644 samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java 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..f763dbfc38 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/DisableSoftDelete.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_disable_soft_delete] +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(); + storage.get(bucketName).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/GetSoftDeletePolicy.java b/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java new file mode 100644 index 0000000000..5507ba3786 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java @@ -0,0 +1,30 @@ +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/SetSoftDeletePolicy.java b/samples/snippets/src/main/java/com/example/storage/bucket/SetSoftDeletePolicy.java new file mode 100644 index 0000000000..ee08b0d809 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/bucket/SetSoftDeletePolicy.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.bucket; + +// [START storage_set_soft_delete_policy] +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(); + storage.get(bucketName).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..b776f4c2fc --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedObjects.java @@ -0,0 +1,45 @@ +/* + * 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..629ac33670 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedVersionsOfObject.java @@ -0,0 +1,50 @@ +/* + * 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..32993ed9db --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java @@ -0,0 +1,42 @@ +/* + * 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 dc68167a68..118ed449c8 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; @@ -77,6 +80,7 @@ import com.google.cloud.Identity; import com.google.cloud.ServiceOptions; import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobId; import com.google.cloud.storage.BlobInfo; import com.google.cloud.storage.Bucket; import com.google.cloud.storage.BucketInfo; @@ -702,4 +706,30 @@ 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 0ebaa55c6d..8e85e1adbc 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; @@ -460,4 +464,60 @@ 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()); + + 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()); + + 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() { + //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()); + + 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))); + } } From ef3e09aec0743fc86bc210bededf888645fc64d9 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Tue, 26 Nov 2024 11:24:40 -0800 Subject: [PATCH 2/7] seperate calls; run lint --- .../com/example/storage/ConfigureRetries.java | 3 +- .../storage/bucket/DisableRequesterPays.java | 3 +- .../storage/bucket/DisableSoftDelete.java | 38 ++++++------ .../bucket/EnableLifecycleManagement.java | 3 +- .../EnableUniformBucketLevelAccess.java | 3 +- .../storage/bucket/GetSoftDeletePolicy.java | 28 ++++----- .../storage/bucket/MakeBucketPublic.java | 3 +- .../storage/bucket/SetBucketAutoclass.java | 3 +- .../SetPublicAccessPreventionEnforced.java | 3 +- .../SetPublicAccessPreventionInherited.java | 3 +- .../storage/bucket/SetRetentionPolicy.java | 3 +- .../storage/bucket/SetSoftDeletePolicy.java | 37 +++++++----- .../object/ListSoftDeletedObjects.java | 24 ++++---- .../ListSoftDeletedVersionsOfObject.java | 43 +++++++------- .../object/RestoreSoftDeletedObject.java | 23 ++++---- .../com/example/storage/ITBucketSnippets.java | 59 +++++++------------ .../com/example/storage/ITObjectSnippets.java | 31 ++++++---- .../example/storage/QuickstartSampleIT.java | 1 - 18 files changed, 148 insertions(+), 163 deletions(-) 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 index f763dbfc38..97d6571469 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/DisableSoftDelete.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/DisableSoftDelete.java @@ -17,28 +17,32 @@ 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(); - storage.get(bucketName).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"); - } + 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 index 5507ba3786..50a522a816 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java @@ -4,27 +4,25 @@ 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"; + 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"; + // 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(); + 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); - } + 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 index ee08b0d809..e923cb2faa 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/SetSoftDeletePolicy.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/SetSoftDeletePolicy.java @@ -17,27 +17,32 @@ 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(); - storage.get(bucketName).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"); - } + 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 index b776f4c2fc..eb3b5d158b 100644 --- a/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedObjects.java +++ b/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedObjects.java @@ -23,23 +23,19 @@ 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"; + 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"; + // 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)); + 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()); - } + 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 index 629ac33670..87532cdc8e 100644 --- a/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedVersionsOfObject.java +++ b/samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedVersionsOfObject.java @@ -24,27 +24,28 @@ 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()); - } + 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 index 32993ed9db..cb29c1d7b2 100644 --- a/samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java +++ b/samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java @@ -23,20 +23,21 @@ 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"; + 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 ID of your GCS bucket + // String bucketName = "your-unique-bucket-name"; - // The name of your GCS object - // String objectName = "your-object-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)); + 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()); - } + 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 118ed449c8..b74985f215 100644 --- a/samples/snippets/src/test/java/com/example/storage/ITBucketSnippets.java +++ b/samples/snippets/src/test/java/com/example/storage/ITBucketSnippets.java @@ -80,7 +80,6 @@ import com.google.cloud.Identity; import com.google.cloud.ServiceOptions; import com.google.cloud.storage.Blob; -import com.google.cloud.storage.BlobId; import com.google.cloud.storage.BlobInfo; import com.google.cloud.storage.Bucket; import com.google.cloud.storage.BucketInfo; @@ -138,14 +137,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() @@ -232,8 +226,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( @@ -300,9 +293,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( @@ -322,9 +313,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) @@ -341,9 +330,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) @@ -365,9 +352,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) @@ -380,9 +365,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) @@ -404,9 +387,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) @@ -465,9 +446,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") @@ -526,9 +505,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() @@ -712,9 +689,12 @@ public void testSetSoftDeletePolicy() { String tempBucket = RemoteStorageHelper.generateBucketName(); Bucket bucket = storage.create(BucketInfo.of(tempBucket)); try { - assertNotEquals(java.time.Duration.ofDays(10), bucket.getSoftDeletePolicy().getRetentionDuration()); + 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()); + assertEquals( + java.time.Duration.ofDays(10), + storage.get(tempBucket).getSoftDeletePolicy().getRetentionDuration()); } finally { storage.delete(tempBucket); } @@ -725,9 +705,12 @@ public void testDisableSoftDelete() { String tempBucket = RemoteStorageHelper.generateBucketName(); Bucket bucket = storage.create(BucketInfo.of(tempBucket)); try { - assertNotEquals(java.time.Duration.ofDays(0), bucket.getSoftDeletePolicy().getRetentionDuration()); + 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()); + 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 8e85e1adbc..8859431817 100644 --- a/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java +++ b/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java @@ -454,9 +454,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)); @@ -467,9 +465,12 @@ public void testSetObjectRetentionPolicy() { @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()); + // 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()); String blob = "softdelobj"; storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build()); @@ -484,9 +485,12 @@ public void testListSoftDeletedObjects() { @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()); + // 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()); String blob = "softdelobj1"; storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build()); @@ -506,9 +510,12 @@ public void testListSoftDeletedVersionsOfObject() { @Test public void testRestoreSoftDeletedObject() { - //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()); + // 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()); String blob = "restorableobj"; long gen = storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build()).getGeneration(); diff --git a/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java b/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java index 60d918e842..847ec0d75a 100644 --- a/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java +++ b/samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java @@ -26,7 +26,6 @@ import java.util.UUID; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; From a8828251da3e488fdb809b5f8c29f2c153e86f85 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Tue, 26 Nov 2024 11:27:14 -0800 Subject: [PATCH 3/7] style issues --- .../storage/bucket/GetSoftDeletePolicy.java | 15 +++++++++++++++ .../storage/object/RestoreSoftDeletedObject.java | 2 +- .../com/example/storage/ITObjectSnippets.java | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) 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 index 50a522a816..c9c7485d4d 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java @@ -1,3 +1,18 @@ +/* + * 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] 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 index cb29c1d7b2..86f09a39ec 100644 --- a/samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java +++ b/samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java @@ -23,7 +23,7 @@ import com.google.cloud.storage.StorageOptions; public class RestoreSoftDeletedObject { - public static void RestoreSoftDeletedObject( + public static void restoreSoftDeletedObject( String projectId, String bucketName, String objectName, long generation) { // The ID of your GCP project // String projectId = "your-project-id"; 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 8859431817..3e4a408a6e 100644 --- a/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java +++ b/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java @@ -523,7 +523,7 @@ public void testRestoreSoftDeletedObject() { assertNull(storage.get(BlobId.of(BUCKET, blob))); - RestoreSoftDeletedObject.RestoreSoftDeletedObject(PROJECT_ID, BUCKET, blob, gen); + RestoreSoftDeletedObject.restoreSoftDeletedObject(PROJECT_ID, BUCKET, blob, gen); assertNotNull(storage.get(BlobId.of(BUCKET, blob))); } From 9fc7b090b0c5dc342d9880dccc0714720c0a1834 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Tue, 26 Nov 2024 11:29:06 -0800 Subject: [PATCH 4/7] one more lint issue --- .../java/com/example/storage/bucket/GetSoftDeletePolicy.java | 1 + 1 file changed, 1 insertion(+) 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 index c9c7485d4d..32f4277a05 100644 --- a/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java +++ b/samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java @@ -13,6 +13,7 @@ * 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] From fd42a0fa96119d06ed339ca2b110de8444f93282 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Tue, 26 Nov 2024 12:05:14 -0800 Subject: [PATCH 5/7] fix test issue --- .../src/test/java/com/example/storage/ITObjectSnippets.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 3e4a408a6e..0b08d3f3c7 100644 --- a/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java +++ b/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java @@ -490,7 +490,8 @@ public void testListSoftDeletedVersionsOfObject() { .setSoftDeletePolicy( BucketInfo.SoftDeletePolicy.newBuilder() .setRetentionDuration(Duration.ofDays(7)) - .build()); + .build()) + .build(); String blob = "softdelobj1"; storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build()); From e8cc7d96e65cbd92d16274fc0f94ec6a45ea00d3 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Thu, 5 Dec 2024 14:45:42 -0800 Subject: [PATCH 6/7] create test-specific bucket in restore test --- .../com/example/storage/ITObjectSnippets.java | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) 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 0b08d3f3c7..0022161863 100644 --- a/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java +++ b/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java @@ -76,6 +76,7 @@ import java.io.OutputStream; import java.net.URL; import java.nio.file.Files; +import java.rmi.Remote; import java.time.Duration; import java.util.Date; import java.util.Map; @@ -470,7 +471,9 @@ public void testListSoftDeletedObjects() { .setSoftDeletePolicy( BucketInfo.SoftDeletePolicy.newBuilder() .setRetentionDuration(Duration.ofDays(7)) - .build()); + .build()) + .build() + .update(); String blob = "softdelobj"; storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build()); @@ -491,7 +494,11 @@ public void testListSoftDeletedVersionsOfObject() { BucketInfo.SoftDeletePolicy.newBuilder() .setRetentionDuration(Duration.ofDays(7)) .build()) - .build(); + .build() + .update(); + + System.out.println(storage.get(BUCKET).getSoftDeletePolicy().toString()); + String blob = "softdelobj1"; storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build()); @@ -511,21 +518,31 @@ public void testListSoftDeletedVersionsOfObject() { @Test public void testRestoreSoftDeletedObject() { - // 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()); + String bucket = RemoteStorageHelper.generateBucketName(); - String blob = "restorableobj"; - long gen = storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build()).getGeneration(); - storage.delete(BlobId.of(BUCKET, blob)); + 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"; - assertNull(storage.get(BlobId.of(BUCKET, blob))); + long gen = storage.create(BlobInfo.newBuilder(BlobId.of(bucket, blob)).build()).getGeneration(); + storage.delete(BlobId.of(bucket, blob)); - RestoreSoftDeletedObject.restoreSoftDeletedObject(PROJECT_ID, BUCKET, blob, gen); + assertNull(storage.get(BlobId.of(bucket, blob))); - assertNotNull(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); + } } } From 18610fff189b4cd50bd7da55b8d24264e86fdcc3 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Thu, 5 Dec 2024 15:03:14 -0800 Subject: [PATCH 7/7] format --- .../com/example/storage/ITObjectSnippets.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) 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 0022161863..092a0a889c 100644 --- a/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java +++ b/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java @@ -76,7 +76,6 @@ import java.io.OutputStream; import java.net.URL; import java.nio.file.Files; -import java.rmi.Remote; import java.time.Duration; import java.util.Date; import java.util.Map; @@ -499,7 +498,6 @@ public void testListSoftDeletedVersionsOfObject() { 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)); @@ -524,16 +522,17 @@ public void testRestoreSoftDeletedObject() { 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(); + .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(); + 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)));