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.