0

我是Python和Google Cloud Storage的新手。 我正在编写一个python脚本,使用Google Cloud Python Client Library从Google Cloud Storage存储桶中获取文件列表,并且Bucket类中的list_blobs()函数未按预期工作。Google Cloud Storage Python list_blob()不打印对象列表

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

如果我理解正确的文档,打印(bucket.list_blobs())应该打印是这样的: 'TESTFILE.TXT', 'testfile2.txt']。

然而,我的脚本印刷这样的:

delete_blob()的文档 “0x7fdfdbcac590 google.cloud.storage.bucket._BlobIterator对象” 具有例如代码相同矿。 https://googlecloudplatform.github.io/google-cloud-python/stable/storage-buckets.html

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

回答

2

一个例子列表功能:

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) 
+0

它的工作原理!谢谢! – bkang61wk

0

你看到了什么,如果你:

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

它的工作原理!非常感谢! – bkang61wk

相关问题