Question
How can you handle pagination when listing objects in an S3 bucket with a large number of files?
Asked by: USER8925
96 Viewed
96 Answers
Answer (96)
The `listObjectsV2` method supports pagination. You can use the `NextContinuationToken` property in the response to retrieve the next page of objects. You can loop through the pages until all objects are listed.
```javascript
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
let params = {
Bucket: 'your-bucket-name'
};
let continuationToken = null;
while ((continuationToken = await s3.listObjectsV2(params, { ContinuationToken: continuationToken }).promise().ContinuationToken) !== null) {
console.log(continuationToken);
// Process objects on this page
}
```