2016-10-16 62 views
1

我想壓縮文件並將二進制輸出轉儲到AWS S3。我首先測試了我的代碼,確保它甚至可以寫入本地ZIP文件,該文件不起作用。如何將從zip到Node.js的二進制流寫入S3

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.putObjectfs.writeFileSync(作爲測試方法)一起使用的東西。

回答

0

Got it!

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); 
    }); 
    }); 
}