Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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,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.Linq;
using Xunit;

[Collection(nameof(StorageFixture))]
public class ListBucketsWithPartialSuccessTest
{
private readonly StorageFixture _fixture;

public ListBucketsWithPartialSuccessTest(StorageFixture fixture)
{
_fixture = fixture;
}

[Fact]
public void ListBucketsWithPartialSuccess()
{
ListBucketsWithPartialSuccessSample partialSample = new ListBucketsWithPartialSuccessSample();
var bucketName = _fixture.GenerateBucketName();
_fixture.CreateBucket(bucketName: bucketName, location: "US", storageClass: "MULTI_REGIONAL");

var buckets = partialSample.ListBucketsWithPartialSuccess(_fixture.ProjectId);

Assert.Contains(buckets.Reachable, c => c.Name == bucketName);

if (buckets.Unreachable.Any())
{
// This indicates that the environment had unreachable buckets.
// We don't assert on the count to avoid flaky tests.
}
}
}
4 changes: 2 additions & 2 deletions storage/api/Storage.Samples.Tests/StorageFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ public void DeleteHmacKey(string accessId, bool isActive)
} while (true);
}

public void CreateBucket(string bucketName, string location = null, AutoclassData autoclassData = null)
public void CreateBucket(string bucketName, string location = null, AutoclassData autoclassData = null, string storageClass = null)
{
StorageClient storageClient = StorageClient.Create();
storageClient.CreateBucket(ProjectId, new Bucket { Name = bucketName, Location = location, Autoclass = autoclassData });
storageClient.CreateBucket(ProjectId, new Bucket { Name = bucketName, Location = location, Autoclass = autoclassData, StorageClass = storageClass });
SleepAfterBucketCreateUpdateDelete();
TempBucketNames.Add(bucketName);
}
Expand Down
68 changes: 68 additions & 0 deletions storage/api/Storage.Samples/ListBucketsWithPartialSuccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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_list_buckets_partial_success]

using Google.Api.Gax;
using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using System;
using System.Collections.Generic;
using System.Linq;

public class ListBucketsWithPartialSuccessSample
{
/// <summary>
/// Lists buckets with an option of return partial success if specific regions are unreachable.
/// </summary>
/// <param name="projectId">The ID of the project to list the buckets.</param>
/// <param name="returnPartialSuccess">If true, buckets from reachable locations are returned. The resource names of
/// buckets from unreachable locations will be available on the <c>Unreachable</c> property of a raw response from
/// <see cref="PagedEnumerable{TResponse, TResource}.AsRawResponses"/>.</param>
public (IReadOnlyList<Bucket> Reachable, IReadOnlyList<string> Unreachable) ListBucketsWithPartialSuccess
(string projectId = "your-project-id", bool returnPartialSuccess = true)
{
var storage = StorageClient.Create();
var pagedResult = storage.ListBuckets(projectId, options: new ListBucketsOptions
{
ReturnPartialSuccess = returnPartialSuccess
});
Comment on lines +33 to +40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The method is named ListBucketsWithPartialSuccess, which strongly implies that the ReturnPartialSuccess option is always enabled. Exposing a returnPartialSuccess parameter to disable this feature can be confusing for users of this sample.

Furthermore, if this method is called with returnPartialSuccess: false and some storage locations are unreachable, the storage.ListBuckets call will throw an exception. Since this sample doesn't include error handling for that scenario, it could lead to an unexpected crash.

To make the sample more focused, clear, and robust, I suggest removing the returnPartialSuccess parameter and hardcoding the option to true.

Please also remove the corresponding <param name="returnPartialSuccess"> documentation block from lines 30-32.

    public (IReadOnlyList<Bucket> Reachable, IReadOnlyList<string> Unreachable) ListBucketsWithPartialSuccess
        (string projectId = "your-project-id")
    {
        var storage = StorageClient.Create();
        var pagedResult = storage.ListBuckets(projectId, options: new ListBucketsOptions
        {
            ReturnPartialSuccess = true
        });


var reachableBuckets = new List<Bucket>();
var unreachableBuckets = new List<string>();

foreach (var page in pagedResult.AsRawResponses())
{
reachableBuckets.AddRange(page.Items ?? Enumerable.Empty<Bucket>());
unreachableBuckets.AddRange(page.Unreachable ?? Enumerable.Empty<string>());
}

Console.WriteLine("Buckets:");
foreach (var bucket in reachableBuckets)
{
Console.WriteLine(bucket.Name);
}

if (unreachableBuckets.Any())
{
Console.WriteLine("The Resource Names of Buckets from Unreachable Locations:");
foreach (var bucket in unreachableBuckets)
{
Console.WriteLine(bucket);
}
}
return (reachableBuckets, unreachableBuckets);
}
}
// [END storage_list_buckets_partial_success]
2 changes: 1 addition & 1 deletion storage/api/Storage.Samples/Storage.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Google.Cloud.Storage.Control.V2" Version="1.5.0" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="4.13.0" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="4.14.0" />
</ItemGroup>

</Project>