Streaming a binary file from riak via node.js to client

francisco treacy francisco.treacy at gmail.com
Sat May 21 11:17:55 EDT 2011


(I'm CC'ing the mailing list so that everybody can benefit from this)

That's right. You need to include the responseEncoding.

db.get('image', 'test.jpg', { responseEncoding: 'binary' });

I recommend you take a look at the docs. It's all there. http://riakjs.org

Francisco


2011/5/21 Max <nash12 at gmail.com>:
> Francisco,
>
> I tried fetching the image content using a plain http GET request
> without the riak client.
>
> This code worked:
>
> app.get('/images-test', function(req, res) {
>    var options = {
>      host: '127.0.0.1',
>      port: 8010,
>      path: '/riak/images/some.jpg',
>      method: 'GET'
>    };
>
>    var req = http.request(options, function(r) {
>      var str = '';
>
>      r.setEncoding('binary');
>      r.on('data', function (chunk) {
>          str += chunk;
>      });
>      r.on('end', function() {
>          res.send(new Buffer(str, 'binary'), { 'Content-Type': 'image/jpeg' });
>      });
>    });
>
>    req.end();
> });
>
> As soon as I removed r.setEncoding('binary'); the image size was wrong
> again. How can I set the response encoding with the riak client?
>
>
> On Sat, May 21, 2011 at 2:25 PM, Max <nash12 at gmail.com> wrote:
>> Francisco, thanks a lot for trying this out and helping me.
>>
>> I checked that image is a Buffer, I tried console.log(image instanceof
>> Buffer); and the output was "true". Any other ideas on how I could do
>> what I´m trying? I built riak from source, if that makes any
>> difference.
>>
>> Thank you!
>>
>> On Sat, May 21, 2011 at 2:18 PM, francisco treacy
>> <francisco.treacy at gmail.com> wrote:
>>> It works fine for me under three scenarios:
>>>
>>> (a) your example, with riak 0.14, riak-js 0.4 and node 0.4.7
>>> (b) riak-js tests (fixture is an image)
>>> (c) my own app that does exactly that for certain images
>>>
>>> The only thing that differs from your example is that i'm using the
>>> Luwak interface (getLarge instead of get).  So last thing i can think
>>> of is that the image variable is not a Buffer.  Could you check that?
>>>
>>> Francisco
>>>
>>>
>>> 2011/5/21 Max <nash12 at gmail.com>:
>>>> Hi Francisco,
>>>>
>>>> thanks for your reply. The versions I use:
>>>>
>>>> riak: 0.14
>>>> riak-js: 0.4
>>>> node: 0.4.7
>>>>
>>>> Thank you!
>>>>
>>>> On Sat, May 21, 2011 at 1:01 PM, francisco treacy
>>>> <francisco.treacy at gmail.com> wrote:
>>>>> and which node.js version?
>>>>>
>>>>>
>>>>> 2011/5/21 francisco treacy <francisco.treacy at gmail.com>:
>>>>>> That should work, but i'll take a look at it. Which versions of
>>>>>> riak-js and Riak are you using?
>>>>>>
>>>>>>
>>>>>> 2011/5/21 Max <nash12 at gmail.com>:
>>>>>>> After watching the presentation about how to use node.js with riak, I
>>>>>>> built some small application on node.js using the express framework.
>>>>>>>
>>>>>>> In riak files are stored via a PHP frontend but they should be served
>>>>>>> via a node.js backend. Anyway, connecting to riak via node.js is not
>>>>>>> that hard and sending the response is also rather easy. But it seems,
>>>>>>> somewhere the buffer I get via the client.get operation is truncated.
>>>>>>> I am using this code:
>>>>>>>
>>>>>>> app.get('/images/', function(req, res){
>>>>>>>    riakClient.get('images', 'some.jpg', function(err, image, meta) {
>>>>>>>        if (err) {
>>>>>>>            throw err;
>>>>>>>        }
>>>>>>>
>>>>>>>        res.send(image, {
>>>>>>>            'Content-Type': meta.contentType
>>>>>>>        });
>>>>>>>    });
>>>>>>> });
>>>>>>>
>>>>>>> To test if the image is served, but whenn I access the node.js url,
>>>>>>> the browser tells me that the image cannot be displayed as it contains
>>>>>>> errors. The filesize of the served image is too small compared to the
>>>>>>> original. If I access the url /images/some.jpg directly via rest
>>>>>>> interface on the command line, the correct image is returned.
>>>>>>>
>>>>>>> Has anyone experience in how to stream binary data from riak?
>>>>>>>
>>>>>>> Thanks in advance!
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> riak-users mailing list
>>>>>>> riak-users at lists.basho.com
>>>>>>> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>




More information about the riak-users mailing list