Created
September 10, 2018 07:14
-
-
Save longsangstan/db7f43c98d41ad0bb9d27d891620dbbe to your computer and use it in GitHub Desktop.
List all objects in s3
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 s3 = require("../aws").s3; | |
/** | |
* List all objects in the specified S3 folder. | |
* @param {String} Bucket | |
* @param {String} Prefix | |
* @param {String} NextContinuationToken | |
* @param {Array} PreviousContents | |
* @returns {Promise<Array>} A promise containing an array of all objects in the folder | |
*/ | |
async function s3ListAllObjects( | |
Bucket, | |
Prefix, | |
NextContinuationToken, | |
PreviousContents | |
) { | |
const params = { | |
Bucket: Bucket, | |
Prefix: Prefix, | |
ContinuationToken: NextContinuationToken | |
}; | |
try { | |
const res = await s3.listObjectsV2(params).promise(); | |
const Contents = PreviousContents | |
? PreviousContents.concat(res.Contents) | |
: res.Contents; | |
if (res.NextContinuationToken) { | |
// console.log("NextContinuationToken: " + res.NextContinuationToken); | |
// console.log("Contents Length: " + Contents.length); | |
return s3ListAllObjects( | |
Bucket, | |
Prefix, | |
res.NextContinuationToken, | |
Contents | |
); | |
} else { | |
// console.log("All Contents Length: " + Contents.length); | |
// console.log("All Contents: " + JSON.stringify(Contents)); | |
return Contents; | |
} | |
} catch (e) { | |
console.log("e: " + e); | |
} | |
} | |
module.exports = s3ListAllObjects; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment