Search code examples
node.jsamazon-web-servicesknox-amazon-s3-client

How to consume response from Knox module?


I am using the Knox S3 module, but when I eventually get the file, the resulting file is corrupt. Am I using Knox incorrectly?

        var data;
        client.getFile(path, function(err, file) {
            file.on('data', function(chunk) { data += chunk; });
            file.on('end', function() {
                //Here I end up sending the response with new Buffer(data), but that produces a bad file.
            });
        });

Solution

  • Try using the writeStream:

    var fs = require('fs');
    var file = fs.createWriteStream(path);
    client.getFile(path, function(err, stream) {
        stream.on('data', function(chunk) { file.write(chunk); });
        stream.on('end', function(chunk) { file.end(); });
    });
    

    and make sure to take a look at https://github.com/aws/aws-sdk-js