-
Notifications
You must be signed in to change notification settings - Fork 409
samples(storagecontrol): add delete folder recursive sample #15658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...torage.Control.V2/Google.Cloud.Storage.Control.V2.Snippets/DeleteFolderRecursiveSample.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright 2026 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 Google.Cloud.Storage.Control.V2; | ||
| using System; | ||
|
|
||
| namespace Google.Cloud.Storage.Control.V2.Snippets | ||
| { | ||
| public class DeleteFolderRecursiveSample | ||
| { | ||
| public void DeleteFolderRecursive(string bucketName, string folderName) | ||
| { | ||
| // [START storage_control_delete_folder_recursive] | ||
| StorageControlClient storageControlClient = StorageControlClient.Create(); | ||
|
|
||
| // Format: projects/{project}/buckets/{bucket}/folders/{folder} | ||
| string folderResourceName = $"projects/_/buckets/{bucketName}/folders/{folderName}"; | ||
|
|
||
| storageControlClient.DeleteFolderRecursive(folderResourceName).PollUntilCompleted(); | ||
|
|
||
| Console.WriteLine($"Deleted folder {folderName} recursively"); | ||
| // [END storage_control_delete_folder_recursive] | ||
| } | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
...ge.Control.V2/Google.Cloud.Storage.Control.V2.Snippets/DeleteFolderRecursiveSampleTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright 2026 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 Google.Cloud.ClientTesting; | ||
| using Google.Cloud.Storage.V1; | ||
| using Google.Cloud.Storage.Control.V2; | ||
| using System; | ||
| using Xunit; | ||
|
|
||
| namespace Google.Cloud.Storage.Control.V2.Snippets | ||
| { | ||
| [SnippetOutputCollector] | ||
| public class DeleteFolderRecursiveSampleTest | ||
| { | ||
| [SkippableFact] | ||
| public void TestDeleteFolderRecursive() | ||
| { | ||
| string projectId = Environment.GetEnvironmentVariable("TEST_PROJECT"); | ||
| Skip.If(string.IsNullOrEmpty(projectId), "No TEST_PROJECT environment variable set."); | ||
|
|
||
| // Setup: Create HN bucket, add folder. | ||
| var storageClient = StorageClient.Create(); | ||
| string bucketName = IdGenerator.FromGuid(prefix: "test-hn-bucket-"); | ||
| string folderName = "test-folder"; | ||
|
|
||
| var bucket = new Google.Apis.Storage.v1.Data.Bucket | ||
| { | ||
| Name = bucketName, | ||
| HierarchicalNamespace = new Google.Apis.Storage.v1.Data.Bucket.HierarchicalNamespaceData { Enabled = true }, | ||
| IamConfiguration = new Google.Apis.Storage.v1.Data.Bucket.IamConfigurationData | ||
| { | ||
| UniformBucketLevelAccess = new Google.Apis.Storage.v1.Data.Bucket.IamConfigurationData.UniformBucketLevelAccessData { Enabled = true } | ||
| } | ||
| }; | ||
|
|
||
| storageClient.CreateBucket(projectId, bucket); | ||
|
|
||
| try | ||
| { | ||
| var storageControlClient = StorageControlClient.Create(); | ||
|
|
||
| var folder = new Folder { Name = $"projects/_/buckets/{bucketName}/folders/{folderName}" }; | ||
| storageControlClient.CreateFolder(new CreateFolderRequest | ||
| { | ||
| ParentAsBucketName = BucketName.FromProjectBucket("_", bucketName), | ||
| FolderId = folderName, | ||
| Folder = folder | ||
| }); | ||
|
|
||
| // Execution: Call sample method. | ||
| var sample = new DeleteFolderRecursiveSample(); | ||
| sample.DeleteFolderRecursive(bucketName, folderName); | ||
|
|
||
| // Verification: Assert successful deletion by trying to get the folder. | ||
| var ex = Assert.Throws<Grpc.Core.RpcException>(() => | ||
| storageControlClient.GetFolder(new GetFolderRequest { Name = folder.Name })); | ||
| Assert.Equal(Grpc.Core.StatusCode.NotFound, ex.StatusCode); | ||
|
Comment on lines
+66
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the repository's general rules, when asserting that an exception is thrown in a test, we should also assert on the exception message to ensure the correct exception is being propagated. var ex = Assert.Throws<Grpc.Core.RpcException>(() =>
storageControlClient.GetFolder(new GetFolderRequest { Name = folder.Name }));
Assert.Equal(Grpc.Core.StatusCode.NotFound, ex.StatusCode);
Assert.Contains("not found", ex.Message, StringComparison.OrdinalIgnoreCase);References
|
||
| } | ||
| finally | ||
| { | ||
| // Cleanup: Standard Dispose. | ||
| storageClient.DeleteBucket(bucketName); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To align with the project's use of strongly-typed resource names (like
BucketName), consider usingFolderNameinstead of manually formatting the resource name string. This reduces the risk of formatting errors and makes the code cleaner.