问题描述
尝试使用Python来获取和遍历我拥有的Cloud Storage存储桶中的所有文件.我正在使用官方库 google-cloud-storage
.
Trying to use Python to get and iterate through all of the files inside of a Cloud Storage bucket I own. I'm using the official library, google-cloud-storage
.
使用 gsutil
,我可以运行 gsutil ls gs://my-composer-bucket/dags/composer_utils/
之类的命令. google-cloud-storage
库是否提供与 gsutil ls
等效的方法?我想使用Python客户端,而不是使用 gsutil
(不想在Docker映像中安装和验证GCloud SDK).
Using gsutil
, I can run commands like gsutil ls gs://my-composer-bucket/dags/composer_utils/
. Does the google-cloud-storage
library offer an equivalent method to gsutil ls
? I'd like to use the Python client rather than shell out to gsutil
(don't want to install and authenticate the GCloud SDK inside of a Docker image).
我尝试了一些不同的操作,这些操作使我对blob的工作方式感到困惑:
I've tried a few different things which have left me confused on how blobs work:
>>> dag_folder_blob = cloud_composer_bucket.blob(bucket, 'dags/')
>>> dag_folder_blob.exists()
True
>>> util_folder_blob = cloud_composer_bucket.blob(bucket, 'dags/composer_utils/') # directory exists
>>> util_folder_blob.exists()
False
>>> util_file_blob = cloud_composer-bucket.blob(bucket, 'dags/composer_utils/__init__.py')
>>> util_file_blob.exists()
True
推荐答案
您将要使用 list_blobs 方法.了解有关列出云存储中对象的更多信息.
You will want to use the list_blobs method of a Bucket object. Read more about listing objects in Cloud Storage.
这篇关于Google Cloud SDK Python客户端:如何在Cloud Storage存储桶中列出文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!