El objetivo es recibir un archivo del cliente, agregar un archivo y cargarlo en Cloud Storage sin crear un archivo temporal. Tanto el cliente como el servidor utilizan la biblioteca del archiver . El problema con el siguiente código es que file2.txt no se agrega al archivo.
Cliente:
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, });Servidor:
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(); }); });