Skip to main content

AWS S3 File Upload using Python and Flask

In our previous Python project tutorial, we have developed Discussion Forum using Flask and Python. In this tutorial, we will build an app to upload files to AWS S3 using Python and Flask.

Amazon Web Services (AWS) is provides on demand cloud services for hosting websites and storing files. The AWS S3 (Simple Storage Service) is a cloud storage service from AWS that offers simple, scalable, cost-effective and secure to store files. It provides feature to create buckets to store files and called objects.

So here in this tutorial, we will develop a flask based application to upload files to AWS S3 server. We will implement functionality to upload files to AWS S3 buckets using Python. We will use boto3 module from Python to upload files.

So let’s proceed with developing AWS S3 File uploader Project:

1. Setup Project

First we will create project aws-s3-file-upload-python-flask using below command.

$ mkdir aws-s3-file-upload-python-flask

and moved to the project.

$ cd aws-s3-file-upload-python-flask

2. Install Module

We will install required modules for our application. As we will develop a web based application, so we will install Flask micro framework module to create web application.

$ pip install Flask

we will also need to install boto3 module that allows to use AWS S3 services.

pip install boto3

3. Handle File Upload S3 Server

We will create our appliction Python file app.py to implement file upload functionality.

We will import request modules in our app file.

from flask import Flask, render_template, request, redirect, send_file, url_for
import os
from s3_file_upload import listFiles, downloadFile, uploadFile

we will also create flask app.

app = Flask(__name__)

We will check for route upload and implement functionality to upload file by calling function uploadFile() to upload to S3.

@app.route("/upload", methods=['POST'])
def upload():
    if request.method == "POST":
        f = request.files['file']
        f.save(f.filename)
        uploadFile(f"{f.filename}", BUCKET)

        return redirect("/listFile")

We will create a python file s3_file_upload.py and import boto3 module. Then we will implement function uploadFile() to upload to S3 server.

import boto3

def uploadFile(fileName, bucket):    
    object_name = fileName
    s3_client = boto3.client('s3')
    response = s3_client.upload_file(fileName, bucket, object_name)
    return response

4. Create File Upload Form

Now we will design a fiel upload form. As we are developing a flask application, so we will create a templates drirectory and then create a html file upload.html and design upload form. We will define form action to /upload to upload file to S3 server.


<h2 class="text-left"> {{ msg }} </h2>
<div class="mt-4">
<h5>Upload file:</h5>
<form method="POST" action="/upload" enctype="multipart/form-data" class="mt-3">
  <div class="mb-3">
	<input type="file" name="file" class="form-control">
  </div>
  <div>
	<input type="submit" value="Upload" class="btn btn-primary">
  </div>
</form>
</div>

5. List Uploaded Files

We will also implement functionalty to list uploaded files. We will check for route and implement function listFile() and call function listFiles() and reutrn to render template to display list in template file.

@app.route("/")
def listFile():
    contents = listFiles(BUCKET)
    return render_template('upload.html', contents=contents)

we will implement function listFiles() in file s3_file_upload.py to get and list files.

def listFiles(bucket):    
    s3 = boto3.client('s3')
    contents = []
    try:
        for item in s3.list_objects(Bucket=bucket)['Contents']:
            print(item)
            contents.append(item)
    except Exception as e:
        pass
    return contents

6. Download Uploaded Files

We will also check for download route and implement download() function. We will call function downloadFile(filename, BUCKET)

@app.route("/download/", methods=['GET'])
def download(filename):
    if request.method == 'GET':
        output = downloadFile(filename, BUCKET)

        return send_file(output, as_attachment=True)

we will implement function downloadFile() in file s3_file_upload.py to download file.

def listFiles(bucket):    
    s3 = boto3.client('s3')
    contents = []
    try:
        for item in s3.list_objects(Bucket=bucket)['Contents']:
            print(item)
            contents.append(item)
    except Exception as e:
        pass
    return contents

You can download the complete script from download link below.

Download