how to check if file exists in Firebase Storage?

Pier picture Pier · Jun 10, 2016 · Viewed 14k times · Source

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?

Answer

mjr picture mjr · Jun 10, 2016

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);
}