Last active
March 19, 2021 13:13
-
-
Save HoraceShmorace/0801578559b1d9fa104d1baa2656a498 to your computer and use it in GitHub Desktop.
A simple function to list all S3 objects in a given bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk') | |
const s3 = new AWS.S3({ apiVersion: '2006-03-01', region: 'us-east-1' }) | |
const params = { | |
Bucket: 'YOUR_BUCKET_NAME' | |
} | |
/** | |
* `s3.listObjects` has a max result set size of 1000. This function iterates to list all objects in the bucket. | |
* | |
* @example | |
* const objectList = listObjects() | |
* | |
* @example | |
* // Or inside an async function: | |
* const objectList = await listObjects() | |
* | |
* @returns {Promise} A promise, which resolves to an array or zero or more objects. | |
*/ | |
async function listObjects (lastData = []) { | |
const res = await s3.listObjects(bucketParams).promise() | |
const data = [...lastData, ...res.Contents] | |
if (res.IsTruncated && (res.NextMarker || res.Contents.length > 0)) { | |
params.Marker = res.NextMarker || res.Contents[res.Contents.length - 1].Key | |
return listObjects(objects) | |
} | |
return data | |
} | |
// usage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment