upload file using flask to amazon s3 upload file using flask to amazon s3 flask flask

upload file using flask to amazon s3


Here is a fully-functioning example of how to upload multiple files to Amazon S3 using an HTML file input tag, Python, Flask and Boto.'

The main keys to making this work are Flask's request.files.getlist and Boto's set_contents_from_string.

Some tips:

  • Be sure to set S3 bucket permissions and IAM user permissions, or the upload will fail. The details are in the readme.
  • Don't forget to include enctype="multipart/form-data" in your HTML form tag.
  • Don't forget to include the attribute multiple in your HTML input tag.
  • Don't forget to store the AWS user's credentials in environment variables as shown in the readme. Make sure these environment variables are available in the session where Python is running.


In your code in the following line:

k.set_contents_from_string(data_file.readlines())

you're sending a list of strings (terminated by newlines!) to Amazon instead of the file content as is.

You need to pass a single str object with the file contents:

set_contents_from_string(data_file.read())