简体   繁体   中英

Create archive from readable, add file and pipe to writable

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();
  });
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM