Question

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

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

Submit an answer

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!

Sign In or Sign Up to Answer