Skip to content
Advertisement

How to upload a file from a Flask’s HTML form to an S3 bucket using python?

I have an HTML form (implemented in Flask) for uploading files. And I want to store the uploaded files directly to S3.

The relevant part of the Flask implementation is as follows:

JavaScript

I then use boto3 to upload the file to S3 as follows:

JavaScript

file is a werkzeug.datastructures.FileStorage object.

But I get the following error when uploading the file to S3:

botocore.exceptions.ClientError: An error occurred (BadDigest) when calling the PutObject operation (reached max retries: 4): The Content-MD5 you specified did not match what we received.

How can I upload the file to S3?

Advertisement

Answer

Since you are using Flask web framework, the file variable is of type werkzeug.datastructures.FileStorage.

I think the problem is that the put() method expects a byte sequence or a file-like object as its Body argument. So, it does not know what to do with the Flask’s FileStorage object.

A possible solution could be to use file.read() to get a sequence of bytes and then pass it to put():

JavaScript
Advertisement