Skip to content

Commit 1f60484

Browse files
samples(storage): add samples and test cases for bucket soft delete policy (#3061)
1 parent 9b7b2bb commit 1f60484

File tree

7 files changed

+266
-2
lines changed

7 files changed

+266
-2
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
using Google;
16+
using System.Net;
17+
using Xunit;
18+
19+
[Collection(nameof(StorageFixture))]
20+
public class BucketDisableSoftDeletePolicyTest
21+
{
22+
private readonly StorageFixture _fixture;
23+
24+
public BucketDisableSoftDeletePolicyTest(StorageFixture fixture)
25+
{
26+
_fixture = fixture;
27+
}
28+
29+
[Fact]
30+
public void TestBucketDisableSoftDeletePolicy()
31+
{
32+
BucketDisableSoftDeletePolicySample disableSample = new BucketDisableSoftDeletePolicySample();
33+
var bucketName = _fixture.GenerateBucketName();
34+
var bucketWithDefaultSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true);
35+
36+
// Initializing zero with a value 0 indicates the retention duration for the bucket.
37+
long zero = 0;
38+
Assert.NotEqual(bucketWithDefaultSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero);
39+
// Disable soft-delete policy for the bucket.
40+
var bucketPostDisableSoftDeletePolicy = disableSample.BucketDisableSoftDeletePolicy(bucketName);
41+
Assert.Equal(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, zero);
42+
// After disabling soft-delete policy for the bucket, EffectiveTimeRaw property will be null.
43+
Assert.Null(bucketPostDisableSoftDeletePolicy.SoftDeletePolicy.EffectiveTimeRaw);
44+
}
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
using Newtonsoft.Json;
16+
using Xunit;
17+
18+
[Collection(nameof(StorageFixture))]
19+
public class BucketGetSoftDeletePolicyTest
20+
{
21+
private readonly StorageFixture _fixture;
22+
23+
public BucketGetSoftDeletePolicyTest(StorageFixture fixture)
24+
{
25+
_fixture = fixture;
26+
}
27+
28+
[Fact]
29+
public void TestBucketGetSoftDeletePolicy()
30+
{
31+
BucketGetSoftDeletePolicySample getSample = new BucketGetSoftDeletePolicySample();
32+
var bucketName = _fixture.GenerateBucketName();
33+
var bucket = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true);
34+
var softPolicyData = getSample.BucketGetSoftDeletePolicy(bucketName);
35+
var bucketSoftDeletePolicy = JsonConvert.SerializeObject(bucket.SoftDeletePolicy);
36+
var softDeletePolicyData = JsonConvert.SerializeObject(softPolicyData);
37+
Assert.Equal(bucketSoftDeletePolicy, softDeletePolicyData);
38+
}
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
using System;
16+
using Xunit;
17+
18+
[Collection(nameof(StorageFixture))]
19+
public class BucketSetSoftDeletePolicyTest
20+
{
21+
private readonly StorageFixture _fixture;
22+
23+
public BucketSetSoftDeletePolicyTest(StorageFixture fixture)
24+
{
25+
_fixture = fixture;
26+
}
27+
28+
[Fact]
29+
public void TestBucketSetSoftDeletePolicy()
30+
{
31+
BucketSetSoftDeletePolicySample setSample = new BucketSetSoftDeletePolicySample();
32+
var bucketName = _fixture.GenerateBucketName();
33+
var bucketWithDefaultSoftDeletePolicy = _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: true, registerForDeletion: true);
34+
35+
int retentionDurationInDays = 10;
36+
long retentionDurationInSeconds = (long) TimeSpan.FromDays(retentionDurationInDays).TotalSeconds;
37+
38+
Assert.NotEqual(bucketWithDefaultSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds);
39+
40+
// Set soft-delete policy for the bucket with a retention duration of 10 days.
41+
var bucketPostSetSoftDeletePolicy = setSample.BucketSetSoftDeletePolicy(bucketName, retentionDurationInDays);
42+
43+
Assert.Equal(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.RetentionDurationSeconds, retentionDurationInSeconds);
44+
// After setting soft-delete policy for the bucket, EffectiveTimeRaw property will be not null.
45+
Assert.NotNull(bucketPostSetSoftDeletePolicy.SoftDeletePolicy.EffectiveTimeRaw);
46+
}
47+
}

storage/api/Storage.Samples.Tests/StorageFixture.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public StorageFixture()
9292

9393
public void Dispose()
9494
{
95-
DeleteBucketSample deleteBucketSample = new DeleteBucketSample();
9695
DeleteFileSample deleteFileSample = new DeleteFileSample();
9796
DeleteFileArchivedGenerationSample deleteFileArchivedGenerationSample = new DeleteFileArchivedGenerationSample();
9897
foreach (var bucket in TempBucketFiles)
@@ -132,7 +131,7 @@ public void Dispose()
132131
{
133132
try
134133
{
135-
deleteBucketSample.DeleteBucket(bucketName);
134+
Client.DeleteBucket(bucketName, new DeleteBucketOptions { DeleteObjects = true });
136135
SleepAfterBucketCreateUpdateDelete();
137136
}
138137
catch (Exception)
@@ -258,6 +257,12 @@ internal Bucket CreateBucket(string name, bool multiVersion, bool softDelete = f
258257
/// <returns>The objectContent.</returns>
259258
internal string GenerateContent() => Guid.NewGuid().ToString();
260259

260+
/// <summary>
261+
/// Generates a new globally unique identifier (GUID).
262+
/// </summary>
263+
/// <returns>A new randomly generated GUID as string.</returns>
264+
internal string GenerateGuid() => Guid.NewGuid().ToString();
265+
261266
/// <summary>
262267
/// Bucket creation/update/deletion is rate-limited. To avoid making the tests flaky, we sleep after each operation.
263268
/// </summary>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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_disable_soft_delete]
16+
17+
using Google.Apis.Storage.v1.Data;
18+
using Google.Cloud.Storage.V1;
19+
using System;
20+
21+
public class BucketDisableSoftDeletePolicySample
22+
{
23+
/// <summary>
24+
/// Disable soft delete policy for the bucket.
25+
/// </summary>
26+
/// <param name="bucketName">The name of the bucket.</param>
27+
public Bucket BucketDisableSoftDeletePolicy(string bucketName = "your-unique-bucket-name")
28+
{
29+
var storage = StorageClient.Create();
30+
var bucket = storage.GetBucket(bucketName);
31+
// To disable soft-delete policy for the bucket, set the soft delete retention duration to 0 seconds.
32+
bucket.SoftDeletePolicy = new Bucket.SoftDeletePolicyData { RetentionDurationSeconds = 0L };
33+
bucket = storage.UpdateBucket(bucket);
34+
Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is disabled");
35+
return bucket;
36+
}
37+
}
38+
// [END storage_disable_soft_delete]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_get_soft_delete_policy]
16+
17+
using Google.Apis.Storage.v1.Data;
18+
using Google.Cloud.Storage.V1;
19+
using System;
20+
21+
public class BucketGetSoftDeletePolicySample
22+
{
23+
/// <summary>
24+
/// Get soft delete policy of the bucket.
25+
/// </summary>
26+
/// <param name="bucketName">The name of the bucket.</param>
27+
public Bucket.SoftDeletePolicyData BucketGetSoftDeletePolicy(string bucketName = "your-unique-bucket-name")
28+
{
29+
var storage = StorageClient.Create();
30+
var bucket = storage.GetBucket(bucketName);
31+
if (bucket.SoftDeletePolicy.RetentionDurationSeconds == 0)
32+
{
33+
Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is disabled");
34+
}
35+
else
36+
{
37+
int retentionDuration = (int) TimeSpan.FromSeconds((double) bucket.SoftDeletePolicy.RetentionDurationSeconds).TotalDays;
38+
Console.WriteLine($"The Soft Delete Policy for the Bucket (Bucket Name: {bucketName}) is : {retentionDuration} days");
39+
}
40+
return bucket.SoftDeletePolicy;
41+
}
42+
}
43+
// [END storage_get_soft_delete_policy]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

Comments
 (0)