samples(storagecontrol): add delete folder recursive sample#4340
samples(storagecontrol): add delete folder recursive sample#4340nidhiii-27 wants to merge 1 commit into
Conversation
[Generated-by: AI]
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a script to recursively delete folders in Google Cloud Storage using the @google-cloud/storage-control library, along with its corresponding system test. Feedback suggests shortening the generated bucket name in tests to avoid exceeding the 63-character limit, and wrapping the bucket deletion in a try/catch block within the cleanup hook to prevent obscuring setup failures.
| const bucketPrefix = `storage-control-samples-${uuid.v4()}`; | ||
| const bucketName = `${bucketPrefix}-a`; |
There was a problem hiding this comment.
The bucket name length is currently 62 characters (storage-control-samples- (24) + uuid.v4() (36) + -a (2)), which is extremely close to the Google Cloud Storage bucket name limit of 63 characters. Any slight increase in the prefix or suffix length in future modifications will cause the tests to fail with an invalid bucket name error. It is safer to use a shorter prefix and avoid unnecessary suffixes.
| const bucketPrefix = `storage-control-samples-${uuid.v4()}`; | |
| const bucketName = `${bucketPrefix}-a`; | |
| const bucketName = "gcs-sc-samples-" + uuid.v4(); |
| after(async () => { | ||
| try { | ||
| await bucket.deleteFiles({force: true}); | ||
| } catch (e) { | ||
| // ignore | ||
| } | ||
| await bucket.delete(); | ||
| }); |
There was a problem hiding this comment.
If the before hook fails (for example, due to permission or quota issues during bucket creation), the after hook will still run in Mocha. Since bucket.delete() is not wrapped in a try/catch block, it will throw a "Not Found" error when trying to delete a non-existent bucket. This unhandled error in the after hook will obscure the actual failure from the before hook in the test output. Wrapping bucket.delete() in a try/catch block ensures robust cleanup and clearer test failure reports.
after(async () => {
try {
await bucket.deleteFiles({force: true});
} catch (e) {
// ignore
}
try {
await bucket.delete();
} catch (e) {
// ignore
}
});
angelcaamal
left a comment
There was a problem hiding this comment.
To use const [operation] = await controlClient.deleteFolderRecursive(request); you need to update the @google-cloud/storage-control library to its latest version. The current version in package.json (^0.5.0) does not include this method, which is why the test is failing.
|
Thank you @angelcaamal for helping in figuring out the test failure. This PR was raised as an experiment for a project. Closing this one, I'll soon be raising another PR as part of the same project. |
This PR adds the SDK sample and tests for the hierarchical namespace recursive delete feature, resolving b/521168740.