首页 文章

Firebase存储安全规则

提问于
浏览
4

我刚开始使用Firebase,我能够读/写/编辑/删除数据库 . 在我的应用程序中,我只向用户显示数据,如果他/她有权访问它 .

我这样做是通过创建用户节点和另一个节点(称之为服务)并引用该用户子节点中的服务 .

我以前从未使用过Firebase的安全规则,现在我想开始使用Firebase存储图像 .

我正在按照教程和我的控制台说,

许可被拒绝 . 无法访问存储桶..请访问Firebase控制台中的“存储”选项卡,为您的存储桶启用Firebase存储,并确保您具有足够的权限来正确配置资源

在谷歌上搜索和搜索如何设置这些安全规则我不知道什么是正确的答案 . 一些答案建议我在我的代码中编写方法以授予权限,但文档建议我需要在Firebase的最后完成 .

这是其中一个例子

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

我无法理解人们的答案

就像几个月前的这个

{
  "rules": {
    "UsersDB": {
      "$uid": {
        ".read": "auth.uid == $uid",
        ".write": "auth.uid == $uid"
      }
    }
  }
}

Can somebody please explain for the current Firebase (and for iOS Swift..if it matters) how to just make it so user 1 can only read/write his/her data/photos

1 回答

  • 5

    您需要相应的文件路径结构:

    例如,当您上传文件存储它们时,如下所示:

    (root…) /user/uidxxx/myfile.jpg

    其中“uidxxx”是您的身份验证数据库中定义的唯一用户ID .

    然后在控制台/存储/规则选项卡上,您可以编写规则:

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

    {userId} 是一个通配符,将被相应的"uidxxx"替换

相关问题