2017-07-14 17 views
2

python googleクラウドストレージのバッチ機能の使用方法に関する例は見つかりません。それが存在するのを見たhereGoogle Cloud StorageのPythonクライアントでのバッチリクエスト

具体的な例が気に入っています。私は与えられた接頭辞を持つブロブの束を削除したいとしましょう。私は

from google.cloud import storage 

storage_client = storage.Client() 
bucket = storage_client.get_bucket('my_bucket_name') 
blobs_to_delete = bucket.list_blobs(prefix="my/prefix/here") 

# how do I delete the blobs in blobs_to_delete in a single batch? 

# bonus: if I have more than 100 blobs to delete, handle the limitation 
#  that a batch can only handle 100 operations 

答えて

3

TLを次のようにブロブのリストを取得を開始したい; DR - ジャスト(google-cloud-pythonライブラリで利用可能)batch() context manager内のすべての要求を送信

をこの例を試してみてください。

from google.cloud import storage 

storage_client = storage.Client() 
bucket = storage_client.get_bucket('my_bucket_name') 
# Accumulate the iterated results in a list prior to issuing 
# batch within the context manager 
blobs_to_delete = [blob for blob in bucket.list_blobs(prefix="my/prefix/here")] 

# Use the batch context manager to delete all the blobs  
with storage_client.batch(): 
    for blob in blobs: 
     blob.delete() 

REST APIを直接使用している場合、バッチあたり100個のアイテムについて心配する必要があります。 batch() context managerは自動的にこの制限を処理し、必要に応じて複数のバッチ要求を発行します。

関連する問題