【问题标题】:Firebase storage in free plan: Anonymous caller does not have storage.objects.get access免费计划中的 Firebase 存储:匿名调用者没有 storage.objects.get 访问权限
【发布时间】:2020-12-16 10:54:30
【问题描述】:

我正在使用免费的 Firebase 存储计划。当我想从 Python 中存储图片时,它可以正常工作:

import os
import firebase_admin
from firebase_admin import credentials, storage


file_extension = os.path.splitext(request.files["input_image"].filename)[1] # ex: jpg
cred = credentials.Certificate(os.getenv("FIREBASE_CREDS"))
firebase_admin.initialize_app(cred)
bucket = storage.bucket(os.getenv("FIREBASE_BUCKET"))

# ex: profile_pictures/user1.jpg
blob = bucket.blob(f"profile_pictures/{current_user.username}{file_extension}") 
blob.upload_from_string(request.files["input_image"].filename)
profile_picture_url = blob.public_url

但是,当我尝试访问图片公共 url 时,我看到了这个页面,而我期待的是一张图片:

链接如下:

https://storage.googleapis.com/bucket_name/folder_name/filename

请注意,我的规则策略如下:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
}
}

【问题讨论】:

  • 您使用的是 Python 的 Admin SDK,它完全绕过了 Firebase 安全规则。结合google-cloud-storage:stackoverflow.com/… 搜索错误消息可能会更好。

标签: python firebase firebase-storage


【解决方案1】:

问题如下:从 Python SDK 发布文件时,似乎不允许其他客户端访问该 URL。

好消息是有一个非常简单的修复方法。只需在发布文件后添加这行代码即可:

blob.make_public()

就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-18
    • 2020-09-23
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多