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/ListSoftDeletedObjectsTest.cs b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs new file mode 100644 index 00000000000..4b37bd64e81 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs @@ -0,0 +1,49 @@ +// 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 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 == 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 new file mode 100644 index 00000000000..b68dcc9fcc2 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/ListSoftDeletedVersionsOfObjectTest.cs @@ -0,0 +1,58 @@ +// 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.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 = _fixture.GenerateName(); + var objectContent = _fixture.GenerateContent(); + uploadObjectFromMemory.UploadObjectFromMemory(bucketName, objectName, objectContent); + 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.Tests/RestoreSoftDeletedObjectTest.cs b/storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs new file mode 100644 index 00000000000..2635061f977 --- /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 = _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); + Assert.Equal(objectName, restoredObject.Name); + _fixture.Client.DeleteObject(bucketName, objectName); + } +} 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] diff --git a/storage/api/Storage.Samples/ListSoftDeletedObjects.cs b/storage/api/Storage.Samples/ListSoftDeletedObjects.cs new file mode 100644 index 00000000000..8e3e6b9b133 --- /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. + 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($"Name: {obj.Name}"); + } + return objects; + } +} +// [END storage_list_soft_deleted_objects] diff --git a/storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs b/storage/api/Storage.Samples/ListSoftDeletedVersionsOfObject.cs new file mode 100644 index 00000000000..4dd25f8ab93 --- /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. + /// 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 and Versions 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] diff --git a/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs b/storage/api/Storage.Samples/RestoreSoftDeletedObject.cs new file mode 100644 index 00000000000..a58bd629493 --- /dev/null +++ b/storage/api/Storage.Samples/RestoreSoftDeletedObject.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 +// +// 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 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] 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 @@ - +