【问题标题】:Google Cloud Storage Python list_blob() not printing object listGoogle Cloud Storage Python list_blob()不打印对象列表
【发布时间】:2017-02-08 14:27:32
【问题描述】:

我是 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']。

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

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

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

【问题讨论】:

    标签: python google-app-engine google-cloud-storage google-cloud-platform google-cloud-python


    【解决方案1】:

    一个示例列表函数:

    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】:

    如果你看到了什么:

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

    【讨论】:

      猜你喜欢
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2019-02-14
      • 2017-07-30
      • 2014-02-12
      • 2020-06-19
      相关资源
      最近更新 更多