在启用了分层命名空间的存储桶中,列出整个存储桶的所有对象或带有某个前缀的所有对象需要消耗大量资源,因为该操作必须遍历每个文件夹和子文件夹,类似于文件系统中的 ls -r 命令。因此,存储桶中的文件夹越多,列出对象的速度就越慢。大量空文件夹也会对对象列出性能产生负面影响。为避免对性能造成不利影响,我们建议您在每个文件夹中存放尽可能多的对象,并定期删除空文件夹。
在使用 Google Cloud 控制台或 Google Cloud CLI 时,您可以使用递归式删除来自动删除文件夹。
您可以使用脚本或自动化流程来定期删除空文件夹。以下脚本提供了一种删除空文件夹的基本方法。此脚本会按顺序删除文件夹,对于大型存储桶,此过程可能会很慢,因此您可以考虑针对生产环境优化此脚本。此外,该脚本还会删除所有空文件夹(显式或隐式创建),包括托管文件夹及其关联的 IAM 政策。如果您需要保留特定文件夹和托管文件夹,请根据要保留的资源调整脚本。
# List all the folders under <bucket>/<prefix> and export results into# folders.txt
gcloudstoragefolderslistgs://<bucket>/<prefix>|grepstorage_url|sed's/storage_url: //' > folders.txt
# Reverse the folder list and export results into folders-reverse.txt
sed'1!G;h;$!d'folders.txt > folders-reverse.txt
# Try deleting each folder in the reverse order (to guarantee child# folders are deleted before parent folders). This will fail for# non-empty folders, so only empty folders will be deleted
xargs-I{}gcloudstoragefoldersdelete"{}" < folders-reverse.txt
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-05。"],[],[],null,["# Optimize performance in buckets with hierarchical namespace enabled\n\nThis page provides guidance on how you can optimize performance in buckets with\nhierarchical namespace enabled.\n\nListing objects\n---------------\n\nThe following are the performance considerations for listing objects:\n\n- In buckets with hierarchical namespace enabled, listing all objects for the entire bucket or with a prefix is resource-intensive as the operation must traverse each folder and subfolder, similar to the `ls -r` command in a file system. Consequently, if there are more folders in your bucket, the slower the object listing happens. A large number of empty folders can also negatively impact object listing performance. To avoid negatively impacting performance, we recommend that you maximize the number of objects in each folder and regularly delete empty folders.\n- Listing or retrieving objects and sub folders within a specific folder using a delimiter and a specific prefix is more efficient in buckets with hierarchical namespace enabled as the objects are organized within a folder structure. To optimize listing performance when using a delimiter and a specific prefix, set the `includeFoldersAsPrefixes` parameter. Otherwise, Cloud Storage performs additional checks to exclude empty folders, which can slow down the operation. For more information about using the `includeFoldersAsPrefixes` when listing objects, see [Listing objects](/storage/docs/json_api/v1/objects/list).\n\nFolder management\n-----------------\n\nFor efficient folder management, we recommend the following:\n\n- **Pre-create folder structure:** Instead of relying on automatic folder creation during object upload, rewrite, and compose operations, use the create folder operation to obtain your intended folder structure in advance. Pre-creating the folder structure improves the performance consistency and predictability.\n- **Maximize objects per folder ratio:** Aim for a high objects-to-folder ratio as it reduces the overhead associated with folder creation and management.\n- **Limit folder creation and deletion requests:** Creating or deleting folders is more resource-intensive than working with individual objects due to its hierarchical nature. To ensure a smooth performance, Cloud Storage limits these operations to 1000 requests per second for each bucket. Requests exceeding this limit are not explicitly restricted but resource availability determines whether they can be processed successfully.\n- **Regularly delete empty folders:** Empty folders can accumulate, especially when using Object Lifecycle Management or deleting objects without explicitly deleting their parent folders. The accumulated folders can impact the performance of object listing operation and other folder related operations. The following are some of the methods that you can use to delete empty folders:\n - When you use [Cloud Storage FUSE](/storage/docs/gcs-fuse) or [Cloud Storage connector](/dataproc/docs/concepts/connectors/cloud-storage) to interact with a bucket enabled with hierarchical namespace, deleting a directory deletes the corresponding folder in your bucket.\n - You can use a recursive delete to delete folders automatically when using the Google Cloud console or Google Cloud CLI.\n - You can use the [`delete_empty_folders.py`](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/storage/hierarchical-namespace/delete_empty_folders.py) script to periodically delete empty folders using parallel processing. The script provides an option to target a specific folder path prefix, which allows the script to perform folder deletions on a subset of the bucket's directory structure. Additionally, the script deletes all empty folders (created implicitly or explicitly) including managed folders and their associated IAM policies. For details about how to use the script, see the [README on GitHub](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/storage/hierarchical-namespace/README.d).\n\nWhat's next\n-----------\n\n- [Create and manage folders](/storage/docs/create-folders).\n- [Rename a folder](/storage/docs/rename-hns-folders).\n- [Listing objects](/storage/docs/listing-objects)."]]