From a5561c8032d2aedaaaf24653e1fbc08ebd0bc696 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Fri, 18 Apr 2025 03:33:34 -0700 Subject: [PATCH 01/19] Sample and test case to list soft deleted objects --- .../ListSoftDeletedObjectsTest.cs | 47 +++++++++++++++++++ .../Storage.Samples/ListSoftDeletedObjects.cs | 39 +++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs create mode 100644 storage/api/Storage.Samples/ListSoftDeletedObjects.cs diff --git a/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs new file mode 100644 index 00000000000..66235d3deeb --- /dev/null +++ b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs @@ -0,0 +1,47 @@ +// Copyright 2025 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 +// +// https://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. + +using System; +using Xunit; + +[Collection(nameof(StorageFixture))] +public class ListSoftDeletedObjectsTest +{ + private readonly StorageFixture _fixture; + + public ListSoftDeletedObjectsTest(StorageFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public void ListSoftDeletedObjects() + { + ListSoftDeletedObjectsSample listSoftDeletedObjects = new ListSoftDeletedObjectsSample(); + UploadObjectFromMemorySample uploadObjectFromMemory = new UploadObjectFromMemorySample(); + var bucketName = _fixture.GenerateBucketName(); + _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: false, registerForDeletion: true); + var objectName = Guid.NewGuid().ToString(); + var objectContents = Guid.NewGuid().ToString(); + var testObjectName = Guid.NewGuid().ToString(); + var testObjectContents = Guid.NewGuid().ToString(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectName, objectContents); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, testObjectName, testObjectContents); + _fixture.Client.DeleteObject(bucketName, objectName); + _fixture.Client.DeleteObject(bucketName, testObjectName); + var objects = listSoftDeletedObjects.ListSoftDeletedObjects(bucketName); + Assert.Contains(objects, c => c.Name == objectName); + Assert.Contains(objects, c => c.Name == testObjectName); + } +} diff --git a/storage/api/Storage.Samples/ListSoftDeletedObjects.cs b/storage/api/Storage.Samples/ListSoftDeletedObjects.cs new file mode 100644 index 00000000000..809b4ea319b --- /dev/null +++ b/storage/api/Storage.Samples/ListSoftDeletedObjects.cs @@ -0,0 +1,39 @@ +// Copyright 2025 Google Inc. +// +// 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. + +// [START storage_list_soft_deleted_objects] + +using Google.Cloud.Storage.V1; +using System; +using System.Collections.Generic; + +public class ListSoftDeletedObjectsSample +{ + /// + /// List soft deleted objects. + /// + /// The name of the bucket to list soft-deleted objects from. + public IEnumerable ListSoftDeletedObjects(string bucketName = "your-unique-bucket-name") + { + var storage = StorageClient.Create(); + var objects = storage.ListObjects(bucketName, null, new ListObjectsOptions { SoftDeletedOnly = true }); + Console.WriteLine("Soft Deleted Objects are as follows:"); + foreach (var obj in objects) + { + Console.WriteLine(obj.Name); + } + return objects; + } +} +// [END storage_list_soft_deleted_objects] From c529a983ee9437e2b8a4e6ff02272c3efd16efaa Mon Sep 17 00:00:00 2001 From: Mahendra Date: Mon, 21 Apr 2025 12:11:00 +0000 Subject: [PATCH 02/19] ListSoftDeletedObjects test changes --- .../api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs index 66235d3deeb..1f2178e7be2 100644 --- a/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs +++ b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs @@ -41,7 +41,6 @@ public void ListSoftDeletedObjects() _fixture.Client.DeleteObject(bucketName, objectName); _fixture.Client.DeleteObject(bucketName, testObjectName); var objects = listSoftDeletedObjects.ListSoftDeletedObjects(bucketName); - Assert.Contains(objects, c => c.Name == objectName); - Assert.Contains(objects, c => c.Name == testObjectName); + Assert.Contains(objects, c => c.Name == objectName && c.Name == testObjectName); } } From 01639cff185055f7b9815b7604fd76806c5d314e Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Mon, 21 Apr 2025 05:59:07 -0700 Subject: [PATCH 03/19] add sample and test case to restore soft deleted object --- .../RestoreSoftDeletedObjectTest.cs | 45 +++++++++++++++++++ .../RestoreSoftDeletedObject.cs | 38 ++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs create mode 100644 storage/api/Storage.Samples/RestoreSoftDeletedObject.cs diff --git a/storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs b/storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs new file mode 100644 index 00000000000..c6d4a1e8308 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs @@ -0,0 +1,45 @@ +// Copyright 2025 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 +// +// https://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. + +using System; +using Xunit; + +[Collection(nameof(StorageFixture))] +public class RestoreSoftDeletedObjectTest +{ + private readonly StorageFixture _fixture; + + public RestoreSoftDeletedObjectTest(StorageFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public void RestoreSoftDeletedObject() + { + RestoreSoftDeletedObjectSample restoreSoftDeletedObjectSample = new RestoreSoftDeletedObjectSample(); + UploadObjectFromMemorySample uploadObjectFromMemory = new UploadObjectFromMemorySample(); + GetMetadataSample getMetadataSample = new GetMetadataSample(); + var bucketName = _fixture.GenerateBucketName(); + _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var objectName = Guid.NewGuid().ToString(); + var objectContents = Guid.NewGuid().ToString(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectName, objectContents); + var objectMetaData = getMetadataSample.GetMetadata(bucketName, objectName); + _fixture.Client.DeleteObject(bucketName, objectName); + var restoredObject = restoreSoftDeletedObjectSample.RestoreSoftDeletedObject(bucketName, objectName, objectMetaData.Generation.Value); + Assert.Equal(objectName, restoredObject.Name); + _fixture.Client.DeleteObject(bucketName, objectName); + } +} diff --git a/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs b/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs new file mode 100644 index 00000000000..f975137c986 --- /dev/null +++ b/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs @@ -0,0 +1,38 @@ +// Copyright 2025 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 +// +// https://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. + +// [START storage_restore_object] + +using Google.Cloud.Storage.V1; +using System; +public class RestoreSoftDeletedObjectSample +{ + /// + /// Restores a soft deleted object. + /// + /// The name of the GCS bucket. + /// The name of the soft-deleted object to restore. + /// The generation number of the soft-deleted object to restore. + public Google.Apis.Storage.v1.Data.Object RestoreSoftDeletedObject( + string bucketName = "your-unique-bucket-name", + string objectName = "your-object-name", + long generation = 1579287380533984) + { + var client = StorageClient.Create(); + var restoredObject = client.RestoreObject(bucketName, objectName, generation); + Console.WriteLine($"The name of the restored previously soft-deleted object is : {restoredObject.Name}"); + return restoredObject; + } +} +// [END storage_restore_object] From d92cbbb7d774cb8b4d2c85422bc090acab907988 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Mon, 21 Apr 2025 19:14:27 +0530 Subject: [PATCH 04/19] Update ListSoftDeletedObjectsTest.cs --- storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs index 1f2178e7be2..016ed3883f8 100644 --- a/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs +++ b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs @@ -41,6 +41,6 @@ public void ListSoftDeletedObjects() _fixture.Client.DeleteObject(bucketName, objectName); _fixture.Client.DeleteObject(bucketName, testObjectName); var objects = listSoftDeletedObjects.ListSoftDeletedObjects(bucketName); - Assert.Contains(objects, c => c.Name == objectName && c.Name == testObjectName); + Assert.All(objects, c => Assert.Contains(objectName, c.Name) && Assert.Contains(testObjectName, c.Name)); } } From 65dff8d97fe741ac85fc563547e071d3216af436 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Mon, 21 Apr 2025 22:26:30 -0700 Subject: [PATCH 05/19] list soft deleted object test update --- .../api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs index 016ed3883f8..2e33eb27ab7 100644 --- a/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs +++ b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs @@ -41,6 +41,9 @@ public void ListSoftDeletedObjects() _fixture.Client.DeleteObject(bucketName, objectName); _fixture.Client.DeleteObject(bucketName, testObjectName); var objects = listSoftDeletedObjects.ListSoftDeletedObjects(bucketName); - Assert.All(objects, c => Assert.Contains(objectName, c.Name) && Assert.Contains(testObjectName, c.Name)); + Assert.Multiple( + () => Assert.Contains(objects, obj => obj.Name == objectName), + () => Assert.Contains(objects, obj => obj.Name == testObjectName) + ); } } From a7eac83b854893ad413cb81512480b3cf8765d1e Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Tue, 22 Apr 2025 01:45:45 -0700 Subject: [PATCH 06/19] restore soft delete sample changes --- storage/api/Storage.Samples/RestoreSoftDeletedObject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs b/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs index f975137c986..e1245dfc4a9 100644 --- a/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs +++ b/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs @@ -31,7 +31,7 @@ public Google.Apis.Storage.v1.Data.Object RestoreSoftDeletedObject( { var client = StorageClient.Create(); var restoredObject = client.RestoreObject(bucketName, objectName, generation); - Console.WriteLine($"The name of the restored previously soft-deleted object is : {restoredObject.Name}"); + Console.WriteLine($"The Name of the Restored Previously Soft-deleted Object is : {restoredObject.Name}"); return restoredObject; } } From 8adda4b76c8e07b025e9b3b5744395dace48af3b Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Tue, 22 Apr 2025 04:51:50 -0700 Subject: [PATCH 07/19] add sample and test case to list soft deleted versions of the object --- .../ListSoftDeletedVersionsOfObjectTest.cs | 59 +++++++++++++++++++ .../ListSoftDeletedVersionsOfObject.cs | 41 +++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 storage/api/Storage.Samples.Tests/ListSoftDeletedVersionsOfObjectTest.cs create mode 100644 storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs diff --git a/storage/api/Storage.Samples.Tests/ListSoftDeletedVersionsOfObjectTest.cs b/storage/api/Storage.Samples.Tests/ListSoftDeletedVersionsOfObjectTest.cs new file mode 100644 index 00000000000..4705fd3cf0b --- /dev/null +++ b/storage/api/Storage.Samples.Tests/ListSoftDeletedVersionsOfObjectTest.cs @@ -0,0 +1,59 @@ +// Copyright 2025 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 +// +// https://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. + +using System; +using System.Collections.Generic; +using Xunit; + +[Collection(nameof(StorageFixture))] +public class ListSoftDeletedVersionOfObjectTest +{ + private readonly StorageFixture _fixture; + private IList _softDeleteObjectGenerations { get; } = new List(); + + public ListSoftDeletedVersionOfObjectTest(StorageFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public void ListSoftDeletedVersionOfObject() + { + int i = 2; + ListSoftDeletedVersionOfObjectSample listSoftDeletedVersionOfObject = new ListSoftDeletedVersionOfObjectSample(); + UploadObjectFromMemorySample uploadObjectFromMemory = new UploadObjectFromMemorySample(); + RestoreSoftDeletedObjectSample restoreSoftDeletedObjectSample = new RestoreSoftDeletedObjectSample(); + GetMetadataSample getMetadataSample = new GetMetadataSample(); + var bucketName = _fixture.GenerateBucketName(); + _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: false, registerForDeletion: true); + var objectName = Guid.NewGuid().ToString(); + var objectContents = Guid.NewGuid().ToString(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectName, objectContents); + while (i >= 0) + { + var objectMetaData = getMetadataSample.GetMetadata(bucketName, objectName); + _softDeleteObjectGenerations.Add(objectMetaData.Generation.Value); + _fixture.Client.DeleteObject(bucketName, objectName); + var restoredObject = restoreSoftDeletedObjectSample.RestoreSoftDeletedObject(bucketName, objectName, objectMetaData.Generation.Value); + i--; + } + var objects = listSoftDeletedVersionOfObject.ListSoftDeletedVersionOfObject(bucketName, objectName); + Assert.Multiple( + () => Assert.Contains(objects, obj => obj.Name == objectName && obj.Generation == _softDeleteObjectGenerations[_softDeleteObjectGenerations.Count - 1]), + () => Assert.Contains(objects, obj => obj.Name == objectName && obj.Generation == _softDeleteObjectGenerations[_softDeleteObjectGenerations.Count - 2]), + () => Assert.Contains(objects, obj => obj.Name == objectName && obj.Generation == _softDeleteObjectGenerations[_softDeleteObjectGenerations.Count - 3]) + ); + _fixture.Client.DeleteObject(bucketName, objectName); + } +} diff --git a/storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs b/storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs new file mode 100644 index 00000000000..ef08c6653ec --- /dev/null +++ b/storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs @@ -0,0 +1,41 @@ +// Copyright 2025 Google Inc. +// +// 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. + +// [START storage_list_soft_deleted_object_versions] + +using Google.Cloud.Storage.V1; +using System; +using System.Collections.Generic; + +public class ListSoftDeletedVersionOfObjectSample +{ + /// + /// List soft deleted versions of the object. + /// + /// The name of the bucket to list soft-deleted versions of the object from. + /// The name of the soft-deleted object to view all the versions of. + public IEnumerable ListSoftDeletedVersionOfObject(string bucketName = "your-unique-bucket-name", + string objectName = "your-object-name") + { + var storage = StorageClient.Create(); + var objects = storage.ListObjects(bucketName, null, new ListObjectsOptions { SoftDeletedOnly = true, MatchGlob = objectName }); + Console.WriteLine("Soft Deleted Versions of the Object with the Name are as follows:"); + foreach (var obj in objects) + { + Console.WriteLine($"Name: {obj.Name} and Version : {obj.Generation}"); + } + return objects; + } +} +// [END storage_list_soft_deleted_object_versions] From c6edeb902e39a236b90c3c682027fae5f823d374 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Wed, 23 Apr 2025 05:10:43 -0700 Subject: [PATCH 08/19] add sample and test case to set soft delete policy for the bucket --- .../BucketSetSoftDeletePolicyTest.cs | 36 ++++++++++++++++++ .../BucketSetSoftDeletePolicy.cs | 37 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs create mode 100644 storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs new file mode 100644 index 00000000000..c0938ac12b8 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -0,0 +1,36 @@ +// Copyright 2025 Google Inc. +// +// 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. + +using Xunit; + +[Collection(nameof(StorageFixture))] +public class BucketSetSoftDeletePolicyTest +{ + private readonly StorageFixture _fixture; + + public BucketSetSoftDeletePolicyTest(StorageFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public void BucketSetSoftDeletePolicy() + { + BucketSetSoftDeletePolicySample bucketSetSoftDeletePolicy = new BucketSetSoftDeletePolicySample(); + var bucketName = _fixture.GenerateBucketName(); + _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var bucket = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName); + Assert.Equal("864000", bucket.SoftDeletePolicy.RetentionDurationSeconds.ToString()); + } +} diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs new file mode 100644 index 00000000000..4c0560af80f --- /dev/null +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -0,0 +1,37 @@ +// Copyright 2025 Google Inc. +// +// 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. + +// [START storage_set_soft_delete_policy] + +using Google.Apis.Storage.v1.Data; +using Google.Cloud.Storage.V1; +using System; + +public class BucketSetSoftDeletePolicySample +{ + /// + /// Set soft delete policy for the bucket. + /// + /// The name of the bucket to set soft-delete policy for. + public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket-name") + { + var storage = StorageClient.Create(); + var bucket = storage.GetBucket(bucketName); + bucket.SoftDeletePolicy.RetentionDurationSeconds = 864000; + bucket = storage.UpdateBucket(bucket); + Console.WriteLine($"Soft Delete Policy for the {bucketName} is set to the 10 days retention duration"); + return bucket; + } +} +// [END storage_set_soft_delete_policy] From ba02d028907da072fe9e3355e180bfb11c40ecf5 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Wed, 23 Apr 2025 08:28:14 -0700 Subject: [PATCH 09/19] sample and test case update to set soft delete policy for bucket --- .../BucketSetSoftDeletePolicyTest.cs | 7 +++++-- storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs | 8 +++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs index c0938ac12b8..f76517c054b 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -30,7 +30,10 @@ public void BucketSetSoftDeletePolicy() BucketSetSoftDeletePolicySample bucketSetSoftDeletePolicy = new BucketSetSoftDeletePolicySample(); var bucketName = _fixture.GenerateBucketName(); _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); - var bucket = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName); - Assert.Equal("864000", bucket.SoftDeletePolicy.RetentionDurationSeconds.ToString()); + long retentionDuration = 10; + // Set soft-delete policy for the bucket. + var bucket = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDuration); + long retentionDurationInSeconds = retentionDuration * 24 * 60 * 60; + Assert.Equal(bucket.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); } } diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs index 4c0560af80f..a61d8577f00 100644 --- a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -24,13 +24,15 @@ public class BucketSetSoftDeletePolicySample /// Set soft delete policy for the bucket. /// /// The name of the bucket to set soft-delete policy for. - public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket-name") + public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket-name", + long retentionDuration = 10) { var storage = StorageClient.Create(); var bucket = storage.GetBucket(bucketName); - bucket.SoftDeletePolicy.RetentionDurationSeconds = 864000; + long retentionDurationInSeconds = retentionDuration * 24 * 60 * 60 ; + bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; bucket = storage.UpdateBucket(bucket); - Console.WriteLine($"Soft Delete Policy for the {bucketName} is set to the 10 days retention duration"); + Console.WriteLine($"Soft Delete Policy for the {bucketName} is set to the {retentionDuration} days retention duration"); return bucket; } } From 1812bf28072461cc3d66b34090db65cef31a0b59 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Thu, 24 Apr 2025 04:06:24 -0700 Subject: [PATCH 10/19] sample and test soft delete policy changes --- .../BucketSetSoftDeletePolicyTest.cs | 2 +- .../Storage.Samples/BucketSetSoftDeletePolicy.cs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs index f76517c054b..e324c96416d 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -33,7 +33,7 @@ public void BucketSetSoftDeletePolicy() long retentionDuration = 10; // Set soft-delete policy for the bucket. var bucket = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDuration); - long retentionDurationInSeconds = retentionDuration * 24 * 60 * 60; + long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); Assert.Equal(bucket.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); } } diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs index a61d8577f00..d25b7de2afb 100644 --- a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -29,9 +29,17 @@ public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket- { var storage = StorageClient.Create(); var bucket = storage.GetBucket(bucketName); - long retentionDurationInSeconds = retentionDuration * 24 * 60 * 60 ; - bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; - bucket = storage.UpdateBucket(bucket); + long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); + if (retentionDuration < 7 || retentionDuration > 90) + { + Console.WriteLine($"Soft Delete Policy for the {bucketName} must have a retention duration between 7 days and 90 days"); + return bucket; + } + else + { + bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; + bucket = storage.UpdateBucket(bucket); + } Console.WriteLine($"Soft Delete Policy for the {bucketName} is set to the {retentionDuration} days retention duration"); return bucket; } From b0024fb87e911005f27ab87537497f6b5dc2468c Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Thu, 24 Apr 2025 05:17:47 -0700 Subject: [PATCH 11/19] data type for retention duration and sample description changes --- .../api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs | 2 +- storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs index e324c96416d..34791580469 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -30,7 +30,7 @@ public void BucketSetSoftDeletePolicy() BucketSetSoftDeletePolicySample bucketSetSoftDeletePolicy = new BucketSetSoftDeletePolicySample(); var bucketName = _fixture.GenerateBucketName(); _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); - long retentionDuration = 10; + int retentionDuration = 10; // Set soft-delete policy for the bucket. var bucket = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDuration); long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs index d25b7de2afb..e7b238757cd 100644 --- a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -24,8 +24,9 @@ public class BucketSetSoftDeletePolicySample /// Set soft delete policy for the bucket. /// /// The name of the bucket to set soft-delete policy for. + /// The retention duration to set soft-delete policy for the bucket. public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket-name", - long retentionDuration = 10) + int retentionDuration = 10) { var storage = StorageClient.Create(); var bucket = storage.GetBucket(bucketName); From 01e5d9ab2c6d1073e81dfc233bc0e0238fde97e5 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Fri, 25 Apr 2025 02:25:57 -0700 Subject: [PATCH 12/19] bucket set soft delete policy assert added --- .../BucketSetSoftDeletePolicyTest.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs index 34791580469..98ff97b6127 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -29,11 +29,12 @@ public void BucketSetSoftDeletePolicy() { BucketSetSoftDeletePolicySample bucketSetSoftDeletePolicy = new BucketSetSoftDeletePolicySample(); var bucketName = _fixture.GenerateBucketName(); - _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var bucketPreSetSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); int retentionDuration = 10; - // Set soft-delete policy for the bucket. - var bucket = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDuration); long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); - Assert.Equal(bucket.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + Assert.NotEqual(bucketPreSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + // Set soft-delete policy for the bucket. + var bucketPostSetSoftDeletePolicy = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDuration); + Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); } } From 4d6b96b129f0abc5c855153507aa0d7d3d6908da Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Fri, 25 Apr 2025 03:21:22 -0700 Subject: [PATCH 13/19] add sample and test case to disable bucket soft delete policy --- .../BucketDisableSoftDeletePolicyTest.cs | 52 +++++++++++++++++++ .../BucketDisableSoftDeletePolicy.cs | 40 ++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs create mode 100644 storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs diff --git a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs new file mode 100644 index 00000000000..2132931bdb6 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs @@ -0,0 +1,52 @@ +// Copyright 2025 Google Inc. +// +// 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. + +using Google; +using System; +using System.Net; +using Xunit; + +[Collection(nameof(StorageFixture))] +public class BucketDisableSoftDeletePolicyTest +{ + private readonly StorageFixture _fixture; + + public BucketDisableSoftDeletePolicyTest(StorageFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public void BucketDisableSoftDeletePolicy() + { + BucketDisableSoftDeletePolicySample bucketDisableSoftDeletePolicy = new BucketDisableSoftDeletePolicySample(); + UploadObjectFromMemorySample uploadObjectFromMemory = new UploadObjectFromMemorySample(); + GetMetadataSample getMetadataSample = new GetMetadataSample(); + var bucketName = _fixture.GenerateBucketName(); + var bucketPreDisableSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var originName = Guid.NewGuid().ToString(); + var originContent = Guid.NewGuid().ToString(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, originName, originContent); + var objectMetaData = getMetadataSample.GetMetadata(bucketName, originName); + int retentionDuration = 0; + long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); + Assert.NotEqual(bucketPreDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0 days. + var bucketPostDisableSoftDeletePolicy = bucketDisableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, retentionDuration); + Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + _fixture.Client.DeleteObject(bucketName, originName); + var exception = Assert.Throws(() => _fixture.Client.RestoreObject(bucketName, originName, objectMetaData.Generation.Value)); + Assert.Equal(HttpStatusCode.BadRequest, exception.HttpStatusCode); + } +} diff --git a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs new file mode 100644 index 00000000000..3abacf6e039 --- /dev/null +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs @@ -0,0 +1,40 @@ +// Copyright 2025 Google Inc. +// +// 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. + +// [START storage_disable_soft_delete] + +using Google.Apis.Storage.v1.Data; +using Google.Cloud.Storage.V1; +using System; + +public class BucketDisableSoftDeletePolicySample +{ + /// + /// Disable soft delete policy for the bucket. + /// + /// The name of the bucket to disable soft-delete policy for. + /// The retention duration to disable soft-delete policy for the bucket. + public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-bucket-name", + int retentionDuration = 0) + { + var storage = StorageClient.Create(); + var bucket = storage.GetBucket(bucketName); + long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); + bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; + bucket = storage.UpdateBucket(bucket); + Console.WriteLine($"Soft Delete Policy for the {bucketName} is disabled"); + return bucket; + } +} +// [END storage_disable_soft_delete] From c7c30e71987e69f6814a8283f6c34dcb1807d1ed Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Sun, 27 Apr 2025 00:14:28 -0700 Subject: [PATCH 14/19] One blank space added between using and class --- storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs b/storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs index 870a01ea5cb..0c4a9cd0bbc 100644 --- a/storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs +++ b/storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs @@ -17,6 +17,7 @@ using Google.Apis.Storage.v1.Data; using Google.Cloud.Storage.V1; using System; + public class RestoreSoftDeletedBucketSample { /// From f199635b09c58bee364d3069be4d10c455935090 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Mon, 28 Apr 2025 05:46:28 +0000 Subject: [PATCH 15/19] Revert "One blank space added between using and class" This reverts commit c7c30e71987e69f6814a8283f6c34dcb1807d1ed. --- storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs b/storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs index 0c4a9cd0bbc..870a01ea5cb 100644 --- a/storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs +++ b/storage/api/Storage.Samples/RestoreSoftDeletedBucket.cs @@ -17,7 +17,6 @@ using Google.Apis.Storage.v1.Data; using Google.Cloud.Storage.V1; using System; - public class RestoreSoftDeletedBucketSample { /// From f3d153c8f6227d0aa7fe09096e29a99ce0cbfc38 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Mon, 28 Apr 2025 04:59:26 -0700 Subject: [PATCH 16/19] add sample and test case to get bucket soft delete policy --- .../BucketGetSoftDeletePolicyTest.cs | 50 +++++++++++++++++++ .../BucketGetSoftDeletePolicy.cs | 43 ++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs create mode 100644 storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs new file mode 100644 index 00000000000..f2a9cb8f6a7 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -0,0 +1,50 @@ +// Copyright 2025 Google Inc. +// +// 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. + +using Xunit; + +[Collection(nameof(StorageFixture))] +public class BucketGetSoftDeletePolicyTest +{ + private readonly StorageFixture _fixture; + + public BucketGetSoftDeletePolicyTest(StorageFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public void BucketGetSoftDeletePolicy() + { + BucketGetSoftDeletePolicySample getBucketSoftDeletePolicy = new BucketGetSoftDeletePolicySample(); + BucketSetSoftDeletePolicySample setSoftDeletePolicy = new BucketSetSoftDeletePolicySample(); + BucketDisableSoftDeletePolicySample disableSoftDeletePolicy = new BucketDisableSoftDeletePolicySample(); + var bucketName = _fixture.GenerateBucketName(); + var bucketPreFetchSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var bucketPostFetchSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); + Assert.Equal(bucketPreFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds); + int retentionDuration = 10; + long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); + Assert.NotEqual(bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + setSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDuration); + var bucketPostSetSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); + Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + int disableSoftDeleteRetentionDuration = 0; + long disableSoftDeleteRetentionDurationInSeconds = disableSoftDeleteRetentionDuration * (24 * 60 * 60); + Assert.NotEqual(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); + disableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, disableSoftDeleteRetentionDuration); + var bucketPostDisableSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); + Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); + } +} diff --git a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs new file mode 100644 index 00000000000..2a581ba8976 --- /dev/null +++ b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs @@ -0,0 +1,43 @@ +// Copyright 2025 Google Inc. +// +// 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. + +// [START storage_get_soft_delete_policy] + +using Google.Apis.Storage.v1.Data; +using Google.Cloud.Storage.V1; +using System; + +public class BucketGetSoftDeletePolicySample +{ + /// + /// Get soft delete policy of the bucket. + /// + /// The name of the bucket to get soft-delete policy of. + public Bucket BucketGetSoftDeletePolicy(string bucketName = "your-unique-bucket-name") + { + var storage = StorageClient.Create(); + var bucket = storage.GetBucket(bucketName); + if (bucket.SoftDeletePolicy.RetentionDurationSeconds == 0) + { + Console.WriteLine($"The Soft Delete Policy is disabled for the {bucketName}"); + } + else + { + int retentionDuration = (int) (bucket.SoftDeletePolicy.RetentionDurationSeconds / (24 * 60 * 60)); + Console.WriteLine($"The Soft Delete Policy for the {bucketName} is : {retentionDuration} days"); + } + return bucket; + } +} +// [END storage_get_soft_delete_policy] From cd9823807c86fd4bc6dfe60464fdb353e3887f1b Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Mon, 28 Apr 2025 05:03:21 -0700 Subject: [PATCH 17/19] chore(deps): update dependency google.cloud.storage.v1 to 4.13.0 --- storage/api/Storage.Samples/Storage.Samples.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/api/Storage.Samples/Storage.Samples.csproj b/storage/api/Storage.Samples/Storage.Samples.csproj index 9d1dbdcfc5e..024b16e560f 100644 --- a/storage/api/Storage.Samples/Storage.Samples.csproj +++ b/storage/api/Storage.Samples/Storage.Samples.csproj @@ -6,7 +6,7 @@ - + From d9c4c9bbe89bc5b485c82037f9e677a23e4c1a85 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Mon, 28 Apr 2025 05:09:44 -0700 Subject: [PATCH 18/19] add space between using namespaces and start of class --- storage/api/Storage.Samples/RestoreSoftDeletedObject.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs b/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs index e1245dfc4a9..088e4322eb6 100644 --- a/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs +++ b/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs @@ -16,6 +16,7 @@ using Google.Cloud.Storage.V1; using System; + public class RestoreSoftDeletedObjectSample { /// From 3fee8c069eb543475d0753bd1edff0e594644944 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Wed, 30 Apr 2025 04:19:56 -0700 Subject: [PATCH 19/19] Recommended changes as a part of code review --- .../BucketDisableSoftDeletePolicyTest.cs | 12 +++++----- .../BucketGetSoftDeletePolicyTest.cs | 24 ++++++++++++++----- .../BucketSetSoftDeletePolicyTest.cs | 7 +++--- .../ListSoftDeletedObjectsTest.cs | 20 ++++++++-------- .../ListSoftDeletedVersionsOfObjectTest.cs | 7 +++--- .../RestoreSoftDeletedObjectTest.cs | 6 ++--- .../Storage.Samples.Tests/StorageFixture.cs | 4 ++++ .../BucketDisableSoftDeletePolicy.cs | 8 +++---- .../BucketGetSoftDeletePolicy.cs | 2 +- .../BucketSetSoftDeletePolicy.cs | 12 +++++----- .../Storage.Samples/ListSoftDeletedObjects.cs | 4 ++-- .../ListSoftDeletedVersionsOfObject.cs | 6 ++--- .../RestoreSoftDeletedObject.cs | 2 +- 13 files changed, 65 insertions(+), 49 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs index 2132931bdb6..a4a1dabae15 100644 --- a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs @@ -35,15 +35,15 @@ public void BucketDisableSoftDeletePolicy() GetMetadataSample getMetadataSample = new GetMetadataSample(); var bucketName = _fixture.GenerateBucketName(); var bucketPreDisableSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); - var originName = Guid.NewGuid().ToString(); - var originContent = Guid.NewGuid().ToString(); + var originName = _fixture.GenerateName(); + var originContent = _fixture.GenerateContent(); uploadObjectFromMemory.UploadObjectFromMemory(bucketName, originName, originContent); var objectMetaData = getMetadataSample.GetMetadata(bucketName, originName); - int retentionDuration = 0; - long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); + int retentionDurationInDay = 0; + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDay).TotalSeconds; Assert.NotEqual(bucketPreDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0 days. - var bucketPostDisableSoftDeletePolicy = bucketDisableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, retentionDuration); + // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0. + var bucketPostDisableSoftDeletePolicy = bucketDisableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, retentionDurationInDay); Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); _fixture.Client.DeleteObject(bucketName, originName); var exception = Assert.Throws(() => _fixture.Client.RestoreObject(bucketName, originName, objectMetaData.Generation.Value)); diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs index f2a9cb8f6a7..02c4d8e14a3 100644 --- a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +using Google; +using System; +using System.Net; using Xunit; [Collection(nameof(StorageFixture))] @@ -30,21 +33,30 @@ public void BucketGetSoftDeletePolicy() BucketGetSoftDeletePolicySample getBucketSoftDeletePolicy = new BucketGetSoftDeletePolicySample(); BucketSetSoftDeletePolicySample setSoftDeletePolicy = new BucketSetSoftDeletePolicySample(); BucketDisableSoftDeletePolicySample disableSoftDeletePolicy = new BucketDisableSoftDeletePolicySample(); + UploadObjectFromMemorySample uploadObjectFromMemory = new UploadObjectFromMemorySample(); + GetMetadataSample getMetadataSample = new GetMetadataSample(); var bucketName = _fixture.GenerateBucketName(); var bucketPreFetchSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var originName = _fixture.GenerateName(); + var originContent = _fixture.GenerateContent(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, originName, originContent); + var objectMetaData = getMetadataSample.GetMetadata(bucketName, originName); var bucketPostFetchSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPreFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds); - int retentionDuration = 10; - long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); + int retentionDurationInDays = 10; + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - setSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDuration); + setSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); var bucketPostSetSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - int disableSoftDeleteRetentionDuration = 0; - long disableSoftDeleteRetentionDurationInSeconds = disableSoftDeleteRetentionDuration * (24 * 60 * 60); + int disableSoftDeleteRetentionDurationInDay = 0; + long disableSoftDeleteRetentionDurationInSeconds = (long) TimeSpan.FromDays(disableSoftDeleteRetentionDurationInDay).TotalSeconds; Assert.NotEqual(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); - disableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, disableSoftDeleteRetentionDuration); + disableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, disableSoftDeleteRetentionDurationInDay); var bucketPostDisableSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); + _fixture.Client.DeleteObject(bucketName, originName); + var exception = Assert.Throws(() => _fixture.Client.RestoreObject(bucketName, originName, objectMetaData.Generation.Value)); + Assert.Equal(HttpStatusCode.BadRequest, exception.HttpStatusCode); } } diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs index 98ff97b6127..a9a1612c8c1 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; using Xunit; [Collection(nameof(StorageFixture))] @@ -30,11 +31,11 @@ public void BucketSetSoftDeletePolicy() BucketSetSoftDeletePolicySample bucketSetSoftDeletePolicy = new BucketSetSoftDeletePolicySample(); var bucketName = _fixture.GenerateBucketName(); var bucketPreSetSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); - int retentionDuration = 10; - long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); + int retentionDurationInDays = 10; + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPreSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); // Set soft-delete policy for the bucket. - var bucketPostSetSoftDeletePolicy = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDuration); + var bucketPostSetSoftDeletePolicy = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); } } diff --git a/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs index 2e33eb27ab7..4b37bd64e81 100644 --- a/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs +++ b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs @@ -32,18 +32,18 @@ public void ListSoftDeletedObjects() UploadObjectFromMemorySample uploadObjectFromMemory = new UploadObjectFromMemorySample(); var bucketName = _fixture.GenerateBucketName(); _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: false, registerForDeletion: true); - var objectName = Guid.NewGuid().ToString(); - var objectContents = Guid.NewGuid().ToString(); - var testObjectName = Guid.NewGuid().ToString(); - var testObjectContents = Guid.NewGuid().ToString(); - uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectName, objectContents); - uploadObjectFromMemory.UploadObjectFromMemory(bucketName, testObjectName, testObjectContents); - _fixture.Client.DeleteObject(bucketName, objectName); - _fixture.Client.DeleteObject(bucketName, testObjectName); + var objectNameOne = _fixture.GenerateName(); + var objectOneContent = _fixture.GenerateContent(); + var objectNameTwo = _fixture.GenerateName(); + var objectTwoContent = _fixture.GenerateContent(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectNameOne, objectOneContent); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectNameTwo, objectTwoContent); + _fixture.Client.DeleteObject(bucketName, objectNameOne); + _fixture.Client.DeleteObject(bucketName, objectNameTwo); var objects = listSoftDeletedObjects.ListSoftDeletedObjects(bucketName); Assert.Multiple( - () => Assert.Contains(objects, obj => obj.Name == objectName), - () => Assert.Contains(objects, obj => obj.Name == testObjectName) + () => Assert.Contains(objects, obj => obj.Name == objectNameOne), + () => Assert.Contains(objects, obj => obj.Name == objectNameTwo) ); } } diff --git a/storage/api/Storage.Samples.Tests/ListSoftDeletedVersionsOfObjectTest.cs b/storage/api/Storage.Samples.Tests/ListSoftDeletedVersionsOfObjectTest.cs index 4705fd3cf0b..b68dcc9fcc2 100644 --- a/storage/api/Storage.Samples.Tests/ListSoftDeletedVersionsOfObjectTest.cs +++ b/storage/api/Storage.Samples.Tests/ListSoftDeletedVersionsOfObjectTest.cs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; using System.Collections.Generic; using Xunit; @@ -37,9 +36,9 @@ public void ListSoftDeletedVersionOfObject() GetMetadataSample getMetadataSample = new GetMetadataSample(); var bucketName = _fixture.GenerateBucketName(); _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: false, registerForDeletion: true); - var objectName = Guid.NewGuid().ToString(); - var objectContents = Guid.NewGuid().ToString(); - uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectName, objectContents); + var objectName = _fixture.GenerateName(); + var objectContent = _fixture.GenerateContent(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectName, objectContent); while (i >= 0) { var objectMetaData = getMetadataSample.GetMetadata(bucketName, objectName); diff --git a/storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs b/storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs index c6d4a1e8308..2635061f977 100644 --- a/storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs +++ b/storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs @@ -33,9 +33,9 @@ public void RestoreSoftDeletedObject() GetMetadataSample getMetadataSample = new GetMetadataSample(); var bucketName = _fixture.GenerateBucketName(); _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); - var objectName = Guid.NewGuid().ToString(); - var objectContents = Guid.NewGuid().ToString(); - uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectName, objectContents); + var objectName = _fixture.GenerateName(); + var objectContent = _fixture.GenerateContent(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectName, objectContent); var objectMetaData = getMetadataSample.GetMetadata(bucketName, objectName); _fixture.Client.DeleteObject(bucketName, objectName); var restoredObject = restoreSoftDeletedObjectSample.RestoreSoftDeletedObject(bucketName, objectName, objectMetaData.Generation.Value); diff --git a/storage/api/Storage.Samples.Tests/StorageFixture.cs b/storage/api/Storage.Samples.Tests/StorageFixture.cs index 9bcc2903b58..6161e7efe4f 100644 --- a/storage/api/Storage.Samples.Tests/StorageFixture.cs +++ b/storage/api/Storage.Samples.Tests/StorageFixture.cs @@ -239,6 +239,10 @@ internal Bucket CreateBucket(string name, bool multiVersion, bool softDelete = f internal string GenerateBucketName() => Guid.NewGuid().ToString(); + internal string GenerateName() => Guid.NewGuid().ToString(); + + internal string GenerateContent() => Guid.NewGuid().ToString(); + /// /// Bucket creation/update/deletion is rate-limited. To avoid making the tests flaky, we sleep after each operation. /// diff --git a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs index 3abacf6e039..0afcff0a773 100644 --- a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs @@ -23,14 +23,14 @@ public class BucketDisableSoftDeletePolicySample /// /// Disable soft delete policy for the bucket. /// - /// The name of the bucket to disable soft-delete policy for. - /// The retention duration to disable soft-delete policy for the bucket. + /// The name of the bucket. + /// The retention duration to disable soft-delete policy for the bucket. public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-bucket-name", - int retentionDuration = 0) + int retentionDurationInDay = 0) { var storage = StorageClient.Create(); var bucket = storage.GetBucket(bucketName); - long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDay).TotalSeconds; bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; bucket = storage.UpdateBucket(bucket); Console.WriteLine($"Soft Delete Policy for the {bucketName} is disabled"); diff --git a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs index 2a581ba8976..3227b98a9ee 100644 --- a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs @@ -23,7 +23,7 @@ public class BucketGetSoftDeletePolicySample /// /// Get soft delete policy of the bucket. /// - /// The name of the bucket to get soft-delete policy of. + /// The name of the bucket. public Bucket BucketGetSoftDeletePolicy(string bucketName = "your-unique-bucket-name") { var storage = StorageClient.Create(); diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs index e7b238757cd..fb12ca30428 100644 --- a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -23,15 +23,15 @@ public class BucketSetSoftDeletePolicySample /// /// Set soft delete policy for the bucket. /// - /// The name of the bucket to set soft-delete policy for. - /// The retention duration to set soft-delete policy for the bucket. + /// The name of the bucket. + /// The retention duration in days to set soft-delete policy for the bucket. public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket-name", - int retentionDuration = 10) + int retentionDurationInDays = 10) { var storage = StorageClient.Create(); var bucket = storage.GetBucket(bucketName); - long retentionDurationInSeconds = retentionDuration * (24 * 60 * 60); - if (retentionDuration < 7 || retentionDuration > 90) + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; + if (retentionDurationInDays < 7 || retentionDurationInDays > 90) { Console.WriteLine($"Soft Delete Policy for the {bucketName} must have a retention duration between 7 days and 90 days"); return bucket; @@ -41,7 +41,7 @@ public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket- bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; bucket = storage.UpdateBucket(bucket); } - Console.WriteLine($"Soft Delete Policy for the {bucketName} is set to the {retentionDuration} days retention duration"); + Console.WriteLine($"Soft Delete Policy for the {bucketName} is set to the {retentionDurationInDays} days retention duration"); return bucket; } } diff --git a/storage/api/Storage.Samples/ListSoftDeletedObjects.cs b/storage/api/Storage.Samples/ListSoftDeletedObjects.cs index 809b4ea319b..8e3e6b9b133 100644 --- a/storage/api/Storage.Samples/ListSoftDeletedObjects.cs +++ b/storage/api/Storage.Samples/ListSoftDeletedObjects.cs @@ -23,7 +23,7 @@ public class ListSoftDeletedObjectsSample /// /// List soft deleted objects. /// - /// The name of the bucket to list soft-deleted objects from. + /// The name of the bucket. public IEnumerable ListSoftDeletedObjects(string bucketName = "your-unique-bucket-name") { var storage = StorageClient.Create(); @@ -31,7 +31,7 @@ public class ListSoftDeletedObjectsSample Console.WriteLine("Soft Deleted Objects are as follows:"); foreach (var obj in objects) { - Console.WriteLine(obj.Name); + Console.WriteLine($"Name: {obj.Name}"); } return objects; } diff --git a/storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs b/storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs index ef08c6653ec..4dd25f8ab93 100644 --- a/storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs +++ b/storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs @@ -23,14 +23,14 @@ public class ListSoftDeletedVersionOfObjectSample /// /// List soft deleted versions of the object. /// - /// The name of the bucket to list soft-deleted versions of the object from. - /// The name of the soft-deleted object to view all the versions of. + /// The name of the bucket. + /// The name of the soft-deleted object. public IEnumerable ListSoftDeletedVersionOfObject(string bucketName = "your-unique-bucket-name", string objectName = "your-object-name") { var storage = StorageClient.Create(); var objects = storage.ListObjects(bucketName, null, new ListObjectsOptions { SoftDeletedOnly = true, MatchGlob = objectName }); - Console.WriteLine("Soft Deleted Versions of the Object with the Name are as follows:"); + Console.WriteLine("Soft Deleted Versions of the Object with the Name and Versions are as follows:"); foreach (var obj in objects) { Console.WriteLine($"Name: {obj.Name} and Version : {obj.Generation}"); diff --git a/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs b/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs index 088e4322eb6..a58bd629493 100644 --- a/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs +++ b/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs @@ -22,7 +22,7 @@ public class RestoreSoftDeletedObjectSample /// /// Restores a soft deleted object. /// - /// The name of the GCS bucket. + /// The name of the bucket. /// The name of the soft-deleted object to restore. /// The generation number of the soft-deleted object to restore. public Google.Apis.Storage.v1.Data.Object RestoreSoftDeletedObject(