From 2c982dcdf74065c356576bb5bf211578f330cb10 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Wed, 12 Nov 2025 23:25:20 -0800 Subject: [PATCH] Samples(Storage): Add sample and test for list buckets operation with return partial success --- .../DownloadPublicFileTest.cs | 4 +- .../ListBucketsWithPartialSuccessTest.cs | 45 +++++++++++++ .../MakeBucketPublicTest.cs | 4 +- .../Storage.Samples.Tests/MakePublicTest.cs | 4 +- .../Storage.Samples.Tests/StorageFixture.cs | 4 +- .../ListBucketsWithPartialSuccess.cs | 65 +++++++++++++++++++ .../Storage.Samples/Storage.Samples.csproj | 2 +- 7 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 storage/api/Storage.Samples.Tests/ListBucketsWithPartialSuccessTest.cs create mode 100644 storage/api/Storage.Samples/ListBucketsWithPartialSuccess.cs diff --git a/storage/api/Storage.Samples.Tests/DownloadPublicFileTest.cs b/storage/api/Storage.Samples.Tests/DownloadPublicFileTest.cs index 2e60c7a023e..0f4a4aebf16 100644 --- a/storage/api/Storage.Samples.Tests/DownloadPublicFileTest.cs +++ b/storage/api/Storage.Samples.Tests/DownloadPublicFileTest.cs @@ -1,4 +1,4 @@ -// Copyright 2021 Google Inc. +// Copyright 2021 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,7 +26,7 @@ public DownloadPublicFileTest(StorageFixture fixture) _fixture = fixture; } - [Fact] + [Fact(Skip = "b/477676781")] public void DownloadPublicFile() { MakePublicSample makePublicSample = new MakePublicSample(); diff --git a/storage/api/Storage.Samples.Tests/ListBucketsWithPartialSuccessTest.cs b/storage/api/Storage.Samples.Tests/ListBucketsWithPartialSuccessTest.cs new file mode 100644 index 00000000000..b43b6ec0961 --- /dev/null +++ b/storage/api/Storage.Samples.Tests/ListBucketsWithPartialSuccessTest.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.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. + } + } +} diff --git a/storage/api/Storage.Samples.Tests/MakeBucketPublicTest.cs b/storage/api/Storage.Samples.Tests/MakeBucketPublicTest.cs index 0fa3276437d..352fb9b682c 100644 --- a/storage/api/Storage.Samples.Tests/MakeBucketPublicTest.cs +++ b/storage/api/Storage.Samples.Tests/MakeBucketPublicTest.cs @@ -1,4 +1,4 @@ -// Copyright 2021 Google Inc. +// Copyright 2021 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,7 +26,7 @@ public MakeBucketPublicTest(StorageFixture fixture) _fixture = fixture; } - [Fact] + [Fact(Skip = "b/477676781")] public void MakeBucketPublic() { MakeBucketPublicSample makeBucketPublicSample = new MakeBucketPublicSample(); diff --git a/storage/api/Storage.Samples.Tests/MakePublicTest.cs b/storage/api/Storage.Samples.Tests/MakePublicTest.cs index 0bee7de93d2..62707d0bdec 100644 --- a/storage/api/Storage.Samples.Tests/MakePublicTest.cs +++ b/storage/api/Storage.Samples.Tests/MakePublicTest.cs @@ -1,4 +1,4 @@ -// Copyright 2020 Google Inc. +// Copyright 2020 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,7 +26,7 @@ public MakePublicTest(StorageFixture fixture) _fixture = fixture; } - [Fact] + [Fact(Skip = "b/477676781")] public void MakePublic() { MakePublicSample makePublicSample = new MakePublicSample(); diff --git a/storage/api/Storage.Samples.Tests/StorageFixture.cs b/storage/api/Storage.Samples.Tests/StorageFixture.cs index b3db2424408..830b3f17af3 100644 --- a/storage/api/Storage.Samples.Tests/StorageFixture.cs +++ b/storage/api/Storage.Samples.Tests/StorageFixture.cs @@ -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); } diff --git a/storage/api/Storage.Samples/ListBucketsWithPartialSuccess.cs b/storage/api/Storage.Samples/ListBucketsWithPartialSuccess.cs new file mode 100644 index 00000000000..5fecaa9d6f3 --- /dev/null +++ b/storage/api/Storage.Samples/ListBucketsWithPartialSuccess.cs @@ -0,0 +1,65 @@ +// 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 +{ + /// + /// Lists buckets, returning both the reachable buckets and the resource names of buckets from unreachable locations when specific regions are unreachable. + /// + /// The ID of the project to list the buckets. + public (IReadOnlyList Reachable, IReadOnlyList 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(); + var unreachableBuckets = new List(); + + foreach (var page in pagedResult.AsRawResponses()) + { + reachableBuckets.AddRange(page.Items ?? Enumerable.Empty()); + unreachableBuckets.AddRange(page.Unreachable ?? Enumerable.Empty()); + } + + 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] diff --git a/storage/api/Storage.Samples/Storage.Samples.csproj b/storage/api/Storage.Samples/Storage.Samples.csproj index 80310ae87bb..07cace9f9ad 100644 --- a/storage/api/Storage.Samples/Storage.Samples.csproj +++ b/storage/api/Storage.Samples/Storage.Samples.csproj @@ -6,7 +6,7 @@ - +