web-dev-qa-db-fra.com

Parse Stream à l'objet de Nodejs

J'essaie Obtenez un objet d'Amazon S3 Storage dans node.js.

Et cela fonctionne parfaitement lorsque je le sauve dans un fichier.

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

};

Dans. helloaa.json est:

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

Mais. Je ne veux pas écrire des données dans le fichier sur mon disque.

Je veux analyser ce Json à l'objet avec json.parse ();

Quand j'imprime un objet avec:

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

Dans la console est-ce:

{"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":

Qu'est-ce que c'est?

Comment puis-je l'analyser?

Est-ce flux?

Puis-je enregistrer le flux à l'objet de Nodejs?

29
Samuel Ondrek

Avez-vous essayé data.Body.toString()?

53
hunterloftis

J'ai dû analyser le Json après la conversion en chaîne:

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