简体   繁体   English

如何从 nodeJS 中的 URL 获取文件,构建 zip 文件和 pipe 到云存储桶

[英]How to get files from URLs in nodeJS, build a zip file and pipe to cloud storage bucket

I want to build a cloud function that receives an object like this one:我想构建一个接收 object 的云 function,如下所示:

{
    "files": [
        {
            "url": "https://myUrl/cat.jpg",
            "name": "cat.jpg"
        },
        {
            "url": "https://anyOtherUrl/mouse.jpg",
            "name": "mouse.jpg"
        },
        {
            "url": "https://myUrl2/dog.jpg",
            "name": "dog.jpg"
        }
    ],
    "referenceId": "cute-images"
}

I would like to get those files, compress them into a zip file (with name = referenceId), save that zip file to a bucket, and finally, send the zip URL back as a response. I would like to get those files, compress them into a zip file (with name = referenceId), save that zip file to a bucket, and finally, send the zip URL back as a response.

My main problem lies with the use of memory and my desire to make correct use of pipes/streams.我的主要问题在于使用 memory 以及我希望正确使用管道/流。 I would really appreciate good sources of where to find the documentation for this implementation.我真的很感激在哪里可以找到这个实现的文档的好资源。

This is what I got for now but I don't know if it's any good:这是我现在得到的,但我不知道它是否有好处:

const ZipStream = require("zip-stream");
const fetch = require("node-fetch");
const { Storage } = require("@google-cloud/storage");

exports.zipBuilder = async (req, res) => {

  // Deleted lines of request validation

  let zip = new ZipStream();
  const queue = req.body.files;

  async function addFilesToZip() {
    let elem = queue.shift();
    const response = await fetch(elem.url);
    const stream = await response.buffer();
    zip.entry(stream, { name: elem.name }, (err) => {
      if (err) throw err;
      if (queue.length > 0) addNextFile();
      else zip.finalize();
    });
  }

  await addFilesToZip();

  const storage = new Storage();
  
  const ourBucket = storage.bucket(process.env.DESTINATION_BUCKET);

  zip.pipe(ourBucket); // this fails

  // Get ZIP URL from bucket

  res.send(zipUrl);
};

Edit: This question seems like a lot of questions in one.编辑:这个问题似乎有很多问题。 But since this must work as a single stream, I ask not for exact answers but of ideas on what to study to get a better understanding of the solution.但由于这必须作为一个单一的 stream 工作,我要求的不是确切的答案,而是关于研究什么以更好地理解解决方案的想法。

You are getting 'Unhandled rejection TypeError: dest.write is not a function' bc ourBucket is not a writable stream .您收到“未处理的拒绝 TypeError: dest.write is not a function” bc ourBucket is not a writable stream It must be an instance of writable stream for you to pipe to it.它必须是可写的 stream 的实例,您可以将 pipe 写入它。 You should create a bucket file writable stream and use that as ourBucket .您应该创建一个可写的存储桶文件 stream并将其用作ourBucket

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用nodejs从谷歌云存储获取链接文件 - how to get link file from google cloud storage use nodejs 如何根据正则表达式从Google Cloud Storage存储桶中获取文件? - How to get files from Google Cloud Storage bucket based on a regular expression? 无法通过管道直接从URL将文件完全上传到Google Cloud Storage存储桶中 - Not uploading file completely on bucket of Google Cloud Storage directly from URL through pipe 使用 NodeJS 从 Google Cloud Storage Bucket 下载文件夹 - Downloading folders from Google Cloud Storage Bucket with NodeJS 无法从Node.js上传到Google Cloud或Firebase存储桶 - Can't upload to google cloud or firebase storage bucket from nodejs 如何使用 Nodejs 中的令牌对 Cloud Storage 中的私有存储桶进行身份验证 - How to authenticate with tokens in Nodejs to a private bucket in Cloud Storage 如何从 NodeJS 中的 IBM Object 存储桶中读取 a.zip 或 .gz 文件? - How to read a .zip or .gz file from IBM Object Store Bucket in NodeJS? 如何通过管道将存档 (zip) 传输到 S3 存储桶 - how to pipe an archive (zip) to an S3 bucket 将多个文件传输到zip文件中 - Pipe multiple files into a zip file 如何从节点中的云功能读取存储在谷歌存储桶中的txt文件 - How to read a txt file, stored in a google storage bucket, from a cloud function, in node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM