问题
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