如何从 Python 中删除 GCS 文件夹?

2022-01-25 00:00:00 python google-cloud-storage

问题描述

使用 https://github.com/googleapis/google-cloud-python/tree/master/storage 或 https://github.com/GoogleCloudPlatform/appengine-gcs-client,我可以通过指定文件名来删除文件,但似乎没有删除文件夹的方法.

Using https://github.com/googleapis/google-cloud-python/tree/master/storage or https://github.com/GoogleCloudPlatform/appengine-gcs-client, I can delete a files by specifying its file name, but there seems not to be ways to delete folders.

有什么方法可以删除文件夹吗?

Is there any ways to delete folders ?

我找到了这个(谷歌云存储: How to Delete a folder (recursive) in Python) in stackvoerflow,但这个答案只是删除文件夹中的所有文件,而不是删除文件夹本身.

I found this(Google Cloud Storage: How to Delete a folder (recursively) in Python) in stackvoerflow, but this answer simply deletes all the files in the folder, not deleting the folder itself.


解决方案

你提到的anwser中提到的代码工作,前缀应该是这样的:

The code mentioned in the anwser you referred works, the prefix should look like this:

from google.cloud import storage

storage_client = storage.Client()
bucket = storage_client.get_bucket('my-bucket')

blobs = bucket.list_blobs(prefix='my-folder/')

for blob in blobs:
    blob.delete()

相关文章