From 761cac923a0bebeb94e1aee285e1bf2dc0637a4c Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Wed, 30 Apr 2025 13:45:30 +0000 Subject: [PATCH 01/19] add samples and test cases for bucket soft delete policy --- .../BucketDisableSoftDeletePolicyTest.cs | 52 ++++++++++++++++ .../BucketGetSoftDeletePolicyTest.cs | 62 +++++++++++++++++++ .../BucketSetSoftDeletePolicyTest.cs | 41 ++++++++++++ .../Storage.Samples.Tests/StorageFixture.cs | 4 ++ .../BucketDisableSoftDeletePolicy.cs | 40 ++++++++++++ .../BucketGetSoftDeletePolicy.cs | 43 +++++++++++++ .../BucketSetSoftDeletePolicy.cs | 48 ++++++++++++++ 7 files changed, 290 insertions(+) create mode 100644 storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs create mode 100644 storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs create mode 100644 storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs create mode 100644 storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs create mode 100644 storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs create mode 100644 storage/api/Storage.Samples/BucketSetSoftDeletePolicy.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..a4a1dabae15 --- /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 = _fixture.GenerateName(); + var originContent = _fixture.GenerateContent(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, originName, originContent); + var objectMetaData = getMetadataSample.GetMetadata(bucketName, originName); + 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. + 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)); + Assert.Equal(HttpStatusCode.BadRequest, exception.HttpStatusCode); + } +} diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs new file mode 100644 index 00000000000..02c4d8e14a3 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -0,0 +1,62 @@ +// 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 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(); + 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 retentionDurationInDays = 10; + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; + Assert.NotEqual(bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + setSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); + var bucketPostSetSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); + Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + int disableSoftDeleteRetentionDurationInDay = 0; + long disableSoftDeleteRetentionDurationInSeconds = (long) TimeSpan.FromDays(disableSoftDeleteRetentionDurationInDay).TotalSeconds; + Assert.NotEqual(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); + 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 new file mode 100644 index 00000000000..a9a1612c8c1 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.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. + +using System; +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(); + var bucketPreSetSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + 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, retentionDurationInDays); + Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + } +} 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 new file mode 100644 index 00000000000..0afcff0a773 --- /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. + /// The retention duration to disable soft-delete policy for the bucket. + public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-bucket-name", + int retentionDurationInDay = 0) + { + var storage = StorageClient.Create(); + var bucket = storage.GetBucket(bucketName); + 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"); + return bucket; + } +} +// [END storage_disable_soft_delete] diff --git a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs new file mode 100644 index 00000000000..3227b98a9ee --- /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. + 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] diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs new file mode 100644 index 00000000000..fb12ca30428 --- /dev/null +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -0,0 +1,48 @@ +// 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. + /// The retention duration in days to set soft-delete policy for the bucket. + public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket-name", + int retentionDurationInDays = 10) + { + var storage = StorageClient.Create(); + var bucket = storage.GetBucket(bucketName); + 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; + } + else + { + bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; + bucket = storage.UpdateBucket(bucket); + } + Console.WriteLine($"Soft Delete Policy for the {bucketName} is set to the {retentionDurationInDays} days retention duration"); + return bucket; + } +} +// [END storage_set_soft_delete_policy] From 4920b0bf95f86268607fd80b0f6fec575174de83 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Tue, 6 May 2025 03:47:34 -0700 Subject: [PATCH 02/19] sample and tests changes --- .../BucketDisableSoftDeletePolicyTest.cs | 8 ++++---- .../BucketGetSoftDeletePolicyTest.cs | 10 +++------- .../Storage.Samples/BucketDisableSoftDeletePolicy.cs | 6 +++--- .../api/Storage.Samples/BucketGetSoftDeletePolicy.cs | 2 +- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs index a4a1dabae15..178e529a214 100644 --- a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs @@ -39,11 +39,11 @@ public void BucketDisableSoftDeletePolicy() var originContent = _fixture.GenerateContent(); uploadObjectFromMemory.UploadObjectFromMemory(bucketName, originName, originContent); var objectMetaData = getMetadataSample.GetMetadata(bucketName, originName); - int retentionDurationInDay = 0; - long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDay).TotalSeconds; + int retentionDurationInDays = 0; + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPreDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0. - var bucketPostDisableSoftDeletePolicy = bucketDisableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, retentionDurationInDay); + // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0 days. + var bucketPostDisableSoftDeletePolicy = bucketDisableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, retentionDurationInDays); 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 02c4d8e14a3..cec750d75cd 100644 --- a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -12,9 +12,7 @@ // 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))] @@ -49,14 +47,12 @@ public void BucketGetSoftDeletePolicy() setSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); var bucketPostSetSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - int disableSoftDeleteRetentionDurationInDay = 0; - long disableSoftDeleteRetentionDurationInSeconds = (long) TimeSpan.FromDays(disableSoftDeleteRetentionDurationInDay).TotalSeconds; + int disableSoftDeleteRetentionDurationInDays = 0; + long disableSoftDeleteRetentionDurationInSeconds = (long) TimeSpan.FromDays(disableSoftDeleteRetentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); - disableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, disableSoftDeleteRetentionDurationInDay); + disableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, disableSoftDeleteRetentionDurationInDays); 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/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs index 0afcff0a773..62e8ec97a43 100644 --- a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs @@ -24,13 +24,13 @@ public class BucketDisableSoftDeletePolicySample /// Disable soft delete policy for the bucket. /// /// The name of the bucket. - /// The retention duration to disable soft-delete policy for the bucket. + /// The retention duration to disable soft-delete policy for the bucket. public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-bucket-name", - int retentionDurationInDay = 0) + int retentionDurationInDays = 0) { var storage = StorageClient.Create(); var bucket = storage.GetBucket(bucketName); - long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDay).TotalSeconds; + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).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 3227b98a9ee..c6a08aa9571 100644 --- a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs @@ -34,7 +34,7 @@ public Bucket BucketGetSoftDeletePolicy(string bucketName = "your-unique-bucket- } else { - int retentionDuration = (int) (bucket.SoftDeletePolicy.RetentionDurationSeconds / (24 * 60 * 60)); + int retentionDuration = (int) TimeSpan.FromSeconds((double) bucket.SoftDeletePolicy.RetentionDurationSeconds).TotalDays; Console.WriteLine($"The Soft Delete Policy for the {bucketName} is : {retentionDuration} days"); } return bucket; From 6339875a32745b1be8065a079a5937547a97b667 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Tue, 6 May 2025 04:15:38 -0700 Subject: [PATCH 03/19] Tests changes --- .../Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs | 1 + .../api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs index 178e529a214..f5fff742d79 100644 --- a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs @@ -45,6 +45,7 @@ public void BucketDisableSoftDeletePolicy() // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0 days. var bucketPostDisableSoftDeletePolicy = bucketDisableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, retentionDurationInDays); Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + Assert.Null(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.EffectiveTimeRaw); _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 a9a1612c8c1..78acdd7add5 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -37,5 +37,6 @@ public void BucketSetSoftDeletePolicy() // Set soft-delete policy for the bucket. var bucketPostSetSoftDeletePolicy = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + Assert.NotNull(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.EffectiveTimeRaw); } } From 554be98c2f78ab6764d879b2d9a246882ae02882 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Wed, 7 May 2025 05:35:00 -0700 Subject: [PATCH 04/19] xml docs added for new helper methods --- storage/api/Storage.Samples.Tests/StorageFixture.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/storage/api/Storage.Samples.Tests/StorageFixture.cs b/storage/api/Storage.Samples.Tests/StorageFixture.cs index 6161e7efe4f..f159b5843c8 100644 --- a/storage/api/Storage.Samples.Tests/StorageFixture.cs +++ b/storage/api/Storage.Samples.Tests/StorageFixture.cs @@ -239,8 +239,16 @@ internal Bucket CreateBucket(string name, bool multiVersion, bool softDelete = f internal string GenerateBucketName() => Guid.NewGuid().ToString(); + /// + /// Generate the name of the object. + /// + /// The objectName. internal string GenerateName() => Guid.NewGuid().ToString(); + /// + /// Generate the content of the object. + /// + /// The objectContent. internal string GenerateContent() => Guid.NewGuid().ToString(); /// From 5e1d4b6a01960b0f4549e12e5fafc8518c52a66b Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Wed, 7 May 2025 05:58:52 -0700 Subject: [PATCH 05/19] license header minor changes --- .../Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs | 2 +- .../api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs | 2 +- .../api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs | 2 +- storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs | 2 +- storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs | 2 +- storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs index f5fff742d79..cdd0a9f65d8 100644 --- a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs @@ -1,4 +1,4 @@ -// Copyright 2025 Google Inc. +// 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. diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs index cec750d75cd..feec9e37a5c 100644 --- a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -1,4 +1,4 @@ -// Copyright 2025 Google Inc. +// 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. diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs index 78acdd7add5..595ced7e3ba 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -1,4 +1,4 @@ -// Copyright 2025 Google Inc. +// 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. diff --git a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs index 62e8ec97a43..5011bbda470 100644 --- a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs @@ -1,4 +1,4 @@ -// Copyright 2025 Google Inc. +// 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. diff --git a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs index c6a08aa9571..98155ea9911 100644 --- a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs @@ -1,4 +1,4 @@ -// Copyright 2025 Google Inc. +// 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. diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs index fb12ca30428..b9d59c1fe0f 100644 --- a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -1,4 +1,4 @@ -// Copyright 2025 Google Inc. +// 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. From 7fc30d57e698119b91b182a118ebc3ed82eb9f56 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Wed, 7 May 2025 06:58:46 -0700 Subject: [PATCH 06/19] comments added --- .../api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs | 2 ++ .../api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs index feec9e37a5c..c40f8a0242e 100644 --- a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -44,12 +44,14 @@ public void BucketGetSoftDeletePolicy() int retentionDurationInDays = 10; long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + // Set soft-delete policy for the bucket with 10 days retention duration period. setSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); var bucketPostSetSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); int disableSoftDeleteRetentionDurationInDays = 0; long disableSoftDeleteRetentionDurationInSeconds = (long) TimeSpan.FromDays(disableSoftDeleteRetentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); + // Disable soft-delete policy for the bucket with 0 days retention duration period. disableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, disableSoftDeleteRetentionDurationInDays); var bucketPostDisableSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs index 595ced7e3ba..cce718a268a 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -34,7 +34,7 @@ public void BucketSetSoftDeletePolicy() int retentionDurationInDays = 10; long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPreSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - // Set soft-delete policy for the bucket. + // Set soft-delete policy for the bucket with 10 days retention duration period. var bucketPostSetSoftDeletePolicy = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); Assert.NotNull(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.EffectiveTimeRaw); From 2cc34467799f31cbabf86c0d13d37226ec076636 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Thu, 8 May 2025 04:22:22 -0700 Subject: [PATCH 07/19] rewording of console output print messages --- storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs | 2 +- storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs | 4 ++-- storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs index 5011bbda470..8f1d21bc09e 100644 --- a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs @@ -33,7 +33,7 @@ public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-buc long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; bucket = storage.UpdateBucket(bucket); - Console.WriteLine($"Soft Delete Policy for the {bucketName} is disabled"); + Console.WriteLine($"Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is disabled"); return bucket; } } diff --git a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs index 98155ea9911..affbae33180 100644 --- a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs @@ -30,12 +30,12 @@ public Bucket BucketGetSoftDeletePolicy(string bucketName = "your-unique-bucket- var bucket = storage.GetBucket(bucketName); if (bucket.SoftDeletePolicy.RetentionDurationSeconds == 0) { - Console.WriteLine($"The Soft Delete Policy is disabled for the {bucketName}"); + Console.WriteLine($"The Soft Delete Policy is disabled for the Bucket (Bucket Name: {bucketName})"); } else { int retentionDuration = (int) TimeSpan.FromSeconds((double) bucket.SoftDeletePolicy.RetentionDurationSeconds).TotalDays; - Console.WriteLine($"The Soft Delete Policy for the {bucketName} is : {retentionDuration} days"); + Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is : {retentionDuration} days"); } return bucket; } diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs index b9d59c1fe0f..e1e318236e8 100644 --- a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -33,7 +33,7 @@ public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket- 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"); + Console.WriteLine($"Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) must have a retention duration between 7 days and 90 days"); return bucket; } else @@ -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 {retentionDurationInDays} days retention duration"); + Console.WriteLine($"Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is set to the {retentionDurationInDays} days retention duration"); return bucket; } } From c4f270295029cb0b6b7ffaf5b4023fa75a0a33ec Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Thu, 8 May 2025 04:33:42 -0700 Subject: [PATCH 08/19] minor changes --- storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs | 2 +- storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs index 8f1d21bc09e..e8083e98652 100644 --- a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs @@ -33,7 +33,7 @@ public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-buc long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; bucket = storage.UpdateBucket(bucket); - Console.WriteLine($"Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is disabled"); + Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is disabled"); return bucket; } } diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs index e1e318236e8..cbcf06b7e41 100644 --- a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -33,7 +33,7 @@ public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket- long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; if (retentionDurationInDays < 7 || retentionDurationInDays > 90) { - Console.WriteLine($"Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) must have a retention duration between 7 days and 90 days"); + Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) must have a retention duration between 7 days and 90 days"); return bucket; } else @@ -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 Bucket (Bucket Name: {bucketName}) is set to the {retentionDurationInDays} days retention duration"); + Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is set to the {retentionDurationInDays} days retention duration"); return bucket; } } From ebcf6243e8adfdfdf08c50ebdb5d75bc48b83740 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Thu, 8 May 2025 05:02:33 -0700 Subject: [PATCH 09/19] removal of unused variable and rewording --- .../Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs | 6 +++--- .../Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs | 2 +- storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs index c40f8a0242e..0c117a34a11 100644 --- a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -38,20 +38,20 @@ public void BucketGetSoftDeletePolicy() var originName = _fixture.GenerateName(); var originContent = _fixture.GenerateContent(); uploadObjectFromMemory.UploadObjectFromMemory(bucketName, originName, originContent); - var objectMetaData = getMetadataSample.GetMetadata(bucketName, originName); + getMetadataSample.GetMetadata(bucketName, originName); var bucketPostFetchSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPreFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds); int retentionDurationInDays = 10; long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - // Set soft-delete policy for the bucket with 10 days retention duration period. + // Set soft-delete policy for the bucket with a retention duration of 10 days. setSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); var bucketPostSetSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); int disableSoftDeleteRetentionDurationInDays = 0; long disableSoftDeleteRetentionDurationInSeconds = (long) TimeSpan.FromDays(disableSoftDeleteRetentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); - // Disable soft-delete policy for the bucket with 0 days retention duration period. + // Disable soft-delete policy for the bucket by setting the retention duration to 0 days. disableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, disableSoftDeleteRetentionDurationInDays); var bucketPostDisableSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs index cce718a268a..933a6818772 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -34,7 +34,7 @@ public void BucketSetSoftDeletePolicy() int retentionDurationInDays = 10; long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; Assert.NotEqual(bucketPreSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - // Set soft-delete policy for the bucket with 10 days retention duration period. + // Set soft-delete policy for the bucket with a retention duration of 10 days. var bucketPostSetSoftDeletePolicy = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); Assert.NotNull(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.EffectiveTimeRaw); diff --git a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs index affbae33180..221a586d5ed 100644 --- a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs @@ -30,7 +30,7 @@ public Bucket BucketGetSoftDeletePolicy(string bucketName = "your-unique-bucket- var bucket = storage.GetBucket(bucketName); if (bucket.SoftDeletePolicy.RetentionDurationSeconds == 0) { - Console.WriteLine($"The Soft Delete Policy is disabled for the Bucket (Bucket Name: {bucketName})"); + Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is disabled"); } else { From 71d79485a5e1299c6da54d92bcacdbcf0ce704e7 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Thu, 8 May 2025 06:00:39 -0700 Subject: [PATCH 10/19] removal of unused code --- .../Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs index 0c117a34a11..1f46bc1b52f 100644 --- a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -31,14 +31,8 @@ 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); - getMetadataSample.GetMetadata(bucketName, originName); var bucketPostFetchSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPreFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds); int retentionDurationInDays = 10; @@ -55,6 +49,5 @@ public void BucketGetSoftDeletePolicy() disableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, disableSoftDeleteRetentionDurationInDays); var bucketPostDisableSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); - _fixture.Client.DeleteObject(bucketName, originName); } } From d30994c5f72ab2fc7d865fb4a8e6aee6c0d7d220 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Thu, 8 May 2025 23:30:04 -0700 Subject: [PATCH 11/19] changes in parameter name description in bucketdisable soft delete policy --- storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs index e8083e98652..122183152c3 100644 --- a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs @@ -24,7 +24,7 @@ public class BucketDisableSoftDeletePolicySample /// Disable soft delete policy for the bucket. /// /// The name of the bucket. - /// The retention duration to disable soft-delete policy for the bucket. + /// The retention duration in days to disable soft-delete policy for the bucket. public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-bucket-name", int retentionDurationInDays = 0) { From b0a7f9e368c3f58817ce5106ea2dd0c86fc343f6 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Thu, 5 Jun 2025 05:03:24 -0700 Subject: [PATCH 12/19] Code review changes --- .../BucketDisableSoftDeletePolicyTest.cs | 34 +++++++++++-------- .../BucketGetSoftDeletePolicyTest.cs | 34 ++++++++++--------- .../BucketSetSoftDeletePolicyTest.cs | 15 +++++--- .../Storage.Samples.Tests/StorageFixture.cs | 9 +++-- .../BucketSetSoftDeletePolicy.cs | 2 ++ 5 files changed, 57 insertions(+), 37 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs index cdd0a9f65d8..e7ef1e699e9 100644 --- a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs @@ -13,7 +13,6 @@ // limitations under the License. using Google; -using System; using System.Net; using Xunit; @@ -28,25 +27,32 @@ public BucketDisableSoftDeletePolicyTest(StorageFixture fixture) } [Fact] - public void BucketDisableSoftDeletePolicy() + public void TestBucketDisableSoftDeletePolicy() { - BucketDisableSoftDeletePolicySample bucketDisableSoftDeletePolicy = new BucketDisableSoftDeletePolicySample(); - UploadObjectFromMemorySample uploadObjectFromMemory = new UploadObjectFromMemorySample(); + BucketDisableSoftDeletePolicySample disableSample = new BucketDisableSoftDeletePolicySample(); + UploadObjectFromMemorySample uploadObjectSample = new UploadObjectFromMemorySample(); GetMetadataSample getMetadataSample = new GetMetadataSample(); var bucketName = _fixture.GenerateBucketName(); - var bucketPreDisableSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); - var originName = _fixture.GenerateName(); - var originContent = _fixture.GenerateContent(); - uploadObjectFromMemory.UploadObjectFromMemory(bucketName, originName, originContent); + var bucketWithDefaultSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + + var originName = _fixture.GenerateGuid(); + var originContent = _fixture.GenerateGuid(); + + uploadObjectSample.UploadObjectFromMemory(bucketName, originName, originContent); var objectMetaData = getMetadataSample.GetMetadata(bucketName, originName); - int retentionDurationInDays = 0; - long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; - Assert.NotEqual(bucketPreDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + + // Initializing zero with value 0 indicates that the retention duration for the bucket. + long zero = 0; + Assert.NotEqual(bucketWithDefaultSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero); + // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0 days. - var bucketPostDisableSoftDeletePolicy = bucketDisableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, retentionDurationInDays); - Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + var bucketPostDisableSoftDeletePolicy = disableSample.BucketDisableSoftDeletePolicy(bucketName, (int) zero); + Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero); + + // After disabling soft-delete policy for the bucket, EffectiveTimeRaw property will be null. Assert.Null(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.EffectiveTimeRaw); - _fixture.Client.DeleteObject(bucketName, originName); + + // After disabling soft-delete policy for the bucket, objects in the bucket can not be restored. 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/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs index 1f46bc1b52f..4c36f2496fe 100644 --- a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -26,28 +26,30 @@ public BucketGetSoftDeletePolicyTest(StorageFixture fixture) } [Fact] - public void BucketGetSoftDeletePolicy() + public void TestBucketGetSoftDeletePolicy() { - BucketGetSoftDeletePolicySample getBucketSoftDeletePolicy = new BucketGetSoftDeletePolicySample(); - BucketSetSoftDeletePolicySample setSoftDeletePolicy = new BucketSetSoftDeletePolicySample(); - BucketDisableSoftDeletePolicySample disableSoftDeletePolicy = new BucketDisableSoftDeletePolicySample(); + BucketGetSoftDeletePolicySample getSample = new BucketGetSoftDeletePolicySample(); + BucketSetSoftDeletePolicySample setSample = new BucketSetSoftDeletePolicySample(); + BucketDisableSoftDeletePolicySample disableSample = 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); + var bucketWithDefaultSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var retrievedBucket = getSample.BucketGetSoftDeletePolicy(bucketName); + Assert.Equal(bucketWithDefaultSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retrievedBucket.SoftDeletePolicy.RetentionDurationSeconds); + int retentionDurationInDays = 10; long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; - Assert.NotEqual(bucketPostFetchSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + // Set soft-delete policy for the bucket with a retention duration of 10 days. - setSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); - var bucketPostSetSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); + setSample.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); + var bucketPostSetSoftDeletePolicy = getSample.BucketGetSoftDeletePolicy(bucketName); Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - int disableSoftDeleteRetentionDurationInDays = 0; - long disableSoftDeleteRetentionDurationInSeconds = (long) TimeSpan.FromDays(disableSoftDeleteRetentionDurationInDays).TotalSeconds; - Assert.NotEqual(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); + + // Initializing zero with value 0 indicates that the retention duration for the bucket. + long zero = 0; // Disable soft-delete policy for the bucket by setting the retention duration to 0 days. - disableSoftDeletePolicy.BucketDisableSoftDeletePolicy(bucketName, disableSoftDeleteRetentionDurationInDays); - var bucketPostDisableSoftDeletePolicy = getBucketSoftDeletePolicy.BucketGetSoftDeletePolicy(bucketName); - Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, disableSoftDeleteRetentionDurationInSeconds); + disableSample.BucketDisableSoftDeletePolicy(bucketName, (int) zero); + var bucketPostDisableSoftDeletePolicy = getSample.BucketGetSoftDeletePolicy(bucketName); + Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero); } } diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs index 933a6818772..8288daf613a 100644 --- a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs @@ -26,17 +26,22 @@ public BucketSetSoftDeletePolicyTest(StorageFixture fixture) } [Fact] - public void BucketSetSoftDeletePolicy() + public void TestBucketSetSoftDeletePolicy() { - BucketSetSoftDeletePolicySample bucketSetSoftDeletePolicy = new BucketSetSoftDeletePolicySample(); + BucketSetSoftDeletePolicySample setSample = new BucketSetSoftDeletePolicySample(); var bucketName = _fixture.GenerateBucketName(); - var bucketPreSetSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var bucketWithDefaultSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + int retentionDurationInDays = 10; long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; - Assert.NotEqual(bucketPreSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + + Assert.NotEqual(bucketWithDefaultSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + // Set soft-delete policy for the bucket with a retention duration of 10 days. - var bucketPostSetSoftDeletePolicy = bucketSetSoftDeletePolicy.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); + var bucketPostSetSoftDeletePolicy = setSample.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); + Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + // After setting soft-delete policy for the bucket, EffectiveTimeRaw property will be not null. Assert.NotNull(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.EffectiveTimeRaw); } } diff --git a/storage/api/Storage.Samples.Tests/StorageFixture.cs b/storage/api/Storage.Samples.Tests/StorageFixture.cs index f159b5843c8..22eb8786db2 100644 --- a/storage/api/Storage.Samples.Tests/StorageFixture.cs +++ b/storage/api/Storage.Samples.Tests/StorageFixture.cs @@ -92,7 +92,6 @@ public StorageFixture() public void Dispose() { - DeleteBucketSample deleteBucketSample = new DeleteBucketSample(); DeleteFileSample deleteFileSample = new DeleteFileSample(); DeleteFileArchivedGenerationSample deleteFileArchivedGenerationSample = new DeleteFileArchivedGenerationSample(); foreach (var bucket in TempBucketFiles) @@ -132,7 +131,7 @@ public void Dispose() { try { - deleteBucketSample.DeleteBucket(bucketName); + Client.DeleteBucket(bucketName, new DeleteBucketOptions { DeleteObjects = true }); SleepAfterBucketCreateUpdateDelete(); } catch (Exception) @@ -251,6 +250,12 @@ internal Bucket CreateBucket(string name, bool multiVersion, bool softDelete = f /// The objectContent. internal string GenerateContent() => Guid.NewGuid().ToString(); + /// + /// Generates a new globally unique identifier (GUID). + /// + /// A new randomly generated GUID as string. + internal string GenerateGuid() => 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/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs index cbcf06b7e41..9ec23f8bd06 100644 --- a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -33,6 +33,8 @@ public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket- long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; if (retentionDurationInDays < 7 || retentionDurationInDays > 90) { + // The maximum retention duration you can set is 90 days and the minimum retention duration you can set is 7 days. + // For more information, please refer to https://cloud.google.com/storage/docs/soft-delete#retention-duration Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) must have a retention duration between 7 days and 90 days"); return bucket; } From 549915138df0af79e7a4f439ba0d381ef6a7f83c Mon Sep 17 00:00:00 2001 From: Mahendra Date: Fri, 6 Jun 2025 12:12:50 +0530 Subject: [PATCH 13/19] Update storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs Co-authored-by: Charlotte Y <38296042+cy-yun@users.noreply.github.com> --- .../BucketGetSoftDeletePolicyTest.cs | 24 +++---------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs index 4c36f2496fe..2928b4fdef4 100644 --- a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -29,27 +29,9 @@ public BucketGetSoftDeletePolicyTest(StorageFixture fixture) public void TestBucketGetSoftDeletePolicy() { BucketGetSoftDeletePolicySample getSample = new BucketGetSoftDeletePolicySample(); - BucketSetSoftDeletePolicySample setSample = new BucketSetSoftDeletePolicySample(); - BucketDisableSoftDeletePolicySample disableSample = new BucketDisableSoftDeletePolicySample(); - var bucketName = _fixture.GenerateBucketName(); - var bucketWithDefaultSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); - var retrievedBucket = getSample.BucketGetSoftDeletePolicy(bucketName); - Assert.Equal(bucketWithDefaultSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retrievedBucket.SoftDeletePolicy.RetentionDurationSeconds); - - int retentionDurationInDays = 10; - long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; - - // Set soft-delete policy for the bucket with a retention duration of 10 days. - setSample.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays); - var bucketPostSetSoftDeletePolicy = getSample.BucketGetSoftDeletePolicy(bucketName); - Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); - - // Initializing zero with value 0 indicates that the retention duration for the bucket. - long zero = 0; - // Disable soft-delete policy for the bucket by setting the retention duration to 0 days. - disableSample.BucketDisableSoftDeletePolicy(bucketName, (int) zero); - var bucketPostDisableSoftDeletePolicy = getSample.BucketGetSoftDeletePolicy(bucketName); - Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero); + var bucket = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var softPolicyData = getSample.BucketGetSoftDeletePolicy(bucketName); + Assert.Equal(bucket.SoftPolicy, softPolicyData); } } From b0e0b69dde9397f1c25fce7edd35248d18d9308a Mon Sep 17 00:00:00 2001 From: Mahendra Date: Fri, 6 Jun 2025 12:13:18 +0530 Subject: [PATCH 14/19] Update storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs Co-authored-by: Charlotte Y <38296042+cy-yun@users.noreply.github.com> --- storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs index 221a586d5ed..ed0ed0d941c 100644 --- a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs @@ -37,7 +37,7 @@ public Bucket BucketGetSoftDeletePolicy(string bucketName = "your-unique-bucket- int retentionDuration = (int) TimeSpan.FromSeconds((double) bucket.SoftDeletePolicy.RetentionDurationSeconds).TotalDays; Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is : {retentionDuration} days"); } - return bucket; + return bucket.SoftDeletePolicy; } } // [END storage_get_soft_delete_policy] From 09fe7d9f45071520359c01171773f9c2de86cf24 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Fri, 6 Jun 2025 02:40:42 -0700 Subject: [PATCH 15/19] BucketGetSoftDeletePolicy sample and test changes --- .../Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs | 6 ++++-- storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs index 2928b4fdef4..d86819f9d41 100644 --- a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; +using Newtonsoft.Json; using Xunit; [Collection(nameof(StorageFixture))] @@ -32,6 +32,8 @@ public void TestBucketGetSoftDeletePolicy() var bucketName = _fixture.GenerateBucketName(); var bucket = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); var softPolicyData = getSample.BucketGetSoftDeletePolicy(bucketName); - Assert.Equal(bucket.SoftPolicy, softPolicyData); + var bucketSoftDeletePolicy = JsonConvert.SerializeObject(bucket.SoftDeletePolicy); + var softDeletePolicyData = JsonConvert.SerializeObject(softPolicyData); + Assert.Equal(bucketSoftDeletePolicy, softDeletePolicyData); } } diff --git a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs index ed0ed0d941c..cfa1f056286 100644 --- a/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs @@ -24,7 +24,7 @@ public class BucketGetSoftDeletePolicySample /// Get soft delete policy of the bucket. /// /// The name of the bucket. - public Bucket BucketGetSoftDeletePolicy(string bucketName = "your-unique-bucket-name") + public Bucket.SoftDeletePolicyData BucketGetSoftDeletePolicy(string bucketName = "your-unique-bucket-name") { var storage = StorageClient.Create(); var bucket = storage.GetBucket(bucketName); From 3773baca073f0d5dfcb132ead60d6dcdee371960 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Fri, 6 Jun 2025 03:20:47 -0700 Subject: [PATCH 16/19] BucketDisableSoftDeletePolicy smaple and test changes --- .../BucketDisableSoftDeletePolicyTest.cs | 6 +++--- .../api/Storage.Samples/BucketDisableSoftDeletePolicy.cs | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs index e7ef1e699e9..d0dbbed3040 100644 --- a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs @@ -41,12 +41,12 @@ public void TestBucketDisableSoftDeletePolicy() uploadObjectSample.UploadObjectFromMemory(bucketName, originName, originContent); var objectMetaData = getMetadataSample.GetMetadata(bucketName, originName); - // Initializing zero with value 0 indicates that the retention duration for the bucket. + // Initializing zero with a value 0 indicates the retention duration for the bucket. long zero = 0; Assert.NotEqual(bucketWithDefaultSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero); - // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0 days. - var bucketPostDisableSoftDeletePolicy = disableSample.BucketDisableSoftDeletePolicy(bucketName, (int) zero); + // Disable soft-delete policy for the bucket. + var bucketPostDisableSoftDeletePolicy = disableSample.BucketDisableSoftDeletePolicy(bucketName); Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero); // After disabling soft-delete policy for the bucket, EffectiveTimeRaw property will be null. diff --git a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs index 122183152c3..00cedb0edb2 100644 --- a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs @@ -24,13 +24,12 @@ public class BucketDisableSoftDeletePolicySample /// Disable soft delete policy for the bucket. /// /// The name of the bucket. - /// The retention duration in days to disable soft-delete policy for the bucket. - public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-bucket-name", - int retentionDurationInDays = 0) + public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-bucket-name") { var storage = StorageClient.Create(); var bucket = storage.GetBucket(bucketName); - long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; + // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0 seconds. + long retentionDurationInSeconds = 0; bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; bucket = storage.UpdateBucket(bucket); Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is disabled"); From 2aa53a68a9f2e864dbf7ea004fcce7ee5a171343 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Wed, 11 Jun 2025 12:26:50 +0530 Subject: [PATCH 17/19] Update storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs Co-authored-by: Charlotte Y <38296042+cy-yun@users.noreply.github.com> --- storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs index 9ec23f8bd06..66266b6c184 100644 --- a/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.cs @@ -38,11 +38,8 @@ public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket- Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {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); - } + bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; + bucket = storage.UpdateBucket(bucket); Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is set to the {retentionDurationInDays} days retention duration"); return bucket; } From 12fc5165f5bebb3b01bedb48b2a0be7af1f5644e Mon Sep 17 00:00:00 2001 From: Mahendra Date: Wed, 11 Jun 2025 12:27:09 +0530 Subject: [PATCH 18/19] Update storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs Co-authored-by: Charlotte Y <38296042+cy-yun@users.noreply.github.com> --- storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs index 00cedb0edb2..f14af1777a1 100644 --- a/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs @@ -29,8 +29,7 @@ public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-buc var storage = StorageClient.Create(); var bucket = storage.GetBucket(bucketName); // To disable soft-delete policy for the bucket, set the soft delete retention duration to 0 seconds. - long retentionDurationInSeconds = 0; - bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; + bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = 0L }; bucket = storage.UpdateBucket(bucket); Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is disabled"); return bucket; From 12067f3e01a7e523b35b706a70f6f7e7194084ab Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Tue, 24 Jun 2025 04:26:06 -0700 Subject: [PATCH 19/19] disable soft delete policy test changes aligning with canonical sample and other languages --- .../BucketDisableSoftDeletePolicyTest.cs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs index d0dbbed3040..fa360976511 100644 --- a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs +++ b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs @@ -30,30 +30,16 @@ public BucketDisableSoftDeletePolicyTest(StorageFixture fixture) public void TestBucketDisableSoftDeletePolicy() { BucketDisableSoftDeletePolicySample disableSample = new BucketDisableSoftDeletePolicySample(); - UploadObjectFromMemorySample uploadObjectSample = new UploadObjectFromMemorySample(); - GetMetadataSample getMetadataSample = new GetMetadataSample(); var bucketName = _fixture.GenerateBucketName(); var bucketWithDefaultSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); - var originName = _fixture.GenerateGuid(); - var originContent = _fixture.GenerateGuid(); - - uploadObjectSample.UploadObjectFromMemory(bucketName, originName, originContent); - var objectMetaData = getMetadataSample.GetMetadata(bucketName, originName); - // Initializing zero with a value 0 indicates the retention duration for the bucket. long zero = 0; Assert.NotEqual(bucketWithDefaultSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero); - // Disable soft-delete policy for the bucket. var bucketPostDisableSoftDeletePolicy = disableSample.BucketDisableSoftDeletePolicy(bucketName); Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero); - // After disabling soft-delete policy for the bucket, EffectiveTimeRaw property will be null. Assert.Null(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.EffectiveTimeRaw); - - // After disabling soft-delete policy for the bucket, objects in the bucket can not be restored. - var exception = Assert.Throws(() => _fixture.Client.RestoreObject(bucketName, originName, objectMetaData.Generation.Value)); - Assert.Equal(HttpStatusCode.BadRequest, exception.HttpStatusCode); } }