如何获取DownloadURL列表并从Firebase Storage下载所有文件的.ZIP文件夹 [英] How to take a list of DownloadURLs and donwload a .ZIP folder of all the files from Firebase Storage

查看:108
本文介绍了如何获取DownloadURL列表并从Firebase Storage下载所有文件的.ZIP文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Firebase存储桶中的文件夹和子文件夹中有很多文件.我的实时Firebase数据库中有所有这些文件的DownloadURLs列表.我正在尝试进行批量下载,理想情况下,创建一个.ZIP文件夹供最终用户立即下载.

I have many files in folders and subfolders in my Firebase Storage bucket. I have a list of DownloadURLs of all of these files in my Realtime Firebase Database. I'm trying to do a bulk download, ideally creating a .ZIP folder for the end user to download at once.

其他几篇文章(例如如何从中下载整个文件夹Firebase Storage?)请注意,没有内置的API可以进行批量下载,例如从存储桶下载整个文件夹.据我所知,这并没有改变.

Several other posts (eg How to download entire folder from Firebase Storage?) note that there is no built in API to do bulk downloads, such as downloading an entire folder from a bucket. As far as I know this has not changed.

但是有一个相关的答案(如何从以下位置下载整个文件夹Firebase Storage?)建议可以创建.ZIP文件夹,但没有详细说明.

However a related answer (How to download entire folder from Firebase Storage?) suggests that creating a .ZIP folder may be possible, but does not elaborate on how.

我怎么能...

... A)在客户端使用纯JS允许最终用户收集所有DownloadURL并立即下载文件?...

... A) use pure JS on the client side to allow the end user to collect all the DownloadURLs and download the files at once?...

...或...

... B)使用Node.JS中编写的Cloud Function来使用DownloadURLs创建一个.ZIP文件,最终用户可以将其下载为一个文件.(我假设.ZIP文件可以作为具有自己的DownloadURL的另一个文件存储在存储桶中,并且可以像其他任何文件一样下载,然后在本地解压缩并提取所有原始文件.如果不是,请更正我情况)

... B) Use a Cloud Function written in Node.JS to use the DownloadURLs to create a .ZIP file that can be downloaded by the end user as one file. (I'm assuming a .ZIP file can be stored in the Storage bucket as another file with its own DownloadURL and be downloaded like any other file, then unzipped locally and all the original files be extracted. Please correct me if this is not the case)

...还是这些方法都不可行?如果不是原因,缺少什么功能?

... Or are neither of those approaches feasible? If not why, what functionality is missing?

在此先感谢您的见解!

推荐答案

迟到总比没有好,您可以使用JSZip 提供了简单的API用法.

Better late than never, you can use JSZip which provides a simple API usage.

生成zip文件后,您可以使用 FileSaver 保存/下载.

After the zip file has been generated you can then use FileSaver to save/download it.

这是我(从客户端)生成一个zip文件的方式

This is how I generated a zip file (from the client-side)

import JSZip from "jszip";
import saveAs from "file-saver";

generateZipFile(photosToDownload) {

  var jszip = new JSZip();

  //Looping through to get the download url for each image
  for (let i = 0; i < photosToDownload.length; i++) {
    let photoName = photosToDownload[i].photo_image_name;
    let photoImageURL = photosToDownload[i].generated_image_url;

    let photoImageExtension = photoName.substr(photoName.length - 4);

    jszip.file(
      photoName + photoImageExtension, //in my case, this produces something like 'my image.jpg'
      this.downloadUrlAsPromise(photoImageURL) //generates the file we need to download
    );

    //If we have reached the last image in the loop, then generate the zip file
    if (i == photosToDownload.length - 1) {
      jszip.generateAsync({ type: "blob" }).then(function(content) {
        saveAs(content, "MyZipFile.zip");
      });
    }
  }
},

downloadUrlAsPromise(url) {
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.responseType = "arraybuffer";
    xhr.onreadystatechange = function(evt) {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          resolve(xhr.response);
        } else {
          reject(new Error("Error for " + url + ": " + xhr.status));
        }
      }
    };
    xhr.send();
  });
}, 

这篇关于如何获取DownloadURL列表并从Firebase Storage下载所有文件的.ZIP文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆