reactjs - Firebase 存储规则 - 意外 'userId'

标签 reactjs firebase firebase-storage firebase-security

我正在尝试在 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 - Unexpected 'userId'

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

最佳答案

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

所以你必须选择像

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

制定规则如下:

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

关于reactjs - Firebase 存储规则 - 意外 'userId',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48429316/

相关文章:

javascript - nextjs 的生产 URL

javascript - 每当可选 Prop 发送到子组件时显示错误

javascript - React Native 平面列表(无限滚动)正在从 Firebase 返回重复记录

Firebase 存储图像缓存不起作用

Firebase 存储工件

javascript - 子组件应该在什么级别拥有自己的容器(逻辑react-redux的connect)组件才能管理自己的mapDispatchToProps?

javascript - 使用 Jest 的问题 - 无法读取未定义的属性 'xxx'

firebase - Firestore : restricting child/field access with security rules

android - Firebase 集成后 Crashlytics 混淆停止工作

node.js - 使用 Google Cloud Storage 将音频文件从 Google 文本转语音保存到 Firebase 存储?