Parse stream to object in Nodejs Parse stream to object in Nodejs javascript javascript

Parse stream to object in Nodejs


I had to parse the JSON after converting to string:

    var fileContents = data.Body.toString();    var json = JSON.parse(fileContents);    console.log(json);


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);    }    });    };