title: AWS Lambda to list S3 bucket objects and subdirectories inside with file versioning
body: <p>I'm new to lambda and trying to list S3 bucket objects that has nested subdirectories , here is the structure:</p>
  
  <pre><code>- mybucket/folder1/project1/samplev1.txt
  - mybucket/folder1/project1/sampleVer2.txt
  - mybucket/folder2/
  - mybucket/folder3/
  </code></pre>
  
  <p>here is my lambda code:</p>
  
  <pre><code>const AWS = require('aws-sdk');
  const s3 = new AWS.S3();
  
  exports.handler = async (event) =&gt; {
    const allKeys = [];
    await getKeys({ Bucket: 'ru-mybucket' }, allKeys);
    console.log(allKeys)
    return allKeys;
  };
  
  async function getKeys(params, keys){
    const response = await s3.listObjectsV2(params).promise();
    response.Contents.forEach(obj =&gt; keys.push(obj.Key));
  
    if (response.IsTruncated) {
      const newParams = Object.assign({}, params);
      newParams.ContinuationToken = response.NextContinuationToken;
      await getKeys(newParams, keys); // RECURSIVE CALL
    }
  }
  </code></pre>
  
  <p>The function list all objects keys inside my bucket with their nested subdirectories and files, The problem is how to list nested objects including their version, how can I achieve this? where to add (listObjectVersions) in my code to able loop over the objects which has versions?</p>