Google Cloud Storage Python list_blob()不打印对象列表 [英] Google Cloud Storage Python list_blob() not printing object list
问题描述
我是Python和Google Cloud Storage新手. 我正在编写一个python脚本,以使用Google Cloud Python客户端库从Google Cloud Storage存储桶中获取文件列表,而Bucket类中的list_blobs()函数无法正常运行.
I am Python and Google Cloud Storage newbie. I am writing a python script to get a file list from Google Cloud Storage bucket using Google Cloud Python Client Library and list_blobs() function from Bucket class is not working as I expected.
https://googlecloudplatform.github.io/google- cloud-python/stable/storage-buckets.html
这是我的python代码:
Here is my python code:
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'].
If I understood the documentation correctly, print(bucket.list_blobs()) should print something like this: ['testfile.txt', 'testfile2.txt'].
但是,我的脚本打印了以下内容: 位于0x7fdfdbcac590的google.cloud.storage.bucket._BlobIterator对象"
However, my script printed this: "google.cloud.storage.bucket._BlobIterator object at 0x7fdfdbcac590"
delete_blob()文档的示例代码与我的相同. https://googlecloudplatform.github.io/google-cloud-python/stable/storage-buckets.html
delete_blob() documentation has example code same as mine. https://googlecloudplatform.github.io/google-cloud-python/stable/storage-buckets.html
我不确定我在做什么错. 任何指针/示例/答案将不胜感激.谢谢!
I am not sure what I am doing wrong here. Any pointers/examples/answers will be greatly appreciated. Thanks!
推荐答案
列表函数示例:
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)
这篇关于Google Cloud Storage Python list_blob()不打印对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!