how to check if file exists in Firebase Storage?

我的梦境 提交于 2020-01-12 11:46:42

问题


When using the database you can do snapshot.exists() to check if certain data exists. According to the docs there isn't a similar method with storage.

https://firebase.google.com/docs/reference/js/firebase.storage.Reference

What is the proper way of checking if a certain file exists in Firebase Storage?


回答1:


I believe that the FB storage API is setup in a way that the user only request a file that exists.

Thus a non-existing file will have to be handled as an error: https://firebase.google.com/docs/storage/web/handle-errors




回答2:


You can use getDownloadURL which returns a Promise, which can in turn be used to catch a "not found" error, or process the file if it exists. For example:

storageRef.child("file.png").getDownloadURL().then(onResolve, onReject);

function onResolve(foundURL) {
    //stuff
}

function onReject(error) {
    console.log(error.code);
}


来源:https://stackoverflow.com/questions/37751202/how-to-check-if-file-exists-in-firebase-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!