I trying to download zip file from AWS S3 using aws-sdk for javascript in gulp. My code download file, but it corrupted. And i can't uncompress that with 7zip program and zip command. Maybe has some encode that i missing.
Here my code:
require('dotenv').config();
const AWS = require('aws-sdk');
const fs = require('fs');
const commandLineArgs = require('command-line-args');
const options = commandLineArgs([
{name: 'filename', alias: 'f', type: String},
]);
gulp.task('zipFromS3', function() {
var AWS = require('aws-sdk');
var fs = require('fs');
AWS.config.update({accessKeyId: process.env.AWS_ACCESS_ID,secretAccessKey: process.env.AWS_ACCESS_SECRET});
var s3bucket = new AWS.S3({params:{Bucket: env.aws.access_backups.bucket}});
return new Promise(function(resolve,reject){
if(options.filename == null || typeof options.filename == 'undefined'){
return reject();
}
let filename = options.filename; //'2017-04-06.zip'
var file = fs.createWriteStream(env.server.downloads + '/'+filename);
var params = {
Key: filename,
Bucket: env.aws.access_backups.bucket
};
s3bucket.getObject(params, function(err, data){
if (err) {reject();}
}).createReadStream()
.on('error', function(err){
console.log(err);
reject();
})
.on('end', function (chunk){
resolve();
}).pipe(file);
});
});
I use this script to download some .txt file from s3, and this works well. If i go to my bucket and download my zip file directly from aws site, it works well too.
The zipped file was created in gulp too. This is the code to create the zip file (before send to AWS this zip can be uncompressed correctly).
const zip = require('gulp-zip');
const env = readConfig('.env.json', { override: true });
gulp.task('zip', () => {
let now = new Date();
let m = now.getMonth();
let d = now.getDate();
m++;
if(m < 10){m = "0"+m;}
if(d < 10){d = "0"+d;}
let name = now.getFullYear()+'-'+m+'-'+d;
if(typeof options.filename != 'undefined' && options.filename != null){
name += "_"+options.filename;
}
return gulp.src(env.server.dist+"/**")
.pipe(zip(name + ".zip"))
.pipe(gulp.dest(env.server.zipfolder));
});