Forked from sarfarazansari/nodejs_aws_s3_file_upload.js
Created
July 18, 2018 17:37
-
-
Save codejockie/bc458ac787dbf7dd29c2d256839ea362 to your computer and use it in GitHub Desktop.
How to upload files to AWS S3 with NodeJS - AWS-SDK? With the help of this library you can upload any kind of file to s3. (NODEJS, AWS-SDK, S3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* we are going to upload file to s3 via node js by using | |
* aws-sdk - required | |
* busboy - required | |
* uuid - optional - for image renaming purpose | |
* with this library you can upload any kind of file to s3 via node js. | |
*/ | |
const AWS = require('aws-sdk'); | |
const UUID = require('uuid/v4'); | |
const Busboy = require('busboy'); | |
AWS.config.update({ accessKeyId: 'S3_ACCESS_KEY', secretAccessKey: 'S3_SECRET_KEY' }); | |
const S3 = new AWS.S3(); | |
/** | |
* route where we get multipart form data | |
* capture file and upload files to s3 | |
* | |
*/ | |
app.post("/upload", (req, res) => { | |
let chunks = [], fname, ftype, fEncoding; | |
let busboy = new Busboy({ headers: req.headers }); | |
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { | |
console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype); | |
fname = filename.replace(/ /g,"_"); | |
ftype = mimetype; | |
fEncoding = encoding; | |
file.on('data', function(data) { | |
// you will get chunks here will pull all chunk to an array and later concat it. | |
console.log (chunks.length); | |
chunks.push(data) | |
}); | |
file.on('end', function() { | |
console.log('File [' + filename + '] Finished'); | |
}); | |
}); | |
busboy.on('finish', function() { | |
const userId = UUID(); | |
const params = { | |
Bucket: 'BUCKET_NAME', // your s3 bucket name | |
Key: `${userId}-${fname}`, | |
Body: Buffer.concat(chunks), // concatinating all chunks | |
ACL: 'public-read', | |
ContentEncoding: fEncoding, // optional | |
ContentType: ftype // required | |
} | |
// we are sending buffer data to s3. | |
S3.upload(params, (err, s3res) => { | |
if (err){ | |
res.send({err, status: 'error'}); | |
} else { | |
res.send({data:s3res, status: 'success', msg: 'Image successfully uploaded.'}); | |
} | |
}); | |
}); | |
req.pipe(busboy); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment