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

December 15, 2018 451 views
Python

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
Be the first one to answer this question.