Parse stream to object in Nodejs
Asked Answered
P

3

31

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?

Palaeozoic answered 31/12, 2012 at 0:20 Comment(0)
I
60

Have you tried data.Body.toString()?

I answered 31/12, 2012 at 3:21 Comment(2)
Yes! It works! Thank you. Can you explain it? I don't understand behavior.Palaeozoic
Glad it worked :) just a hunch, I haven't done that with the s3 library myself, but your output looks like a Buffer - more info here: nodejs.org/api/buffer.htmlI
C
19

I had to parse the JSON after converting to string:

    var fileContents = data.Body.toString();
    var json = JSON.parse(fileContents);
    console.log(json);
Complemental answered 15/3, 2016 at 23:49 Comment(0)
H
0

Convert the parsed response to base64 and then it can be used directly as a source in the image tag of HTML. Lambda call can be made by Amazon REST API

const AWS = require('aws-sdk');
//*/ get reference to S3 client 
var s3 = new AWS.S3();
exports.handler = (event, context, callback) => {
    var params = {
  "Bucket": "bucket-name",
  "Key": "object-name"  
    };
    s3.getObject(params, function(err, data){
       if(err) {
           callback(err, null);
       } else {
           let image = new Buffer(data.Body).toString('base64');
           image = "data:"+data.ContentType+";base64,"+image;
           let response = {
        "statusCode": 200,
        "headers": {
            "Access-Control-Allow-Origin": "*",
            'Content-Type': data.ContentType
        },
        "body":image,
        "isBase64Encoded": true
    };
           callback(null, response);
    }
    });
    
};
Helfant answered 1/1, 2021 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.