Firebase存储安全规则

Rub*_*444 6 firebase firebase-security firebase-realtime-database firebase-storage

我刚开始使用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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我无法理解人们的答案

就像几个月前的这个

    {
  "rules": {
    "UsersDB": {
      "$uid": {
        ".read": "auth.uid == $uid",
        ".write": "auth.uid == $uid"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释当前的Firebase(以及iOS Swift ......如果它很重要)如何制作它以便用户1只能读/写他/她的数据/照片

Thi*_*ryC 7

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

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

(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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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