pewpewlasers
pewpewlasers

Reputation: 3215

Streaming files directly to Client from Amazon S3 (Node.js)

I am using sails.js and am trying to stream files from the Amazon s3 server directly to the client.

To connect to S3, I use the s3 Module : https://www.npmjs.org/package/s3 This module provides capabilities like client.downloadFile(params) and client.downloadBuffer(s3Params).

My current code looks like the following:

var view = client.downloadBuffer(params);
view.on('error', function(err) {
    cb({success: 0, message: 'Could not open file.'}, null);
});
view.on('end', function(buffer) {
    cb(null, buffer);
});

I catch this buffer in a controller using:

User.showImage( params , function (err, buffer){
    // this is where I can get the buffer
});

Is it possible to stream this data as an image file (using buffer.pipe(res) doesn't work of course). But is there something similar to completely avoid saving file to server disk first?

The other option client.downloadFile(params) requires a local path (i.e. a server path in our case)

Upvotes: 2

Views: 1952

Answers (1)

andrewrk
andrewrk

Reputation: 31152

The GitHub issue contains the "official" answer to this question: https://github.com/andrewrk/node-s3-client/issues/53

Upvotes: 1

Related Questions