Create a paginator for the S3 list_objects_v2 operation so you can list all objects in a bucket across multiple pages.
paginator=s3.get_paginator("list_objects_v2")
import boto3import jsondef get_bucket_size(bucketname, region = "us-west-1"): s3 = boto3.client("s3") # A paginator automatically fetches "next pages" when results are > 1000 objects paginator = s3.get_paginator("list_objects_v2") # Each `page` is one API response page (up to 1000 objects) for page in paginator.paginate(Bucket=bucketname): # print(json.dumps(page, default=str, indent=2)) content = page.get("Contents", []) print(json.dumps(content, default=str, indent=2))