Boto3 grabbing only selected objects from the S3 resource
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When working with large S3 buckets, fetching every object and filtering locally is slow and expensive. In Boto3, the efficient approach is narrowing results server-side as much as possible, then applying lightweight client-side filtering for the remaining conditions. This keeps code fast and easier to maintain.
Use Prefix Filtering First
S3 API supports prefix filtering natively through list operations. Always use that first if object keys share meaningful path prefixes.
Prefix filtering reduces transferred metadata and speeds iteration significantly.
Handle Large Listings with Paginators
list_objects_v2 returns at most one thousand objects per request. For real buckets, always use paginators.
This pattern is reliable and memory-friendly for large inventories.
Resource API and Client API Tradeoff
Boto3 resource API can feel more object-oriented, while client API gives explicit control and usually maps closer to AWS documentation.
Resource example:
For simple iteration, resource API is concise. For advanced options, client API is often clearer.
Select by Time, Size, or Pattern
After prefix narrowing, apply additional filters in Python.
This avoids unnecessary downloads when selection can be made from metadata.
Download Only Selected Keys
Once keys are selected, stream only those files.
Keep the selection stage separate from the transfer stage for cleaner logging and retry behavior.
Robust Selection Pipelines with Logging and Retries
In production jobs, object selection should be observable. Log why each object is included or excluded, then persist selected keys for reproducibility.
Persisting this list to a manifest file allows deterministic reruns and easier debugging when downstream processing fails.
For high-volume pipelines, consider S3 Inventory for daily object manifests. Inventory can be more efficient than repeated full-prefix listing in large buckets.
For very high request volume, add request metrics and backoff telemetry so throttling patterns are visible in logs.
Common Pitfalls
A common pitfall is listing entire bucket contents and filtering in memory. This scales poorly and can trigger API throttling.
Another issue is forgetting pagination, which silently misses objects beyond the first page.
Developers also assume wildcard syntax in S3 list APIs. S3 supports prefix filtering, not arbitrary glob matching.
Finally, avoid broad IAM permissions. Restrict list and get permissions to required prefixes when possible.
Summary
- Use prefix filtering first to reduce S3 listing scope.
- Use paginators for complete and scalable iteration.
- Apply additional metadata filters client-side only after narrowing.
- Separate selection and download phases for cleaner workflows.
- Keep IAM permissions scoped to required bucket paths.
Related reading
- boto3 how to create object with metadata?
- Boto3 S3, sort bucket by last modified
- Boto3 to download all files from a S3 Bucket
- Boto3 updating multiple values
- Boto3/S3 Renaming an object using copy_object
- Boto - Uploading file to a specific location on Amazon S3
- Boto 3 DynamoDB batchWriteItem Invalid attribute value type when specifying types
- botocore.exceptions.ClientError An error occurred 404 when calling the HeadObject operation Not Found

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.