how to check if file exists in Firebase Storage?

前端 未结 3 1458
渐次进展
渐次进展 2020-12-30 21:32

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.

3条回答
  •  情深已故
    2020-12-30 22:22

    I've found a nice solution while keeping within the Node.js Firebase Gogole Cloud Storage SDK using the File.exists, taught it would be ideal to share for those searching.

    const admin = require("firebase-admin");
    const bucket = admin.storage().bucket('my-bucket');
    
    const storageFile = bucket.file('path/to/file.txt');
    storageFile
      .exists()
      .then(() => {
        console.log("File exists");
      })
      .catch(() => {
        console.log("File doesn't exist");
      });
    

    Google Cloud Storage: Node.js SDK version 5.1.1 (2020-06-19) at the time of writing

提交回复
热议问题