Interview Prep/Python for DevOps/AWS automation with boto3

A script that lists all S3 objects in a bucket reports 1,000 objects, but the console says there are 48,000. What happened, and how do you fix the script?

BeginnerScenarioAWS automation with boto3
Answer

Situation read → list_objects_v2 returns at most 1,000 keys per call; the script read one page.

What I'd do →

  1. Replace the single call with a paginator:
total = 0
for page in s3.get_paginator("list_objects_v2").paginate(Bucket=bucket, Prefix=prefix):
    total += page.get("KeyCount", 0)
  1. Audit other scripts for the same bug on any list_*/describe_* call.
  2. For counts and sizes across huge buckets, use S3 Inventory or Storage Lens instead of listing.

Why → Every AWS list API is paginated; one page is not the answer.

Interview tip → Say "pagination bugs are silent: no error, just wrong data".

All Python for DevOps questions Roadmap: Python for automation

More on AWS automation with boto3