Got Error: Stream yields empty buffer when resizing images from S3 with local node service
<p>I have a really similar approach as: <a href="https://stackoverflow.com/q/28741131/3781320">Resizing image with nodeJs and AWS</a>. And I still get the same error when I run my service locally on Windows 10. It works on lambda cloud flawless. I found this discussion about how limited memory or limited timeout could throw an <code>stderr</code> in <code>gm</code>: <a href="https://forums.aws.amazon.com/thread.jspa?threadID=174231" rel="nofollow noreferrer">Stream yields empty buffer error when processing large image files using gm</a>. Besides, this discussion also mentioned about how 
limited memory will make your lambda service vulnerable and not robust: <a href="https://stackoverflow.com/q/30876345/3781320">In amazon lambda, resizing multiple thumbnail sizes in parallel async throws Error: Stream yields empty buffer</a>. However, I'm running this node service locally with plenty of idle memory, I shouldn't run into this "limited memory" trap all the time. I'm thinking this problem is from <code>gm</code>. And here is my code:</p>

<pre><code>async.forEachOf(_sizesArray, function(value, key, callback) {
        async.waterfall([

            function download(next) {
                s3.getObject({
                    Bucket: srcBucket,
                    Key: srcKey
                }, next);
            },
            function convert(response, next) {
                console.log(response.Body);
                gm(response.Body,srcKey).antialias(true).density(
                    300).toBuffer('JPG', function(err,
                    buffer) {
                    if (err) {
                        console.log(err + "\n\nfrom convert\n");
                        next(err);
                    } else {
                        next(null, buffer);
                    }
                });
            },
            function process(response, next) {
                gm(response).size(function(err, size) {
                    var scalingFactor = Math.min(
                        _sizesArray[key].width /
                        size.width, _sizesArray[
                            key].width / size.height
                    );
                    var width = scalingFactor *
                        size.width;
                    var height = scalingFactor *
                        size.height;
                    var index = key;
                    this.resize(width, height).toBuffer(
                        'JPG', function(err,
                            buffer) {
                            if (err) {
                                console.log(err+"\n\nfrom process\n");
                                next(err);
                            } else {
                                next(null,buffer,key);
                            }
                        });
                });
            },
            function upload(data, index, next) {
                s3.putObject({
                    Bucket: dstBucket,
                    Key: myPath + "/" + fileName.slice(0, -4) + 
                        _sizesArray[index].suffix +
                        ".jpg",
                    Body: data,
                    ContentType: 'JPG'
                }, next);
            }
        ], function(err, result) {
            if (err) {
                console.error(err);
            }
            console.log("End of step " + key);
            callback();
        });
    }, function(err) {
        if (err) {
            console.error('Unable to resize ' + srcBucket +
                '/' + srcKey + ' and upload to ' + dstBucket +
                myPath + '/' + ' due to an error: ' + err);
        } else {
            console.log('Successfully resized ' + srcBucket +
                ' and uploaded to ' + dstBucket + '/' + myPath + '/');
        }
        cb(myPath+"/"+fileName);
    });
</code></pre>

<p>To be more specific, my <code>async.waterfall()</code> function will be exited when executing function <code>convert(res,next)</code> and <code>toBuffer()</code> will throw the error I mentioned in the question title. </p>