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

时间:2018-01-24 18:29:40

标签: reactjs firebase firebase-storage firebase-security-rules

我正在尝试设置一个规则,在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 - 意外的'userId'

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

1 个答案:

答案 0 :(得分:4)

与这两位评论者一样,语法不允许带“ -

的”文件夹“

所以你必须选择像

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

制定规则:

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