[英]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.