【问题标题】:Best way to send zip file as response in Azure functions using node?使用节点在 Azure 函数中发送 zip 文件作为响应的最佳方式?
【发布时间】:2019-06-04 01:21:07
【问题描述】:

使用 Azure 函数应用,我希望能够从不同的 url 下载图像到特定文件夹,压缩它们并将压缩文件发送回响应中。

我可以通过以下步骤来实现:

  1. 文件请求
  2. 在本地保存文件
  3. 使用压缩目录
  4. 存档读取压缩文件,将其转换为base64
  5. 在响应正文中发送缓冲区

下载并保存图片

const img = await request(url, { encoding: "binary" });
fs.writeFile(filesName, data, "binary", err => {
    if (err) {
        reject(`Error while writing the file; ${err}`);
    } else {
        resolve(data);
    }
});

压缩目录,读取压缩文件并发送响应

const target = await zipDirectory(dirName, targetFile);
context.log('Target ' + targetFile);
const rawFile = await readFile(targetFile);
const fileBuffer = Buffer.from(rawFile, "base64");
context.res = {
    body: fileBuffer,
    headers: {
    "Content-Disposition": `filename=target.zip`,
    "Content-Type": "application/zip"
},
status: 202
};

有没有更好的方法来做到这一点?

【问题讨论】:

    标签: javascript node.js zip azure-functions


    【解决方案1】:
    1. 使用http 触发器创建一个函数,其中输入将是图像的 uri,以及 blob 容器的输出绑定。逻辑是将图像保存在 blob 存储中。

    2. 创建另一个由 blob 触发的函数,该函数将抓取文件并对其进行压缩,并且它可以具有输出 blob 绑定。它将压缩文件并将其放置在您的输出 blob 绑定中。

    3. 您的压缩文件将位于输出 blob 容器中。

    或者,您可以使用持久功能来编排整个过程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2018-01-14
      相关资源
      最近更新 更多