![](/img/trans.png)
[英]How to download Zip File from an s3 bucket to a local directory within a node.js lambda function
[英]How to write Node.js binary stream from zip to an S3
我想壓縮文件並將二進制輸出轉儲到AWS S3。 我正在測試我的代碼,確保它甚至可以寫入本地ZIP文件,這是無效的。
function zipFiles(filenames) {
return new Promise((resolve, reject) => {
const child = spawn(zipCmd, ['-'].concat(filenames));
let buffer = '';
child.stdout.on('data', (data) => {
buffer += data.toString();
});
child.stderr.on('data', (data) => {
// console.error(data.toString());
});
child.on('close', (code) => {
fs.writeFileSync('testing.zip', buffer);
resolve(code);
});
});
}
這會導致損壞的zip文件。 我不確定如何處理來自spawn
的緩沖流並將其組裝成可與s3.putObject
和fs.writeFileSync
一起使用的東西(作為測試方法)。
得到它了!
function zipFiles(filenames) {
return new Promise((resolve, reject) => {
const out = fs.openSync('testing.zip', 'a');
const child = spawn(zipCmd, ['-'].concat(filenames));
let buffer = new Buffer('');
child.stdout.on('data', (data) => {
buffer = Buffer.concat([buffer, data]);
});
child.stderr.on('data', (data) => {
// console.error(data.toString());
});
child.on('close', (code) => {
fs.writeFileSync('testing.zip', buffer);
resolve(code);
});
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.