gpt4 book ai didi

python - 使用 Google Cloud Storage python 客户端批量请求

转载 作者:太空狗 更新时间:2023-10-29 22:20:58 26 4
gpt4 key购买 nike

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

我想要一个具体的例子。假设我想删除一堆具有给定前缀的 blob。我将开始获取 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_to_delete:
blob.delete()

如果您直接使用 REST API,则只需担心每批 100 件商品。 batch() context manager自动处理此限制,并在需要时发出多个批处理请求。

关于python - 使用 Google Cloud Storage python 客户端批量请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45100483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com