Reading a file from a private S3 bucket to a pandas dataframe
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Reading directly from a private S3 bucket into pandas is a common data-engineering task. The important detail is that "private" changes the authentication path, not the pandas logic: you still download bytes from S3, then hand those bytes to the appropriate pandas reader.
The Direct boto3 Approach
The most explicit solution is to use boto3 to fetch the object and wrap the response body in a file-like buffer. This works well when you want full control over credentials, region, and error handling.
This pattern is simple and reliable. get_object returns a streaming body, and BytesIO gives pandas something file-like to read from.
Credentials and Access Control
Because the bucket is private, the identity behind boto3 must have permission to fetch the object. In AWS terms, that usually means the active user or role needs s3:GetObject on the relevant bucket path.
The cleanest way to provide credentials is usually one of these:
- an IAM role attached to the compute environment
- a named AWS profile on your machine
- environment variables supplied by your runtime or CI system
Avoid hardcoding access keys in the script. That solves the immediate problem but creates a worse long-term security problem.
Reading Other File Formats
Once you have the bytes, the pandas side depends on the file type. For JSON you might use pd.read_json, and for Parquet you can use pd.read_parquet.
The authentication step stays the same. Only the reader changes.
When to Use s3fs Instead
Some teams prefer using an S3 URL directly with pandas, often through s3fs. That can be convenient:
This feels compact, but it adds another dependency layer and can hide credential behavior that is easier to debug when you call boto3 yourself. For production pipelines, many engineers prefer the explicit get_object route because failures are clearer.
Common Pitfalls
The most common problem is missing IAM permission. If the code works for one bucket and not another, check the policy first. Private buckets fail fast when s3:GetObject is missing or scoped to the wrong prefix.
Another issue is loading large files fully into memory. The BytesIO approach is fine for moderate files, but huge CSV files can exhaust RAM. In that case, consider chunked reads with chunksize, Parquet, or a different processing design.
Be careful with the object key. S3 keys are exact strings, not directory objects in the filesystem sense. A small typo in the prefix or file name produces a NoSuchKey error.
Finally, do not assume bucket privacy and network accessibility are the same problem. You can have correct IAM permissions and still fail because of VPC endpoint policy, corporate proxy rules, or region misconfiguration.
Summary
- Use
boto3.get_objectto fetch bytes from a private S3 object. - Wrap the body in
BytesIOand pass it to the correct pandas reader. - Make sure the active AWS identity has
s3:GetObjectaccess. - Prefer IAM roles, profiles, or environment-based credentials over hardcoded keys.
- Watch memory usage when files are large and choose a more scalable format or read pattern when needed.
Related reading
- Real world typo statistics?
- Reasonable optimized chart scaling
- Recommendation engine without ratings
- Recommendations for using graphs theory in machine learning?
- Reading a JSON file from S3 using Python boto3
- reading a packaged file in aws lambda package
- Reading binary file and looping over each byte
- Reading contents of a gzip file from a AWS S3 in Python

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.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.