从可读创建存档,将文件和管道添加到可写

发布于 2025-01-15 08:28:46 字数 1011 浏览 1 评论 0原文

目标是从客户端接收存档、添加文件并将其上传到 Cloud Storage,而不创建临时文件。客户端和服务器都使用archiver库。下面代码的问题是 file2.txt 没有添加到存档中。

客户端:

import archiver from "archiver";

const archive = archiver("zip", {
    zlib: { level: 9 },
});

archive.append("string cheese!", { name: "file1.txt" });

await fetch(`/archive`, {
    method: "POST",
    body: archive,
});

服务器:

import archiver from "archiver";
import { Storage } from "@google-cloud/storage";
import { Router } from "express";

const router = Router();
const storage = new Storage();

router.post("/", (req, res) => {
  const archive = archiver("zip", {
    zlib: { level: 9 },
  });

  const cloudFile = storage.bucket("archives").file("archive.zip");

  req.pipe(archive, {
    end: false,
  });

  archive.pipe(cloudFile.createWriteStream());

  req.on("end", async () => {
    archive.append("string cheese!", { name: "file2.txt" });
    archive.finalize();
    archive.end();
  });
});

The goal is to receive an archive form the client, add a file, and upload it to Cloud Storage without creating a temporary file. The client and the server both use the archiver library. The problem with the code below is that file2.txt does not get added to the archive.

Client:

import archiver from "archiver";

const archive = archiver("zip", {
    zlib: { level: 9 },
});

archive.append("string cheese!", { name: "file1.txt" });

await fetch(`/archive`, {
    method: "POST",
    body: archive,
});

Server:

import archiver from "archiver";
import { Storage } from "@google-cloud/storage";
import { Router } from "express";

const router = Router();
const storage = new Storage();

router.post("/", (req, res) => {
  const archive = archiver("zip", {
    zlib: { level: 9 },
  });

  const cloudFile = storage.bucket("archives").file("archive.zip");

  req.pipe(archive, {
    end: false,
  });

  archive.pipe(cloudFile.createWriteStream());

  req.on("end", async () => {
    archive.append("string cheese!", { name: "file2.txt" });
    archive.finalize();
    archive.end();
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾兮 2025-01-22 08:28:46

正如我从 文档 中看到的
他们正在使用 fs.createReadStream(file1) 来访问文件系统
但是,如果您从接收到的数据中获取缓冲区,则可以实现此目的,

const multer  = require('multer')
const upload = multer()

router.post("/", upload.none(), function (req, res, next) {

// append a file from buffer
const buffer = req.files[0].buffer
archive.append(buffer, { name: 'file3.txt' });
})

如果您在无服务器环境中工作,则很可能您无权访问,这也是一个提示
对文件系统进行读取、写入和清理
但在某些情况下您可以访问 /temp 目录
如果是这种情况,值得快速搜索

as i can see from the documentations
they are using fs.createReadStream(file1) to reach out file system
however you can achieve this if you get Buffer from the received data

const multer  = require('multer')
const upload = multer()

router.post("/", upload.none(), function (req, res, next) {

// append a file from buffer
const buffer = req.files[0].buffer
archive.append(buffer, { name: 'file3.txt' });
})

also a tip if you working in serverless env most likely you don't have access
to file system to read, write and clean
but at some cases you have access to /temp directory
it worth a quick search if this is the case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文