【问题标题】:How to check if a file exists in Firebase storage when trying to download multiple files - javascript尝试下载多个文件时如何检查Firebase存储中是否存在文件 - javascript
【发布时间】:2019-07-18 17:22:38
【问题描述】:

我有一个需要从 Firebase 存储下载的文件夹和文件名称的 Arrylist。

它抛出异常是因为某些文件夹和文件在 firebase storage 中不存在。 是否可以在不引发异常的情况下检测文件夹和文件?

JS 代码:

var parNum = 'S3DD4';
var sessionNum = '3333';
var storageInfoarr = ["Testing/Testing1.3gp", "Testing/Testing1.xlsx"];
 for (i = 0 ; i< storageInfoarr.length; i++){
    var starsRef = storageRef.child(parNum+'/'+sessionNum+'/'+storageInfoarr[i])
    var urlString = starsRef.getDownloadURL().catch(function(error) {
    switch (error.code) {
      case 'storage/object_not_found': // <<< here you decide what to do when the file doesn't exist
        // File doesn't exist
        break;

      case 'storage/unauthorized':
        // User doesn't have permission to access the object
        break;

      case 'storage/canceled':
        // User canceled the upload
        break;

    }
  });

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions firebase-storage


    【解决方案1】:

    getDownloadUrl() 总是会在路径不正确时抛出异常,你可以做的是处理这些异常并且只下载存在的文件。您可以创建一个合并的 Promise 来判断某些路径是否正确和不正确,并相应地处理它们。

    var parNum = 'S3DD4';
    var sessionNum = '3333';
    var storageInfoarr = ["Testing/Testing1.3gp", "Testing/Testing1.xlsx"];
    
    Promise.all(storageInfoarr.map((sStorage) => {
        var starsRef = storageRef.child(parNum+'/'+sessionNum+'/'+sStorage);
        starsRef.getDownloadURL().then(url => {
            // url is download url, use it download the file
        }).catch(err => {
           // Here you can handle the error for individual download
        });
    }) ).then((aResponses) => {
       // This will be called if all promises are resolved. aResponses contain all download url
    }).catch((err) => {
       // This will be called if any of the download path is incorrect, with the error of first promise which fails
    });
    

    【讨论】:

    • 仍然抛出异常
    • 它总是会抛出异常,你需要按照提供的代码正确处理它们。
    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 2021-02-26
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2022-01-24
    相关资源
    最近更新 更多