Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

s3 file upload does not return response

I'm using the Node AWS-SDK to upload files to an existing S3 bucket. With the code below, the file eventually uploads but it seems to return no status code a couple of times. Also, when the file successfully uploads, the return statement does not execute.

Code

exports.create = function(req, res) {
	var stream = fs.createReadStream(req.file.path);
	var params = {
		Bucket: 'aws bucket',
		Key: req.file.filename,
		Body: stream,
		ContentLength: req.file.size,
		ContentType: 'audio/mp3'
	};
	var s3upload = s3.upload(params, options).promise();
	
	s3upload
		.then(function(data) {
			console.log(data);
			return res.sendStatus(201);
		})
		.catch(function(err) {
			return handleError(err);
		});
}

Logs

POST /api/v0/episode/upload - - ms - -
POST /api/v0/episode/upload - - ms - -
{ Location: 'https://krazykidsradio.s3-us-west-2.amazonaws.com/Parlez-vous%2BFrancais.mp3',
  Bucket: 'krazykidsradio',
  Key: 'Parlez-vous+Francais.mp3',
  ETag: '"f3ecd67cf9ce17a7792ba3adaee93638-11"' }
like image 581
Patrick San Juan Avatar asked May 06 '26 00:05

Patrick San Juan


1 Answers

Also, when the file successfully uploads, the return statement does not execute.

No value is returned from create() call, see Why is value undefined at .then() chained to Promise?

exports.create = function(req, res) {
    var stream = fs.createReadStream(req.file.path);
    var params = {
        Bucket: 'aws bucket',
        Key: req.file.filename,
        Body: stream,
        ContentLength: req.file.size,
        ContentType: 'audio/mp3'
    };
    var s3upload = s3.upload(params, options).promise();
    // return the `Promise`
    return s3upload
        .then(function(data) {
            console.log(data);
            return res.sendStatus(201);
        })
        .catch(function(err) {
            return handleError(err);
        });
}
like image 152
guest271314 Avatar answered May 07 '26 13:05

guest271314



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!