Trying to download a file from aws s3 into nodejs writeStream
<p>I am trying to download a file from s3 and directly put into into a file on the filesystem using a writeStream in nodejs. This is my code:</p>

<pre><code>downloadFile = function(bucketName, fileName, localFileName) {
    //Donwload the file
    var bucket = new AWS.S3({
        params: { Bucket: bucketName },
        signatureVersion: 'v4'
    });
    var file = require('fs').createWriteStream(localFileName);
    var request = bucket.getObject({ Key: fileName });
    request.createReadStream().pipe(file);
    request.send();
    return request.promise();
}
</code></pre>

<p>Running this function I get this error:</p>

<pre><code>Uncaught Error: write after end
</code></pre>

<p>What is happening? Is the file closed before the write is finished? Why?</p>