Google 云存储:如何在 Python 中删除文件夹(递归)

Google Cloud Storage: How to Delete a folder (recursively) in Python

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

我测试了这段代码:

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 有效,但第二次无效。什么可以递归删除文件夹?

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

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

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

现在可以通过以下方式完成:

def delete_folder(cls, bucket_name, folder_name):
    bucket = cls.storage_client.get_bucket(bucket_name)
    """Delete object under folder"""
    blobs = list(bucket.list_blobs(prefix=folder_name))
    bucket.delete_blobs(blobs)
    print(f"Folder {folder_name} deleted.")

deleteFiles might be what you are looking for. Or in Python delete_blobs。假设它们相同,Node 文档在描述行为方面做得更好,即

This is not an atomic request. A delete attempt will be made for each file individually. Any one can fail, in which case only a portion of the files you intended to be deleted would have.

Operations are performed in parallel, up to 10 at once.