我找不到任何有关如何使用 python 谷歌云存储的批处理功能的示例。我看到它存在here https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/storage/google/cloud/storage/batch.py.
我想要一个具体的例子。假设我想删除一堆具有给定前缀的 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()上下文管理器 https://google-cloud-python.readthedocs.io/en/latest/storage/client.html#google.cloud.storage.client.Client.batch(可在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()上下文管理器 https://google-cloud-python.readthedocs.io/en/latest/storage/client.html#google.cloud.storage.client.Client.batch自动处理此限制,并在需要时发出多个批量请求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)