Google cloud storage 使用google云存储API删除路径前缀内的所有blob
我正在使用google云存储python API。我遇到了一个情况,我需要删除一个文件夹,其中可能有数百个文件使用API。有没有一种不进行递归和多次删除调用的有效方法 我的一个解决方案是列出bucket中具有给定路径前缀的所有blob对象,然后逐个删除它们 另一种解决方案是使用Google cloud storage 使用google云存储API删除路径前缀内的所有blob,google-cloud-storage,Google Cloud Storage,我正在使用google云存储python API。我遇到了一个情况,我需要删除一个文件夹,其中可能有数百个文件使用API。有没有一种不进行递归和多次删除调用的有效方法 我的一个解决方案是列出bucket中具有给定路径前缀的所有blob对象,然后逐个删除它们 另一种解决方案是使用gsutil: $gsutil rm-R gs://bucket/path 试着这样做: bucket = storage.Client().bucket(bucket_name) blobs = bucket.list_
gsutil
:
$gsutil rm-R gs://bucket/path
试着这样做:
bucket = storage.Client().bucket(bucket_name)
blobs = bucket.list_blobs()
while True:
blob = blobs.next()
if not blob: break
if blob.name.startswith('/path'): blob.delete()
如果要删除bucket的内容而不是bucket中的文件夹,可以通过单个方法调用来完成:
bucket = storage.Client().bucket(bucket_name)
bucket.delete_blobs(bucket.list_blobs())
在这种情况下,folder=“path”与
from google.cloud import storage
def deleteStorageFolder(bucketName, folder):
"""
This function deletes from GCP Storage
:param bucketName: The bucket name in which the file is to be placed
:param folder: Folder name to be deleted
:return: returns nothing
"""
cloudStorageClient = storage.Client()
bucket = cloudStorageClient.bucket(bucketName)
try:
bucket.delete_blobs(blobs=bucket.list_blobs(prefix=folder))
except Exception as e:
print str(e.message)