【问题标题】:Firebase storage rules - Unexpected 'userId'Firebase 存储规则 - 意外的“用户 ID”
【发布时间】:2018-07-03 21:38:39
【问题描述】:

我正在尝试在 firebase 中设置一个规则进行存储,只有经过身份验证的用户才能在其中写入名为 avatar 的头像图像

这是firebase docs 示例:

// Grants a user access to a node matching their user ID
service firebase.storage {
  match /b/{bucket}/o {
    // Files look like: "user/<UID>/path/to/file.txt"
    match /user/{userId}/{allPaths=**} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}

这些是我在 storage.rules 文件中定义的规则:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }

    match /images/{allPaths=**} {
      allow write: if request.resource.size < 5 * 1024 * 1024
                    && request.resource.contentType.matches("image/.*")
                    && request.resource.contentType == resource.contentType
    }

    match /images/user-{userId}/avatar.* {
      allow read;
      allow write: if request.auth.uid == userId;
    }
  }
}

当我部署时抛出的错误是

[E] 13:25 - 意外的“用户 ID”

我在文档中找不到任何其他内容告诉我如何定义此 userId。文档指定了它,我做错了什么?

【问题讨论】:

  • 难道通配符不允许作为部分匹配,而只能在某个位置作为完全匹配?所以,不是/images/user-{userId},而是/images/{userId}
  • 我认为你的意思是 /images/user/{userId}/avatar 不是 /images/user-{userId}/avatar ,不是吗?

标签: reactjs firebase firebase-storage firebase-security


【解决方案1】:

就像两位评论者所说的那样,语法不允许带有 - 的“文件夹”

所以你必须选择像

这样的结构
/images/user/{userId}

制定如下规则:

match /images/user/{userId}/avatar.* {
  allow read;
  allow write: if request.auth.uid == userId;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-11
    • 2020-10-17
    • 2020-07-20
    • 2019-11-07
    • 2021-01-03
    • 2020-11-28
    • 2017-06-16
    相关资源
    最近更新 更多