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