0

I am trying out NodeJS server app for getting AWS S3 bucket file (object) names list and return as HTTP response text.

below is the full code I wrote.

var AWS = require('aws-sdk');
var http = require("http");

function onRequest(request, response) {
    response.writeHead(200, {
        'Content-Type': 'application/json'
    });
    var credentials = new AWS.SharedIniFileCredentials();
    AWS.config.credentials = credentials;
    AWS.config.logger = console;
    var str = "";
    var s3 = new AWS.S3();
    var params = {
        Bucket: 'bucketname',
        Delimiter: '/',
        Prefix: 'path/to/the/folder'
    }
    //var data = s3.listObjectsV2(params);   returns data as [object Object]

    s3.listObjects(params, function(err, data) {
        console.log("Data : " + data);
        for (var i = 0; i < data.Contents.length; i++) {
            var tokens = data.Contents[i].Key.split("/");
            str = str + tokens[tokens.length - 1];
            if (i != data.Contents.length) {
                str = str + ",";
            }
        }
        console.log("Final text:" + str);
    });

    response.write(str);
    response.end();
}

http.createServer(onRequest).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');'

Problem: Due to asynchronous call to listObjects my HTTP response writing ends before I get callback value.

Can anyone help me to make it synchronous so that I can add str as part of the response body?

Thanks in advance.

Vikas Thange
  • 230
  • 2
  • 13

2 Answers2

1

You can just put res.write and res.end inside the callback that will work:

var AWS = require('aws-sdk'); var http = require("http");
function onRequest(request, response) { 
    response.writeHead(200, { 'Content-Type': 'application/json' }); 
    var credentials = new AWS.SharedIniFileCredentials(); 
    AWS.config.credentials = credentials; 
    AWS.config.logger = console; 
    var str = ""; 
    var s3 = new AWS.S3(); 
    var params = { Bucket: 'bucketname', Delimiter: '/', Prefix: 'path/to/the/folder' } 
    //var data = s3.listObjectsV2(params); 
    returns data as [object Object]

    s3.listObjects(params, function(err, data) {
        console.log("Data : " + data);
        for (var i = 0; i < data.Contents.length; i++) {
            var tokens = data.Contents[i].Key.split("/");
            str = str + tokens[tokens.length - 1];
            if (i != data.Contents.length) {
                str = str + ",";
            }
        }
        console.log("Final text:" + str);

        response.write(str); // And putting response write() and end() here
        response.end();
    });

    // taking off write() and end() from here

}

http.createServer(onRequest).listen(8081); 
console.log('Server running at http://127.0.0.1:8081/');
Jair
  • 96
  • 4
0

You only need response.end(str), and it can go inside the callback. Node will keep the HTTP stream open until the S3 call returns.

ouni
  • 3,233
  • 3
  • 15
  • 21