谷歌云存储python客户端的批量请求

问题描述:

我找不到任何关于如何使用python谷歌云存储的批处理功能的例子。我看到它存在here谷歌云存储python客户端的批量请求

我很喜欢一个具体的例子。比方说,我想要删除一组具有给定前缀的blob。我开始越来越斑点的名单如下

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 

TL; DR - 只需发送的所有batch() context manager范围内的请求(可在google-cloud-python库)

试试这个例子:

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会自动处理此限制,并在需要时发出多个批处理请求。