[英]trying to create a human readable date / time from a firestore timestamp
[英]Create archive from readable, add file and pipe to writable
目标是从客户端接收存档、添加文件并将其上传到 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();
});
});
正如我从文档中看到的那样,他们正在使用 fs.createReadStream(file1) 来访问文件系统,但是如果您从接收到的数据中获取 Buffer 就可以实现这一点
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 目录,如果是这种情况,值得快速搜索
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.