Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud storage 使用google云存储API删除路径前缀内的所有blob_Google Cloud Storage - Fatal编程技术网

Google cloud storage 使用google云存储API删除路径前缀内的所有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_

我正在使用google云存储python API。我遇到了一个情况,我需要删除一个文件夹,其中可能有数百个文件使用API。有没有一种不进行递归和多次删除调用的有效方法

我的一个解决方案是列出bucket中具有给定路径前缀的所有blob对象,然后逐个删除它们

另一种解决方案是使用
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)