Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse stream to object in Nodejs

I am trying Get an object from Amazon S3 storage in Node.Js.

And this perfectly works when I am saving it to a file.

amazon.getObject = function () {      var options = {         BucketName : 'mybucket',         ObjectName : 'path/to/my.json',         ResponseContentType : 'application/json'     };      s3.GetObject(options, function(err, data) {         var fs = require('fs');         var fd = fs.openSync('helloaa.json', 'w+');         fs.writeSync(fd, data.Body, 0, data.Body.length, 0);         fs.closeSync(fd);     });  }; 

In. helloaa.json is:

{     "hello": 1,     "world": 3 } 

But. I don't want to write data to file on my disk.

I want parse this json to object with JSON.parse();

When I print object there with:

    s3.GetObject(options, function(err, data) {         console.log(JSON.stringify(data));     }); 

In console is this:

{"StatusCode":200,"Headers":{"x-amz-id-2":"N1gDLPam+fDCLWd9Q2NI62hizH7eXAjg 61oLYOkanLoSlqUlDl6tqasbfdQXZ","x-amz-request-id":"C53957DAF635D3FD","date" :"Mon, 31 Dec 2012 00:11:48 GMT","last-modified":"Sun, 30 Dec 2012 23:22:57        "etag":"\"8677a54c9b693bb6fc040ede8cc6a\"","accept-ranges":"bytes","co ntent-type":"application/json","content-length":"176","server":"AmazonS3"}, "Body":{"0":123,"1":10,"2":32,"3":32,"4":32,"5":32,"6":34,"7":105,"8":100," 9":34,"10":58,"11":32,"12":49,"13":44,"14":10,"15":32,"16":32,"17":32,"18": 

What is it?

How can I parse it?

Is it stream?

Can I save stream to object in NodeJs?

like image 225
Samuel Ondrek Avatar asked Dec 31 '12 00:12

Samuel Ondrek


People also ask

What is stream PassThrough?

PassThrough. This Stream is a trivial implementation of a Transform stream that simply passes the input bytes across to the output.

What is a Readstream?

A readable stream is an abstraction for a source from which data can be consumed. An example of that is the fs. createReadStream method. A writable stream is an abstraction for a destination to which data can be written.

What is stream chaining in node JS?

Chaining is a mechanism to connect the output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations. Now we'll use piping and chaining to first compress a file and then decompress the same.


1 Answers

Have you tried data.Body.toString()?

like image 123
hunterloftis Avatar answered Sep 20 '22 17:09

hunterloftis