在Python中的Google Cloud Platform存储桶中遍历目录树

S.G*_*.G. 5 python google-cloud-storage google-cloud-platform

对于本地计算机上的目录,该os.walk()方法通常用于在Python中遍历目录树。

Google有一个Python模块(google.cloud.storage),可以使用本地运行的Python脚本在GCP存储桶中进行上传和下载。

我需要一种在GCP存储桶中遍历目录树的方法。我浏览了google.cloudPython模块中的类,但是找不到任何东西。有没有一种方法可以os.walk()对GCP存储桶中的目录执行类似的操作?

Bra*_*ugh 6

GCS 库中不存在这样的函数。然而,GCS 可以按前缀列出对象,这通常是足够等效的:

from google.cloud import storage

bucket = storage.Client().get_bucket(bucket_name)
for blob in bucket.list_blobs(prefix="dir1/"):
  print(blob.name)
Run Code Online (Sandbox Code Playgroud)