|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// [START storage_set_soft_delete_policy] |
| 16 | + |
| 17 | +using Google.Apis.Storage.v1.Data; |
| 18 | +using Google.Cloud.Storage.V1; |
| 19 | +using System; |
| 20 | + |
| 21 | +public class BucketSetSoftDeletePolicySample |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Set soft delete policy for the bucket. |
| 25 | + /// </summary> |
| 26 | + /// <param name="bucketName">The name of the bucket.</param> |
| 27 | + /// <param name="retentionDurationInDays">The retention duration in days to set soft-delete policy for the bucket.</param> |
| 28 | + public Bucket BucketSetSoftDeletePolicy(string bucketName = "your-unique-bucket-name", |
| 29 | + int retentionDurationInDays = 10) |
| 30 | + { |
| 31 | + var storage = StorageClient.Create(); |
| 32 | + var bucket = storage.GetBucket(bucketName); |
| 33 | + long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds; |
| 34 | + if (retentionDurationInDays < 7 || retentionDurationInDays > 90) |
| 35 | + { |
| 36 | + // The maximum retention duration you can set is 90 days and the minimum retention duration you can set is 7 days. |
| 37 | + // For more information, please refer to https://cloud.google.com/storage/docs/soft-delete#retention-duration |
| 38 | + Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) must have a retention duration between 7 days and 90 days"); |
| 39 | + return bucket; |
| 40 | + } |
| 41 | + bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = retentionDurationInSeconds }; |
| 42 | + bucket = storage.UpdateBucket(bucket); |
| 43 | + Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is set to the {retentionDurationInDays} days retention duration"); |
| 44 | + return bucket; |
| 45 | + } |
| 46 | +} |
| 47 | +// [END storage_set_soft_delete_policy] |
0 commit comments