有没有办法防止重复图像上传到firebase存储

时间:2017-04-08 05:43:58

标签: firebase firebase-storage

我有一个要求,我的应用程序将允许用户上传图像。在firebase存储中是否有一个功能,我可以防止重复,并在用户/其他用户尝试加载相同的图像时使用旧的图像URL。

1 个答案:

答案 0 :(得分:-1)

是。散列图像并使用该名称存储文件。编写一条不允许覆盖文件的规则。

// Upload the file on the client to a file named after a hash of the data
var string = 'Hello, world!'
firebase.storage().ref().child('images/' + hash(string)).putString(string);

var hash = function(string) { /* SHA1, MD5, etc. (note: collisions exist for certain algorithms ) */ }


// Rules
service firebase.storage {
  match /b/{bucket}/o {
    match /images/{imageHash} {
      // only allow create and delete, not overwrite
      allow write: if resource == null || request.resource == null
    }
  }
}
相关问题