Google云存储python客户端的批处理请求

Google云存储python客户端的批处理请求,python,google-cloud-storage,Python,Google Cloud Storage,我找不到任何关于如何使用python google云存储的批处理功能的示例。我看到它存在 我想要一个具体的例子。假设我想删除一堆具有给定前缀的blob。我将开始得到如下的斑点列表 from google.cloud import storage storage_client = storage.Client() bucket = storage_client.get_bucket('my_bucket_name') blobs_to_delete = bucket.list_blobs(pre

我找不到任何关于如何使用python google云存储的批处理功能的示例。我看到它存在

我想要一个具体的例子。假设我想删除一堆具有给定前缀的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-只需在中发送所有请求(可在
谷歌云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_to_delete:
        blob.delete()

如果您直接使用RESTAPI,则只需担心每批100项。自动处理此限制,并在需要时发出多个批处理请求。

文档中的何处提到它是上下文管理器?@NKijak文档中没有提到这一事实,但如果您查看,您可以看到enter和exit方法。顺便说一句,根据我的用法,如果超过最大批大小(当前为1000),上下文管理器将抛出异常,因此您确实需要跟踪每个批的项目数。