← All posts
2 min read

Control S3 listing pagination

#aws#cli#s3#storage
📑 On this page

Part 40 of AWS from Zero. This lesson keeps the scope to one S3 behavior you can verify from the terminal.

What we are learning

The AWS CLI can make multiple service calls automatically. CLI pagination options control how much the CLI returns and provide a continuation token.

Before you run it

aws sts get-caller-identity
REGION="ap-south-1"
BUCKET="replace-with-your-private-demo-bucket"

Use a private general purpose bucket that you own. Replace every placeholder before running a write or delete command.

The command

aws s3api list-objects-v2 \
  --bucket "$BUCKET" \
  --max-items 5

A successful configuration command may return no output. Treat inspection as a separate required step.

Inspect the result

TOKEN=$(aws s3api list-objects-v2 \
  --bucket "$BUCKET" \
  --max-items 5 \
  --query NextToken \
  --output text)
echo "$TOKEN"

A non-empty NextToken means more CLI results remain.

One tiny variation

aws s3api list-objects-v2 \
  --bucket "$BUCKET" \
  --max-items 5 \
  --starting-token "$TOKEN"

Use the CLI's NextToken as --starting-token; do not substitute an unrelated service token field.

Common mistake

Do not combine pagination flags blindly in automation. --page-size controls service-call size, while --max-items controls the total items returned by the CLI.

Cleanup

# This lesson does not require an additional persistent resource.
aws s3api head-bucket --bucket "$BUCKET"

Keep the shared demo bucket for the next lesson, or remove only the configuration and objects created here.

Next, we will learn Count S3 objects and total their size.

Official AWS CLI reference