kee*_*kee 6 google-cloud-storage google-cloud-python
我试图用它的Python库删除GCS中的文件夹及其所有内容(包括子目录).另外我明白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/')
Run Code Online (Sandbox Code Playgroud)
第一次调用delete_blob工作但不是第二次.我可以递归删除文件夹么?
Bra*_*ugh 10
要删除以特定前缀开头的所有内容(例如,目录名称),您可以遍历列表:
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs(prefix='some/directory')
for blob in blobs:
blob.delete()
Run Code Online (Sandbox Code Playgroud)
请注意,对于具有数百万或数十亿对象的非常大的存储桶,这可能不是一个非常快速的过程.为此,您需要执行更复杂的操作,例如在多个线程中删除或使用生命周期配置规则来安排要删除的对象.
小智 5
现在可以通过以下方式完成:
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.")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2517 次 |
| 最近记录: |