Google Cloud Storage Python list_blob() not printing object list

不想你离开。 提交于 2019-12-02 08:53:57

问题


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

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())

If I understood the documentation correctly, print(bucket.list_blobs()) should print something like this: ['testfile.txt', 'testfile2.txt'].

However, my script printed this: "google.cloud.storage.bucket._BlobIterator object at 0x7fdfdbcac590"

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!


回答1:


An example list function:

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)



回答2:


What do you see if you:

for blob in bucket.list_blobs():
    print(blob)
    print(blob.name)


来源:https://stackoverflow.com/questions/39784244/google-cloud-storage-python-list-blob-not-printing-object-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!