仅当 Firebase 存储上尚不存在资源时才允许写入

uri*_*ish 7 firebase firebase-storage

我正在尝试设置 Firebase 安全规则,仅当同名文件不存在时才允许上传文件。理想情况下,当新文件的内容与现有文件相同时,我希望支持覆盖文件。

我尝试了以下方法:

allow write: if !resource;
Run Code Online (Sandbox Code Playgroud)

allow write: if !resource.size;
Run Code Online (Sandbox Code Playgroud)

allow write: if request.resource.md5hash == resource.md5hash;
Run Code Online (Sandbox Code Playgroud)

但似乎两者都不起作用。他们所做的只是禁止任何上传。

Mik*_*ald 3

我认为你想要这些的组合:

service firebase.storage {
  match /b/<your-bucket>/o {
    match /path/to/file {
      // !resource allows the upload of a new file
      // hash comparison allows re-upload of the same file
      // note that property names are case-sensitive
      allow write: if !resource || request.resource.md5Hash == resource.md5Hash;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)