如何使用 Google Cloud API 获取给定存储桶中的文件夹列表

问题描述

我想使用 Google Cloud Storage API 获取给定 Google Cloud 存储桶或文件夹中的所有文件夹.

I wanted to get all the folders inside a given Google Cloud bucket or folder using Google Cloud Storage API.

例如如果gs://abc/xyz包含三个文件夹gs://abc/xyz/x1,gs://abc/xyz/x2gs://abc/xyz/x3.API 应返回 gs://abc/xyz 中的所有三个文件夹.

For example if gs://abc/xyz contains three folders gs://abc/xyz/x1, gs://abc/xyz/x2 and gs://abc/xyz/x3. The API should return all three folder in gs://abc/xyz.

使用 gsutil

gsutil ls gs://abc/xyz

但我需要使用 python 和 Google Cloud Storage API 来完成.

But I need to do it using python and Google Cloud Storage API.


解决方案

您可以使用 Python GCS API 客户端库.有关指向文档和下载.

You can use the Python GCS API Client Library. See the Samples and Libraries for Google Cloud Storage documentation page for relevant links to documentation and downloads.

在您的情况下,首先我想指出您混淆了桶"一词.我建议阅读文档的 关键条款 页面.你说的是对象名前缀.

In your case, first I want to point out that you're confusing the term "bucket". I recommend reading the Key Terms page of the documentation. What you're talking about are object name prefixes.

您可以从 list-objects 开始.py 示例在 GitHub 上.查看 列表 参考页,您需要传递 bucket=abcprefix=xyz/delimiter=/.

You can start with the list-objects.py sample on GitHub. Looking at the list reference page, you'll want to pass bucket=abc, prefix=xyz/ and delimiter=/.

相关文章