在Firebase存储中,我可以使用数据库获取规则吗?可以使用、规则、数据库、Firebase

2023-09-03 09:18:23 作者:敌场称霸,

我正在尝试基于用户数据保护我的Firebase(谷歌云存储)文件。在FireStore中,我使用了一个基于获取数据库内容的规则(在USERS表中查找uid并匹配一个字段),这很好用。我正尝试在Firebase存储中使用相同类型的规则,但在模拟器中得到Error: simulator.rules line [12], column [17]. Function not found error: Name: [get].; Error: Invalid argument provided to call. Function: [get], Argument: ["||invalid_argument||"]。我的规则如下:

  match /b/{bucket}/o {
    function isAuth() {
      return request.auth != null && request.auth.uid != null
    }
    function isAdmin() {
      return isAuth() &&
      "admin" in get(/databases/$(database)/documents/users/$(request.auth.uid)).data.roles;
    }
    function clientMatch(client) { // expects user's "client" field to be ID of client
      return isAuth() &&
      client == get(/databases/$(database)/documents/users/$(request.auth.uid)).data.client;
    }
    match /P/Clients/{client}/{allPaths=**} {
      allow read, write: if isAdmin() || clientMatch(client);
    }
  }
}
第12行是clientMatch()client == get的开头行。 我还不知道这些函数是否只支持FiRestore(Db)规则,或者它们是否也适用于存储。

如果这不起作用,我还有什么选择?人们如何查找用户数据以确保Firebase存储安全?

推荐答案

如何手动向Firebase添加数据

您当前无法引用存储规则中的FiRestore文档。如果您希望将其作为存储规则的一项功能,请file a feature request。

请考虑改用Cloud Functions存储触发器在上载文件后执行一些其他检查,如果发现该文件无效则将其删除。