Google Cloud Storage:如何在 Python 中(递归)删除文件夹 [英] Google Cloud Storage: How to Delete a folder (recursively) in Python

查看:19
本文介绍了Google Cloud Storage:如何在 Python 中(递归)删除文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用其 Python 库删除 GCS 中的一个文件夹及其所有内容(包括子目录).另外我知道 GCS 并没有真正的文件夹(但前缀?),但我想知道我该怎么做?

I am trying to delete a folder in GCS and its all content (including sub-directories) with its Python library. Also I understand GCS doesn't really have folders (but prefix?) but I am wondering how I can do that?

我测试了这段代码:

from google.cloud import storage

def delete_blob(bucket_name, blob_name):
    """Deletes a blob from the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)

    blob.delete()

delete_blob('mybucket', 'top_folder/sub_folder/test.txt')
delete_blob('mybucket', 'top_folder/sub_folder/')

第一次调用 delete_blob 有效,但第二次无效.我可以递归删除文件夹吗?

The first call to delete_blob worked but not the 2nd one. What can I delete a folder recursively?

推荐答案

要删除以某个前缀开头的所有内容(例如,目录名称),您可以遍历列表:

To delete everything starting with a certain prefix (for example, a directory name), you can iterate over a list:

storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs(prefix='some/directory')
for blob in blobs:
  blob.delete()

请注意,对于包含数百万或数十亿对象的非常大的存储桶,这可能不是一个很快的过程.为此,您需要做一些更复杂的事情,例如在多个线程中删除或使用生命周期配置规则来安排要删除的对象.

Note that for very large buckets with millions or billions of objects, this may not be a very fast process. For that, you'll want to do something more complex, such as deleting in multiple threads or using lifecycle configuration rules to arrange for the objects to be deleted.

这篇关于Google Cloud Storage:如何在 Python 中(递归)删除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆