How list Amazon S3 bucket contents by modified date?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The S3 API does not support sorting objects by modification date natively. You must retrieve the object listing and sort it client-side. With the AWS CLI, pipe the output through sort. With Python and Boto3, sort the results list in memory. For large buckets with millions of objects, use S3 Inventory reports instead of repeated API calls to avoid high costs and slow performance.
How S3 Stores Object Metadata
Every object in S3 has a LastModified timestamp that records when the object was created or last overwritten. This timestamp is part of the object's metadata and is returned by both ListObjectsV2 and HeadObject API calls. S3 stores this as a UTC timestamp with millisecond precision.
Important: S3 does not maintain a modification-date index. The ListObjectsV2 API returns objects sorted lexicographically by key (the object's path), not by date. Any date-based sorting must happen after retrieval.
Method 1: AWS CLI
Basic listing with date information
This outputs three columns: modification date, size in bytes, and key name.
Sort by modification date
The sort -k1 flag sorts by the first column (LastModified). Because S3 timestamps are in ISO 8601 format, lexicographic sorting produces correct chronological order.
Most recently modified objects
The -r flag reverses the sort order so the newest objects appear first.
Filter by prefix
Filtering by prefix reduces the number of objects returned, which speeds up both the API call and the client-side sort.
Using aws s3 ls (simpler but less flexible)
The aws s3 ls command outputs date and time in the first two columns. Sorting by both columns (-k1,2) produces chronological order. This is simpler but does not support JMESPath queries for filtering.
Method 2: Python with Boto3
Basic script
Handling pagination for large buckets
list_objects_v2 returns at most 1,000 objects per call. For buckets with more objects, use the paginator:
Filter by prefix and date range
Find the N most recently modified objects
This heap-based approach uses O(N) memory for the top N results instead of O(total_objects) for sorting the entire listing.
Method 3: S3 Inventory (For Very Large Buckets)
For buckets with millions of objects, calling list_objects_v2 repeatedly is slow and expensive. S3 Inventory generates a daily or weekly manifest of all objects, delivered as CSV, ORC, or Parquet files to a destination bucket.
Configure S3 Inventory
Query the inventory with Athena
Once the inventory is delivered, query it with Amazon Athena for fast, indexed lookups:
This is dramatically faster and cheaper than paginating through the list API for buckets with tens of millions of objects.
Comparison of Methods
| Method | Best For | Max Objects | Cost | Sort Support |
| AWS CLI + sort | Quick checks, small buckets | Up to 100K | Standard API pricing | Client-side |
| Boto3 paginator | Automation, scripting, filtering | Up to 1M+ | Standard API pricing | Client-side |
| Boto3 + heapq | Finding top N in large buckets | Any size | Standard API pricing | Heap-based (efficient) |
| S3 Inventory + Athena | Millions of objects | Unlimited | Inventory + Athena pricing | SQL ORDER BY |
IAM Permissions Required
The IAM user or role needs the following permissions:
s3:ListBucket is the permission required for list_objects_v2. It is a bucket-level permission (applied to the bucket ARN without /*), not an object-level permission.
Common Pitfalls
- Assuming the S3 API returns objects sorted by date. It does not. Objects are always returned in lexicographic key order. You must sort client-side.
- Not paginating for large buckets.
list_objects_v2returns at most 1,000 objects per request. Without pagination, you only see the first 1,000 objects (sorted by key), which may not include the objects you are looking for. - Sorting millions of objects in memory. For very large buckets, loading all object metadata into memory and sorting is both slow and expensive. Use S3 Inventory with Athena instead.
- Confusing
LastModifiedwith upload date for multipart uploads. For multipart uploads,LastModifiedreflects when the multipart upload was completed, not when it was initiated. - Using
s3:GetObjectinstead ofs3:ListBucket. Listing bucket contents requires thes3:ListBucketpermission on the bucket resource, nots3:GetObjecton the objects. - Ignoring API costs. Each
list_objects_v2call is a LIST request. At $0.005 per 1,000 requests, listing a bucket with 10 million objects costs about $50 per full scan. S3 Inventory is much cheaper for repeated analysis.
Summary
- S3 does not support server-side sorting by modification date. All sorting must happen client-side.
- For quick checks, use
aws s3api list-objects-v2piped throughsort -k1. - For automation, use Boto3 with the paginator to iterate through all objects and sort in Python.
- For finding the top N most recent objects efficiently, use a heap to avoid sorting the entire listing.
- For buckets with millions of objects, configure S3 Inventory and query the results with Athena.
- Always paginate when listing objects. The API returns at most 1,000 objects per call.

