You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Storage规则异常:上传正常但获取下载URL无权限

Firebase Storage getDownloadURL 返回 storage/unauthorized 问题解决

问题原因

你的Storage规则将read和write权限共用了同一套条件,但其中request.resource.size和request.resource.contentType仅在写入(upload)操作时有效,执行读取(getDownloadURL)操作时request.resource对象不存在,导致这两个条件判定为false,触发权限拒绝。

修正后的Storage规则

将read和write的权限条件拆分,read仅保留用户UID匹配校验:

service firebase.storage {
  match /b/{bucket}/o {
    match /images/{userId}/{fileName} {
      // 写入权限保留原有所有校验
      allow write: if request.auth.uid == userId
                          && request.resource.size < 1 * 1024 * 1024
                          && (request.resource.contentType.matches('image/jpeg')
                              || request.resource.contentType.matches('image/png'));
      // 读取权限仅校验用户UID匹配
      allow read: if request.auth.uid == userId;
    }
  }
}

额外验证点

  • 确认调用getDownloadURL时用户处于已登录状态(auth.currentUser不为null),上传后未出现登出情况
  • 核对文件路径中的UID与当前登录用户UID完全一致(注意大小写、特殊字符)

内容的提问来源于stack exchange,提问作者Paweł Owczarek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 02:42:47