Tuesday, 4 April 2017

How to download and extract zip files into memory then upload the files using node js

I need to download and extract gzip file from s3 bucket into memory then I need to upload the files as a folder to S3 bucket using node js

Actually am getting error when I download and extracted into memory.Kindly help me to process the files and upload into s3 without storing on local drive.

function DownloadZipInMemory() {

var bucketName = "my.Testbucket";

var fileKey = "employee.tar.gz";

var params = { Bucket: bucketName, Key: fileKey };

s3.getObject(params)
.on('httpData', function (chunk) {

    data.push(chunk);
    dataLen += chunk.length;
 })
 .on('httpDone', function () {

 var buf = new Buffer(dataLen);

 for (var i = 0, len = data.length, pos = 0; i < len; i++) {
        data[i].copy(buf, pos);
        pos += data[i].length;
 }
 var zip = new AdmZip(buf);    < == Getting Error

    var zipEntries = zip.getEntries();

    for (var i = 0; i < zipEntries.length; i++) {
        var fileName = zip.readAsText(zipEntries[i]);
       //  var folderName=?
        UploadFilesToS3(folderName, fname)
    }

}).send();

}

function UploadFilesToS3(folder, fname) {

var filestream = fs.createReadStream(fname);
    var s3 = new AWS.S3({ params: { Bucket: BucketPath } });

fs.readFile(fname, function (err, data) {

    s3.upload({

        Key: folder + fname
        , Body: filestream
        // change contentType as needed
        , ACL: 'public-read',
    })
   });
 //.......
 }



via Sharan

No comments:

Post a Comment