In this article you will learn how to upload files to your S3 bucket using Flask from your website.

Note: You must have an account on aws to access the S3 service.

We will be having

  • .py file with the flask code
  • .html file to choose the file from the website

In the html file you need to put the following code:

<form action="/upload" method="post" enctype="multipart/form-data">
<p class="card-text">Choose a file to upload it to AWS S3</p>
<input type="file" name="file" value="file">
<hr>
<input type="submit" name="upload" value="Upload" class="btn btn-success">
</form>
{{msg}}

You will find two inputs with type file and submit respectively. The input with type file will allow you to select the a file/image from your computer. The input with type submit will allow to upload it.

When the user clicks on the submit type button form action = /upload function is triggered. This is where the flask part comes into picture.

Before we get into that, lets connect out aws account with our flask application. To do so look at the following code:

import boto3s3 = boto3.client('s3',
aws_access_key_id='access key here',
aws_secret_access_key= 'secret key here',
aws_session_token='secret token here'
)
BUCKET_NAME='hackershrine'

boto3 will help us connect our account as well as the aws service that we want to use. In your aws account you will find the access keys, just paste them and you are good to go. You have to create a bucket first on your aws s3 service and then paste the name of the bucket.

To create an S3 bucket do the following steps:

  • Search S3 on your aws account
  • Click on Create Bucket
  • Enter a unique bucket name(here I have named hackershrine)
  • Region must be ‘US East (N. Virginia)’ and then Click Next
  • Keep the default settings for Configure Options(for this article) and click Next
  • In Permissions, make sure public access checkbox is unchecked so you can have access to that bucket and Click Next
  • Review your settings and click Create Bucket.

Lets look at the flask application now. The below code will setup your flask application.

from flask import Flask, render_template, requestapp = Flask(__name__)
from werkzeug.utils import secure_filename

Write your first app route like this. This will show you the Choose file and upload button options when you run your application.

@app.route('/')  
def home():
return render_template("file_upload_to_s3.html")

So when the user clicks on the upload button the below snippet will run in flask

@app.route('/upload',methods=['post'])
def upload():
if request.method == 'POST':
img = request.files['file']
if img:
filename = secure_filename(img.filename)
img.save(filename)
s3.upload_file(
Bucket = BUCKET_NAME,
Filename=filename,
Key = filename
)
msg = "Upload Done ! "return render_template("file_upload_to_s3.html",msg =msg)if __name__ == "__main__":

app.run(debug=True)

The action form will take you here. Since this is a Post method it will first check that. Then the file that user has selected will be stored in the variable img.

Make sure the highlighted part below is the same in your code as well

<input type="file" name="file" value="file"> //html file
img = request.files['file'] //flask python code

Once the image is selected, the upload_file function will put the file in our s3 bucket. It takes 3 parameters, the bucket name, filename and the Key.

In the end, return the same html file with a msg saying that upload is completed. Now go and check your s3 bucket that you created earlier, you will the file that you chose.

Thats it !!

You have successfully uploaded a file onto your S3 bucket using Flask. This is a simple application but heavily used in bigger applications.

You will find the entire code for this on my GitHub. I have also made a video on this particular applicaton on YouTube. Do check it out

I hope this helps someone.

Peace.