Error: read ECONNRESET ExpressJS and Amazon S3

2.3k views Asked by At

Im using Knox S3 plugin in ExpressJS web application to display image uploaded in Amazon S3. When displaying the image, I sometimes have the error below. I don't have idea on the error. What caused the error?

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: read ECONNRESET
    at errnoException (net.js:884:11)
    at TCP.onread (net.js:539:19)

This is how I render the image from Amazon S3:

var data = '';
client.get(url).on('response', function(s3res) {
    s3res.setEncoding('binary');
    s3res.on('data', function(chunk){
        data += chunk;
    });
    s3res.on('end', function() {
        res.contentType('image/jpg');
        res.write(data, encoding='binary');
        res.end();
    });
}).end();
1

There are 1 answers

0
Emil Condrea On

Since drain event can happen for a stream, it is possible that you are writing faster than the other stream is capable of reading. You should use pipe that handles this for you:

s3res.pipe(response);

http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options

This is from nodejs documentation: This method pulls all the data out of a readable stream, and writes it to the supplied destination, automatically managing the flow so that the destination is not overwhelmed by a fast readable stream.