Firebase存储安全规则

时间:2017-02-20 08:04:25

标签: firebase firebase-realtime-database firebase-security firebase-storage

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

我通过创建用户节点和另一个节点(称之为服务)并引用该用户子节点中的服务来实现这一点。

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

我正在关注一个教程,我的控制台说,

  

许可被拒绝。无法访问存储桶..   请访问存储,为您的存储桶启用Firebase存储   Firebase控制台中的选项卡,并确保您有足够的内容   允许正确配置资源

在谷歌搜索和搜索SO如何设置这些安全规则时,我不确定什么是正确的答案。一些答案建议我在我的代码中编写方法来授予权限,但文档建议我需要在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"
      }
    }
  }
}

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

2 个答案:

答案 0 :(得分:6)

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

例如,当您上传文件时,将它们存储起来:

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

&#34; uidxxx&#34;是您的身份验证数据库中定义的唯一用户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}是一个通配符,将被相应的&#34; uidxxx&#34;

取代

答案 1 :(得分:0)

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }`enter code here`
  }
}

这是Firebase存储的正确答案

相关问题