Skip to content Skip to sidebar Skip to footer

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,

Solution 1:

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) {
            returnhandleError(err);
        });
}

Solution 2:

I figured it out. The request timeout was not long enough for the upload to finish, thus it was making the call again and so on and so on. To resolve the issue, I set the timeout for the request to 0, giving the request all the time it needs to finish the upload. With this in place, it properly returns a 201 response back to the client.

exports.create = function(req, res) {
    req.setTimeout(0); // <= set a create request to no timeout length.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`
    s3upload
        .then(function(data) {
            console.log(data);
            return res.sendStatus(201);
        })
        .catch(function(err) {
            returnhandleError(err);
        });
}

Post a Comment for "S3 File Upload Does Not Return Response"