Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Last active August 21, 2023 19:30
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save keithweaver/575a61aab19711bbeb98c10785be4674 to your computer and use it in GitHub Desktop.
Save keithweaver/575a61aab19711bbeb98c10785be4674 to your computer and use it in GitHub Desktop.
S3 File Upload to AWS S3
const AWS = require('aws-sdk');
const Busboy = require('busboy');
const BUCKET_NAME = '';
const IAM_USER_KEY = '';
const IAM_USER_SECRET = '';
function uploadToS3(file) {
let s3bucket = new AWS.S3({
accessKeyId: IAM_USER_KEY,
secretAccessKey: IAM_USER_SECRET,
Bucket: BUCKET_NAME
});
s3bucket.createBucket(function () {
var params = {
Bucket: BUCKET_NAME,
Key: file.name,
Body: file.data
};
s3bucket.upload(params, function (err, data) {
if (err) {
console.log('error in callback');
console.log(err);
}
console.log('success');
console.log(data);
});
});
}
module.exports = (app) => {
// The following is an example of making file upload with additional body
// parameters.
// To make a call with PostMan
// Don't put any headers (content-type)
// Under body:
// check form-data
// Put the body with "element1": "test", "element2": image file
app.post('/api/upload', function (req, res, next) {
// This grabs the additional parameters so in this case passing in
// "element1" with a value.
const element1 = req.body.element1;
var busboy = new Busboy({ headers: req.headers });
// The file upload has completed
busboy.on('finish', function() {
console.log('Upload finished');
// Your files are stored in req.files. In this case,
// you only have one and it's req.files.element2:
// This returns:
// {
// element2: {
// data: ...contents of the file...,
// name: 'Example.jpg',
// encoding: '7bit',
// mimetype: 'image/png',
// truncated: false,
// size: 959480
// }
// }
// Grabs your file object from the request.
const file = req.files.element2;
console.log(file);
// Begins the upload to the AWS S3
uploadToS3(file);
});
req.pipe(busboy);
});
}
@hari419
Copy link

hari419 commented Oct 11, 2017

var file = req.files.element2;
TypeError: Cannot read property 'element2' of undefined
above error getting when i'm trying to use code...

@keithweaver
Copy link
Author

@SamBellerose
Copy link

SamBellerose commented Oct 23, 2017

I got the following error @keithweaver
Error: params.Body is required
Any idea what this might be? It says that file.data is undefined

And here's what I got when I log the file
{ fieldName: 'file', originalFilename: 'aaaaaaaaaa.png', path: '/var/folders/tb/jt5f67f91x7d60vzm14fnqv40000gn/T/XARw2C-5embUMiEefpiqv8oq.png', headers: { 'content-disposition': 'form-data; name="file"; filename="aaaaaaaaaa.png"', 'content-type': 'image/png' }, size: 102806, name: 'aaaaaaaaaa.png', type: 'image/png' }

@ChrisHomsey
Copy link

I'm able to use this and send a post request with Postman without any issue. I cannot seem to get a simple file upload form to work with it, though. I'm using react and axios. Do you have an example of any front end code that would work with this?

@ashok-sc
Copy link

This definitely doesn't work. The request never completes after the first busboy.on(). This gist only got me running in circles and wasting time.

I would highly recommend using something like https://www.npmjs.com/package/s3fs or https://www.npmjs.com/package/multer-s3.

@cazyjones
Copy link

This works just great. Request never completes cause there is no response in the code. Just console.log.

@debashishdwivedi
Copy link

I am getting the error on this line: const file = req.files.element2; as Property files does not exists on request

@yashSrijan
Copy link

yashSrijan commented Jul 26, 2018

as ashok-sc said the request never completes after the first busboy.on and therefor after two seconds (the default timeout) one more request automatically is generated and i end up having two (same) files with different timestamps in my s3 bucket... what am i supposed to do ???

@AlfredBryan
Copy link

AlfredBryan commented Sep 13, 2018

Use of multer is highly advised it's better

@ramanareddyanamala
Copy link

var file = req.files.element2;
TypeError: Cannot read property 'element2' of undefined
above error getting when i'm trying to use code...

Simply you can "express-fileupload" package to your code
In this way :---------------

const fileUpload = require('express-fileupload');
app.use(fileUpload());

This works for me. Good luck :-)

@menthos984
Copy link

I got the following error @keithweaver
Error: params.Body is required
Any idea what this might be? It says that file.data is undefined

And here's what I got when I log the file
{ fieldName: 'file', originalFilename: 'aaaaaaaaaa.png', path: '/var/folders/tb/jt5f67f91x7d60vzm14fnqv40000gn/T/XARw2C-5embUMiEefpiqv8oq.png', headers: { 'content-disposition': 'form-data; name="file"; filename="aaaaaaaaaa.png"', 'content-type': 'image/png' }, size: 102806, name: 'aaaaaaaaaa.png', type: 'image/png' }

Any updates?

@ChirantanPatel
Copy link

@menthos I Resolved that issues :

npm install express-fileupload
const fileUpload = require('express-fileupload');
app.use(fileUpload());
........................................................
exports.uploadToS3 = function (file) {
var file = file.files; // This line need to write...............................................
let s3bucket = new AWS.S3({
accessKeyId: '',
secretAccessKey: '',
Bucket: '',
});
s3bucket.createBucket(function () {
var params = {
Bucket: '',
Key: file.name,
Body: file.data,
};
s3bucket.upload(params, function (err, data) {
if (err) {
console.log('error in callback');
console.log(err);
}
console.log('success');
console.log(data);
});
});
}

@belly34
Copy link

belly34 commented Aug 21, 2023

I got the following error @keithweaver
Error: params.Body is required
Any idea what this might be? It says that file.data is undefined
And here's what I got when I log the file
{ fieldName: 'file', originalFilename: 'aaaaaaaaaa.png', path: '/var/folders/tb/jt5f67f91x7d60vzm14fnqv40000gn/T/XARw2C-5embUMiEefpiqv8oq.png', headers: { 'content-disposition': 'form-data; name="file"; filename="aaaaaaaaaa.png"', 'content-type': 'image/png' }, size: 102806, name: 'aaaaaaaaaa.png', type: 'image/png' }

Any updates?

i am struggling with same error Error: params.Body is required` did you the answer ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment