Node.js 云运行-将压缩文件附加到响应

Node.js 云运行-将压缩文件附加到响应,node.js,asynchronous,puppeteer,google-cloud-run,Node.js,Asynchronous,Puppeteer,Google Cloud Run,我正在使用Node上的函数框架包运行谷歌云运行的测试环境。我目前正在尝试使用.download将zip文件附加到我的响应中,但我不相信该文件存在于测试云实例中,而它存在于我的文件中?对不起,我真的不太熟悉正在发生的事情 tldr:我试图提示下载包含站点上所有图像的zip,但我在将zip附加到响应时遇到问题 index.js const puppeteer = require("puppeteer"); const archiver = require("archiv

我正在使用Node上的函数框架包运行谷歌云运行的测试环境。我目前正在尝试使用.download将zip文件附加到我的响应中,但我不相信该文件存在于测试云实例中,而它存在于我的文件中?对不起,我真的不太熟悉正在发生的事情

tldr:我试图提示下载包含站点上所有图像的zip,但我在将zip附加到响应时遇到问题

index.js

const puppeteer = require("puppeteer");
const archiver = require("archiver");
const fs = require("fs");

exports.screenshot = async (req, res) => {
  var output = fs.createWriteStream("./example.zip");
  var archive = archiver("zip");

  archive.on("error", function (err) {
    throw err;
  });

  archive.pipe(output);

  let browserPromise = puppeteer.launch({
    args: ["--no-sandbox"],
  });

  const url = req.query.url || "http://example.org";

  const browser = await browserPromise;
  const context = await browser.createIncognitoBrowserContext();
  const page = await context.newPage();

  await page.setViewport({ width: 3840, height: 2160 });
  await page.goto(url);

  const images = await page.evaluate(() =>
    Array.from(document.getElementsByTagName("img"), (e) => [
      e.x,
      e.y,
      e.width,
      e.height,
    ])
  );
 count = 0;
  await asyncForEach(images, async (image) => {
    count++;
    await page.evaluate(
      (y) => document.scrollingElement.scrollTo(0, y),
      image[1]
    );
    await page.screenshot({
      path: `image.png`,
      clip: {
        x: image[0],
        y: image[1],
        width: image[2],
        height: image[3],
      },
    });
    if (count < 10) {
      archive.file("./image.png", { name: `0${count}.png` });
    } else {
      archive.file("./image.png", { name: `${count}.png` });
    }
  });

  archive.finalize();
  //res.setHeader("Content-Type", "image/png");
  // res.set("Content-Type", "application/zip");
  // res.set("Content-Disposition", "attachment; filename=" + "example.zip");
  // res.download("/example.zip", "result.zip");
  // res.send("example.zip");

  context.close();
};

async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array);
  }
}

问题是,对于Cloud Run,没有内置的存储机制,文件只能临时存储在容器实例中进行处理,但这意味着您不能在要处理的应用程序旁边“部署”文件

对于持久存储,您必须将应用程序与集成

"scripts": {
    "start": "functions-framework --target=screenshot"
  },
"dependencies": {
    "@google-cloud/functions-framework": "^1.7.1",
    "archiver": "^5.1.0",
    "fs": "0.0.1-security",
    "puppeteer": "^5.5.0"
  }