How to close Boto S3 connection?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
With boto3, S3 does not behave like a traditional database connection that you open, use, and close on every request. The client reuses HTTP connections internally, so the real question is when to release client resources and when to close streaming responses such as get_object.
How boto3 manages S3 connections
When you create an S3 client, botocore builds an HTTP-backed client object that handles authentication, retries, and connection pooling. In a short script, it is often fine to create the client, use it, and let the process exit naturally.
Here is a minimal example:
This code works without any explicit cleanup, but long-running applications should think more carefully about resource lifetime.
Session, client, and resource are not the same thing
It helps to separate the three layers in the SDK:
- a
Sessionstores configuration such as region and credentials lookup - a client performs low-level API calls such as
put_objectandget_object - a resource offers a higher-level object interface built on top of clients
When people ask how to "close the S3 connection," they are usually talking about the client or an active response stream. The session itself is mostly configuration state, not a persistent socket that needs constant manual shutdown.
Closing the client explicitly
If you want deterministic cleanup, call close() when the client is no longer needed:
That releases the HTTP resources owned by the client. It is a good pattern for scripts or jobs with a clear end of life.
Closing object streams matters more
The most common place developers forget cleanup is not the client itself, but the streaming body returned by get_object. If you download an object as a stream, close that body after reading it:
If the body stays open, the underlying HTTP connection can remain occupied longer than necessary.
Reuse clients in real applications
In most applications, the best pattern is to create the client once and reuse it rather than opening and closing it for every operation:
This pattern is common in web services, background workers, and AWS Lambda functions. Reuse is faster and creates less connection churn.
When explicit closure is optional
For a short command-line script, explicit closure is often mostly about clarity and neat cleanup. The operating system will reclaim resources when the process exits. The cases where manual cleanup matters most are:
- loops that create many clients
- long-running server processes
- code paths that stream data from S3
Those are the situations where poor cleanup becomes visible as wasted sockets or unnecessary resource pressure.
Common Pitfalls
- Creating a new S3 client inside every helper function instead of reusing one.
- Forgetting to close
response["Body"]afterget_object. - Expecting
close()to fix permission, region, or credential errors. - Treating an S3 client like a stateful database connection that must be reopened constantly.
Summary
- '
boto3S3 clients use HTTP connection pooling under the hood.' - Call
client.close()when you want deterministic cleanup at the end of a client lifecycle. - Always close streaming bodies returned by operations such as
get_object. - In long-running applications, reuse S3 clients instead of recreating them repeatedly.
Related reading
- How to CNAME to Amazon API Gateway Endpoint
- How to configure custom domain name for Amazon ECR
- how to configure eb cli with eb env that is already running
- How to Configure SSL for Amazon S3 bucket
- How to color scatter markers as a function of a third variable
- How to combine multiple QuerySets in Django?
- How to configure VPN connection between 2 Kubernetes clusters
- How to connect Apache Kafka with Amazon S3?

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.