diff --git a/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.cs new file mode 100644 index 00000000000..fa360976511 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/BucketDisableSoftDeletePolicyTest.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 +// +// 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.Net; +using Xunit; + +[Collection(nameof(StorageFixture))] +public class BucketDisableSoftDeletePolicyTest +{ + private readonly StorageFixture _fixture; + + public BucketDisableSoftDeletePolicyTest(StorageFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public void TestBucketDisableSoftDeletePolicy() + { + BucketDisableSoftDeletePolicySample disableSample = new BucketDisableSoftDeletePolicySample(); + var bucketName = _fixture.GenerateBucketName(); + var bucketWithDefaultSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + + // 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); + } +} diff --git a/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs new file mode 100644 index 00000000000..d86819f9d41 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs @@ -0,0 +1,39 @@ +// 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 +// +// 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 Newtonsoft.Json; +using Xunit; + +[Collection(nameof(StorageFixture))] +public class BucketGetSoftDeletePolicyTest +{ + private readonly StorageFixture _fixture; + + public BucketGetSoftDeletePolicyTest(StorageFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public void TestBucketGetSoftDeletePolicy() + { + BucketGetSoftDeletePolicySample getSample = new BucketGetSoftDeletePolicySample(); + var bucketName = _fixture.GenerateBucketName(); + var bucket = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + var softPolicyData = getSample.BucketGetSoftDeletePolicy(bucketName); + var bucketSoftDeletePolicy = JsonConvert.SerializeObject(bucket.SoftDeletePolicy); + var softDeletePolicyData = JsonConvert.SerializeObject(softPolicyData); + Assert.Equal(bucketSoftDeletePolicy, softDeletePolicyData); + } +} diff --git a/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs new file mode 100644 index 00000000000..8288daf613a --- /dev/null +++ b/storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.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 +// +// 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 TestBucketSetSoftDeletePolicy() + { + BucketSetSoftDeletePolicySample setSample = new BucketSetSoftDeletePolicySample(); + var bucketName = _fixture.GenerateBucketName(); + var bucketWithDefaultSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true); + + int retentionDurationInDays = 10; + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; + + Assert.NotEqual(bucketWithDefaultSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds); + + // Set soft-delete policy for the bucket with a retention duration of 10 days. + 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 b5b8b764195..b3db2424408 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) @@ -258,6 +257,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/BucketDisableSoftDeletePolicy.cs b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs new file mode 100644 index 00000000000..f14af1777a1 --- /dev/null +++ b/storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.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 +// +// 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. + public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-bucket-name") + { + 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. + 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; + } +} +// [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..cfa1f056286 --- /dev/null +++ b/storage/api/Storage.Samples/BucketGetSoftDeletePolicy.cs @@ -0,0 +1,43 @@ +// 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 +// +// 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.SoftDeletePolicyData 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 for the Bucket (Bucket Name: {bucketName}) is disabled"); + } + else + { + 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.SoftDeletePolicy; + } +} +// [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..66266b6c184 --- /dev/null +++ b/storage/api/Storage.Samples/BucketSetSoftDeletePolicy.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 +// +// 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) + { + // 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; + } + 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; + } +} +// [END storage_set_soft_delete_policy]