如何批量删除存储桶

Eri*_*uan 6 python google-cloud-storage

如何批量删除存储桶?

这是我尝试过的.

def deleteAllBuckets():
    batch = storage_client.batch()
    with batch:
        for bucket in storage_client.list_buckets():
            bucket.delete()
Run Code Online (Sandbox Code Playgroud)

技术上它是有效的,因为桶被删除,但我不相信我发送一个批量请求.看起来我每桶发送一个请求.

将上述代码与Google Cloud Datastore中的批量请求进行比较

def deleteAllEntities():
    query = datastore_client.query(kind="Charge")
    queryIter = query.fetch()

    batch = datastore_client.batch()
    with batch:
        for entity in queryIter:
            batch.delete(entity.key)
Run Code Online (Sandbox Code Playgroud)

您可以看到我正在批处理对象上调用delete方法.使用存储代码,我在bucket对象上调用delete.不幸的是,云存储python API没有任何示例.

jte*_*ace 2

当您使用上下文管理器时,它会自动将请求添加到批处理中,并在退出上下文管理器时执行。

但是,我在您的代码中看到的问题是您在批处理上下文管理器中调用列表存储桶。我认为您想在外部创建存储桶迭代器。像这样的东西:

def deleteAllBuckets():
    buckets_iterator = storage_client.list_buckets()
    batch = storage_client.batch()
    with batch:
        for bucket in buckets_iterator:
            bucket.delete()
Run Code Online (Sandbox Code Playgroud)