Report this

What is the reason for this report?

Should I create a new instance of boto3 client for each file upload request, or use a shared instance

Posted on December 15, 2018

I plan to purchase Space for storage purpose, and Droplet to run app code.

App code will perform authentication, uploading and downloading operations.

However, I have some doubt on the correct way to write App code. I’m using Flask + Boto3

I was wondering, should I create a new instance of boto3 client for each file upload request, or use a shared instance? Which is the correct way to do so?

Create a new instance each upload request

@app.route('/', methods=['POST'])
def upload_file():
    file = request.files['file']

    file.save('/tmp/file.ext')

    client = boto3.client('s3')

    client.upload_file('/tmp/file.ext',  # Path to local file
        'my-space',  # Name of Space
        'file.ext')  # Name for remote file

Shared instance for each upload request

client = boto3.client('s3')


@app.route('/', methods=['POST'])
def upload_file():
    file = request.files['file']

    file.save('/tmp/file.ext')

    client.upload_file('/tmp/file.ext',  # Path to local file
        'my-space',  # Name of Space
        'file.ext')  # Name for remote file


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

In most cases, reusing the Boto3 client instance (i.e., the second approach) is the preferred method. The Boto3 library is designed to be thread-safe and to manage its own internal connection pooling, so you don’t need to worry about creating a new instance for each request. From the docs:

Unlike Resources and Sessions, clients are generally thread-safe. There are some caveats, defined below, to be aware of though.

This practice has several benefits:

  1. Performance: Creating a new client for every request can add significant overhead, especially in a highly concurrent environment. Reusing the same client instance allows the underlying HTTP connection to be reused, which can result in substantial performance improvements.

  2. Resource utilization: Each new client instance consumes resources (e.g., memory, file descriptors for connections). In a high-throughput environment, creating a new client for each request can quickly exhaust available resources.

However, if you are processing requests with differing configurations (different access keys, regions, etc.), you might have to instantiate a new client for each of those configurations.

In conclusion, unless there’s a specific need for different configurations, it’s generally better and more efficient to reuse the Boto3 client.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.