I am trying to read the buckets in an account and save the list to a variable that can be accessed by other functions.
I am trying using a global variable.
Result: Global variable value not retained/saved.
Node: v10 Code:
var AWS = require('aws-sdk');
AWS.config.update({region: 'eu-west-1'});
s3 = new AWS.S3({apiVersion: '2006-03-01'});
let blist;
getBuckets();
console.log("list: " + blist);
async function getBuckets() {
let response = await s3.listBuckets().promise();
// do your processing
var buckets = response.Buckets.map(x=>x.Name);
console.log("buckets: " + buckets);
blist = buckets;
//return bnames;
}
Output:
Output:
(GV) List: undefined (blist)
(LV) Buckets: [all good]
Global variable value is undefined.
Can someone please help me? All I need is to access the list of bucket names from a different function.
s3.listBuckets()does't return text, it returns a request object... which you don't need when invoking it asynchronously, as you are doing. docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/… Yourreturn ndata;in the callback doesn't make sense, because the turn value from callbacks is discarded.