![](/img/trans.png)
[英]Upload files to Google Cloud Storage Bucket from Google Cloud Datalab using Python API
[英]How do you fetch files from Google cloud storage bucket after a certain file is fetched using python?
假設在我的 Google Cloud 存儲桶中有大約 10k 個文件,並且在使用 python 獲取這些文件時,我將限制設置為max_results=100
。 我使用blob.updated
和blob.name
保存時間戳和最后一個文件的名稱。 如何確保下次運行我的 python 程序時,它將獲取第 100 個文件(已保存)之后的文件。 所以基本上在max_results=100
之后獲取文件,即從max_results=101
我瀏覽了文檔,但找不到與我想做的事情相關的任何內容。 我也知道 max_results 參數會給出結果,直到它被調用的數字,在我的例子中是 100。這里是代碼:
storage_client = storage.Client()
bucket_name = 'json_file.json'
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs(max_results=100)
last_file_timestamp = list()
name_list = list()
for blob in blobs:
name_list.append(blob.name)
last_file_timestamp.append(blob.updated)
print(name_list)
print(last_file_timestamp)
簡而言之-如何確保第二次執行 python 腳本時,它將在 100 個文件后從存儲桶中獲取文件。? 有辦法嗎? 請幫忙
當您對 Google API 執行查詢時,您有一組結果和一個下一頁令牌,您有更多結果。 在這種情況下,使用此令牌並向 API 請求下一頁。
這是一個基於您的代碼的示例
storage_client = storage.Client()
bucket_name = 'json_file.json'
bucket = storage_client.get_bucket(bucket_name)
#First 100 results
blobs = bucket.list_blobs(max_results=100)
for blob in blobs:
print(blob.name)
#Next 100 results
blobs = bucket.list_blobs(page_token=blobs.next_page_token,max_results=100)
for blob in blobs:
print(blob.name)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.