【问题标题】:Firebase storage security rules not workingFirebase 存储安全规则不起作用
【发布时间】:2017-08-02 19:40:51
【问题描述】:

我尝试使用元数据在 Firebase 存储上暗示安全规则,但它不起作用:

service firebase.storage {
  match <firebase-storage-url> {
  match /{userId}/{allPaths=**}{
  allow read: if resource.metadata.userid== userId;
  allow write: if resource.metadata.userid== userId
  }
  }
}


StorageMetadata  metadata = new StorageMetadata.Builder()
            .setCustomMetadata("userid", user.ID)
            .build();

filepath = mStorage.child( user.ID + "/" + String.valueOf(chatmessageamount + 1) + ".mp3");

有人可以帮我吗?

【问题讨论】:

  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。有关澄清此问题的帮助,请参阅如何提问页面。 stackoverflow.com/help/how-to-ask
  • 我想将元数据用于安全规则以及我是如何实现的,但它不起作用
  • 第一部分是存储安全规则。第二部分是我如何将文件上传到 Firebase 存储

标签: android firebase firebase-security firebase-storage


【解决方案1】:

我想你想要的是:

service firebase.storage {
  match /b/{bucket}/o {  // this should be this string literally, no need to put in the bucket name
    match /{userId}/{allPaths=**}{
      allow read: if request.auth.uid == userId;
      allow write: if request.resource.metadata.userId == userId;  // request.resource is the resource being written, resource is what already exists (which on first write will be null)
    }
  }
}

【讨论】:

  • 我尝试过但不工作。读取始终有效,不受限制
  • 我没有使用这条线: match /b/{bucket}/o{ 也许这就是读取始终有效的原因。使用存储桶行代替我的项目名称有什么重要性?
  • 规则匹配对象的 RESTful 路径(即/b/some-bucket/o/path/to/object),{bucket} 捕获任何存储桶名称。没有它,规则应该失败,因为它们不应该匹配任何内容。请注意,如果您通过下载令牌下载,则会忽略规则。
  • @MosheGil 如果它回答了您的问题,请考虑接受此答案(单击它左侧的复选标记),以便其他人知道它已解决。
猜你喜欢
  • 2018-01-30
  • 2021-01-03
  • 1970-01-01
  • 2018-11-05
  • 2020-08-24
  • 2017-01-25
  • 2021-12-24
相关资源
最近更新 更多