[英]Node.js ArchiverJS library not writing to output filestream in AWS Lambda
我正在嘗試按照此處的建議使用node-archiver壓縮目錄的內容。 該函數在 Node.js 運行時環境中的 AWS Lambda 中運行。
我的功能如下:
function zipDirectory(source, outputTarget) {
var archive = archiver("zip");
const stream = fs.createWriteStream(outputTarget, { flags: 'w' });
stream.on("close", () => {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
return new Promise((resolve, reject) => {
archive.pipe(stream);
archive.on("error", err => reject(err))
archive.directory(source, false);
archive.finalize();
stream.close();
resolve();
});
}
結果是創建了 zip 但文件大小為零:
INFO 0 total bytes
INFO archiver has been finalized and the output file descriptor has closed.
我也無法提取具有以下錯誤的存檔。
INFO Error: end of central directory record signature not found
筆記:
/tmp/
目錄。zip
二進制文件並將其作為子進程運行,但顯然,這是不受支持的,因為它是一個 32 位包。 如果您有替代方法,我可以使用它來壓縮節點之外的目錄,這些目錄可以在 Lambda 的運行時中使用,請務必告知。感謝您的幫助,謝謝!
好吧,搞清楚我的錯誤了。
archive.finalize()
返回一個promise,並且函數在歸檔完成之前返回。stream.close()
是不必要的,因為archive.finalize()
會為您執行此操作,因此無論存檔是否完成,流都將被關閉。正確的代碼如下所示:
function zipDirectory(source, outputTarget) {
var archive = archiver("zip");
const stream = fs.createWriteStream(outputTarget, { flags: 'w' });
stream.on("close", () => {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
return new Promise(async (resolve, reject) => {
archive.pipe(stream);
archive.on("error", err => reject(err))
archive.directory(source, false);
await archive.finalize();
resolve();
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.