gpt4 book ai didi

python - 谷歌云存储 Python list_blobs() 不打印对象列表

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:13 30 4
gpt4 key购买 nike

我是 Python 和 Google Cloud Storage 新手。我正在编写一个 python 脚本,以使用 Google Cloud Python 客户端库从 Google Cloud Storage 存储桶中获取文件列表,并且 Bucket 类中的 list_blobs() 函数没有按预期工作。

https://googlecloudplatform.github.io/google-cloud-python/stable/storage-buckets.html

这是我的python代码:

from google.cloud import storage
from google.cloud.storage import Blob

client = storage.Client.from_service_account_json(service_account_json_key_path, project_id)
bucket = client.get_bucket(bucket_id)
print(bucket.list_blobs())

如果我对文档的理解正确,print(bucket.list_blobs()) 应该打印如下内容:['testfile.txt', 'testfile2.txt']。

但是,我的脚本打印了这个:“0x7fdfdbcac590 处的 google.cloud.storage.bucket._BlobIterator 对象”

delete_blob() 文档中的示例代码与我的相同。 https://googlecloudplatform.github.io/google-cloud-python/stable/storage-buckets.html

我不确定我在这里做错了什么。任何指针/示例/答案将不胜感激。谢谢!

最佳答案

列表函数示例:

def list_blobs(bucket_name):
"""Lists all the blobs in the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)

blobs = bucket.list_blobs()

for blob in blobs:
print(blob.name)

关于python - 谷歌云存储 Python list_blobs() 不打印对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39784244/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com