【问题标题】:Firebase Storage: Programatically delete files from /tmp/Firebase 存储:以编程方式从 /tmp/ 中删除文件
【发布时间】:2020-07-01 03:24:14
【问题描述】:

我搞砸了一个 Firebase 云功能,不小心创建了 190 万张存储在 gs://myapp.appspot.com//tmp/ 中的图像。那个双斜线是准确的——服务器正在写信给/tmp/,我猜这会导致上面提到的路径。

我现在想删除那些文件(它们都是废话)。我尝试像这样使用 Python 包装器:

export GOOGLE_APPLICATION_CREDENTIALS="../secrets/service-account.json"

然后:

from google.cloud import storage

storage_client = storage.Client()
bucket = storage_client.get_bucket('tmp')
blobs = bucket.list_blobs(bucket='tmp', prefix='')
for blob in blobs:
  print(' * deleting', blob)
  blob.delete()

但这会引发:

google.api_core.exceptions.Forbidden: 403 GET https://storage.googleapis.com/storage/v1/b/tmp?projection=noAcl: firebase-adminsdk-yr6f8@myapp.iam.gserviceaccount.com does not have storage.buckets.get access to tmp.

有谁知道如何允许管理员凭据从/tmp/ 中删除?任何指针都会非常有帮助!

【问题讨论】:

    标签: python firebase google-cloud-storage firebase-storage


    【解决方案1】:

    我能够使用 gsutil 命令重现此问题:

    gsutil cp ~/<example-file> gs://<my-project-name>.appspot.com//tmp/
    

    首先,在我的Firebase console 中,我可以通过一个勾号(整个文件夹)来完成,不确定您是否考虑这一点。

    无论如何,如果您想使用 API 完成它,我已经找到了以下解决方案。

    1. 我认为(与我的测试相比)存储桶名称应该是:myapp.appspot.com
    2. 如果你在 python 中打印 blob,你会得到这样的结果:&lt;Blob: &lt;my-project-name&gt;.appspot.com, /tmp/&lt;example-file&gt;, 1584700014023619>

    第二个值是 blob 对象的 name 属性。我注意到在这种情况下,它的 blob 名称以 /tmp/

    开头

    适用于我的代码是:

    from google.cloud import storage
    storage_client = storage.Client()
    bucket = storage_client.get_bucket('myapp.appspot.com')
    blobs = bucket.list_blobs()
    for blob in blobs:
            if blob.name.startswith('/tmp/'):
                    print(' * deleting', blob)
                    blob.delete()
    

    我不认为它非常优雅的解决方案,但一次修复可能就足够了。

    希望对你有帮助!

    【讨论】:

    • 谢谢一百万! firebase api 显然有几个 python 客户端,所以我的谷歌搜索是从每个客户端中提取代码/示例。这非常有用!
    猜你喜欢
    • 2017-12-28
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多