11/18/2020 5:43:52 PM

Due to it's asynchronous nature, if you are new to NodeJS, you might find it difficult to do a simple thing such as listing keys in an S3 bucket and iterating over the results. Below are some examples of how you can use the AWS S3 sdk to pull S3 objects. These example are being run within an AWS Lambda async function.

const AWS = require('aws-sdk'); const s3 = new AWS.S3(); let all_keys = []; let bucket_name = 'my-great-bucket'; //optional let key_prefix = 'files/images/'; exports.handler = async(event) => { var continuation_token = null; do { var s3_params = { Bucket: bucket_name, Prefix: key_prefix, MaxKeys: 1000, ContinuationToken: continuation_token }; //get the results //call the aws sdk promise() method, await the call var promise_obj_awaited = await s3.listObjectsV2(s3_params).promise(); for (var x = 0; x < promise_obj_awaited.Contents.length; x++) { //console.log(promise_obj_awaited.Contents[x].Key); //do something with each key all_keys.push(promise_obj_awaited.Contents[x].Key); } if (promise_obj_awaited.IsTruncated == false) { break } continuation_token = promise_obj_awaited.NextContinuationToken; } while (continuation_token != ''); console.log("count keys found", all_keys.length); // TODO implement const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; return response; };