I have unzipped the zip files to buffer. Now I need to upload those unzipped files from buffer to S3 bucket using Node.js. How do I do that?
1 Answer
Hope this will help, Just loop on your unzipped files and send each
var AWS = require('aws-sdk');
var fs = require('fs');
module.exports.UploadFile = function(credentials, BucketPath, folder, Region, fname, s3url, req, res, callback) {
// AWS Region
AWS.config.region = Region;
// AWS credentials
AWS.config.update(credentials);
// create read stream for file
var filestream = fs.createReadStream(fname);
//create an instance of s3
var s3 = new AWS.S3({params: {Bucket: BucketPath}});
// read file
fs.readFile(fname, function (err, data) {
//upload to s3
s3.upload({ Key: folder + fname
, Body: filestream
// change contentType as needed
, ACL: 'public-read','ContentType' : 'application/pdf'}
, function(err,result) {
if(err) throw err;
// check if the file uploaded or not
s3.getObject({ Key: folder + fname },
function (error, data) {
if (error != null) {
} else {
// remove file from local if needed
fs.unlink(fname, function (err) {
if (err) {
console.log(err);
}
callback("", data);
});
}
});
});
});
};