Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a5561c8
Sample and test case to list soft deleted objects
mahendra-google Apr 18, 2025
c529a98
ListSoftDeletedObjects test changes
mahendra-google Apr 21, 2025
01639cf
add sample and test case to restore soft deleted object
mahendra-google Apr 21, 2025
d92cbbb
Update ListSoftDeletedObjectsTest.cs
mahendra-google Apr 21, 2025
65dff8d
list soft deleted object test update
mahendra-google Apr 22, 2025
a7eac83
restore soft delete sample changes
mahendra-google Apr 22, 2025
8adda4b
add sample and test case to list soft deleted versions of the object
mahendra-google Apr 22, 2025
c6edeb9
add sample and test case to set soft delete policy for the bucket
mahendra-google Apr 23, 2025
ba02d02
sample and test case update to set soft delete policy for bucket
mahendra-google Apr 23, 2025
1812bf2
sample and test soft delete policy changes
mahendra-google Apr 24, 2025
b0024fb
data type for retention duration and sample description changes
mahendra-google Apr 24, 2025
01e5d9a
bucket set soft delete policy assert added
mahendra-google Apr 25, 2025
4d6b96b
add sample and test case to disable bucket soft delete policy
mahendra-google Apr 25, 2025
c7c30e7
One blank space added between using and class
mahendra-google Apr 27, 2025
f199635
Revert "One blank space added between using and class"
mahendra-google Apr 28, 2025
f3d153c
add sample and test case to get bucket soft delete policy
mahendra-google Apr 28, 2025
cd98238
chore(deps): update dependency google.cloud.storage.v1 to 4.13.0
mahendra-google Apr 28, 2025
d9c4c9b
add space between using namespaces and start of class
mahendra-google Apr 28, 2025
3fee8c0
Recommended changes as a part of code review
mahendra-google Apr 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<GoogleApiException>(() => _fixture.Client.RestoreObject(bucketName, originName, objectMetaData.Generation.Value));
Assert.Equal(HttpStatusCode.BadRequest, exception.HttpStatusCode);
}
}
62 changes: 62 additions & 0 deletions storage/api/Storage.Samples.Tests/BucketGetSoftDeletePolicyTest.cs
Original file line number Diff line number Diff line change
@@ -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<GoogleApiException>(() => _fixture.Client.RestoreObject(bucketName, originName, objectMetaData.Generation.Value));
Assert.Equal(HttpStatusCode.BadRequest, exception.HttpStatusCode);
}
}
41 changes: 41 additions & 0 deletions storage/api/Storage.Samples.Tests/BucketSetSoftDeletePolicyTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
49 changes: 49 additions & 0 deletions storage/api/Storage.Samples.Tests/ListSoftDeletedObjectsTest.cs
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
Original file line number Diff line number Diff line change
@@ -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<long> _softDeleteObjectGenerations { get; } = new List<long>();

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);
}
}
45 changes: 45 additions & 0 deletions storage/api/Storage.Samples.Tests/RestoreSoftDeletedObjectTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 4 additions & 0 deletions storage/api/Storage.Samples.Tests/StorageFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/// <summary>
/// Bucket creation/update/deletion is rate-limited. To avoid making the tests flaky, we sleep after each operation.
/// </summary>
Expand Down
40 changes: 40 additions & 0 deletions storage/api/Storage.Samples/BucketDisableSoftDeletePolicy.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Disable soft delete policy for the bucket.
/// </summary>
/// <param name="bucketName">The name of the bucket.</param>
/// <param name="retentionDurationInDay">The retention duration to disable soft-delete policy for the bucket.</param>
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]
Loading