Uploading Files To AWS S3 Bucket From Html-Form Using A Python CGI Script

March 20, 2022 | Comments(0) |

Introduction

In this blog, we will discuss the traditional way of handling web forms using python with an example: uploading a file to AWS S3 using python boto3 package and python CGI package.

AWS S3 is a cloud service used to store static files in AWS. To upload any file to the S3 bucket, we need to log in to the AWS management console and open the S3 console. So, we will upload files to AWS S3 from the S3 console. To overcome all these login processes, we can create a web page to directly upload any file from any computer to our AWS S3 bucket. In this blog, we will be explaining this functionality using Python CGI script.

What is Boto3?

Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python. It allows Python developers to write software that uses services like Amazon S3 and Amazon EC2.

It gives the flexibility to access the AWS services from Python scripts. They are mainly used to automate the AWS services.

What is CGI?

CGI stands for Common Gateway Interface. It is an interface where web servers execute any program externally, generally processing client requests. These programs are primarily written using scripting languages like Python, Perl, PHP. These scripts are generally mentioned as CGI scripts.

Use Case

Its general use case occurs when a web client submits a form on a web page that uses CGI script as a backend. Then the submitted form data is sent to the web server using an HTTP request using HTTP methods like GET/POST, denoted in a URL that points to a CGI script. Then, the webserver that launches the CGI script file present in the URL in a new computer process passes the form data to that script. Now script gets processed, the response of the CGI script is returned in the form of HTML processed output and is sent to the webserver then web server sends the HTML output back to the web client from where the request is made.

AWS S3

Features of CGI:

  • CGI is a method of interacting with HTML. It has the capability of processing the HTML written in a CGI script.
  • CGI scripts are written using scripting languages like Python, Perl, C, or maybe just a simple shell script.
  • Today CGI scripts are evolved as more browser friendly.

Advantages of CGI:

  • As long as the applications follow the CGI standard, they may be built in any language and executed in any environment.
  • Complicated tasks in CGI are now a lot easier to implement.

Disadvantages of CGI:

  • Each webpage load in the CGI Interface incurs overhead because the program must be loaded into memory.
  • CGI consumes a significant amount of processing time.
  • For each form submitted, the client web page gets reloaded.

Prerequisites:

  1. Python IDE
  2. Python Boto3 package.
  3. Lambda Role access key and secrete key, which has AWS S3 full access.

Step-by-Step Guide:

->Create a folder that says “Project1”. Now create an HTML file say “form_upload.html” which contains below html form code.
Python

Whenever the form is submitted, the file will be redirected to the address present in the action parameter. In addition, form data will be redirected to the python script using the POST method.

  • CGI scripts must be in a particular directory, like cgi-bin. So now let’s create folder “cgi-bin” inside the “Project1” folder.
  • Now go to the cgi-bin folder to write our Python script, let us name the file as

Python

  • Now open the command prompt and go to the “Project1” folder, from here we will be starting our server which starts listening in port 8000 by using the below command.

Python

  • Now, open the browser and enter the URL form_uoload and choose a file, then press the upload button
    Python
    Python
  • We can also check the response data from the command prompt while the server is running
    Python

Conclusion:

There are many ways to upload files to AWS S3 using the python boto3 package, but in this blog, we tried one of the ways, i.e., using traditional CGI script, which does not require any AWS API gateway configuration and serverless scripts like AWS lambda functions.

About CloudThat

We here at CloudThat are the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge on cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Feel free to drop a comment or any queries that you have regarding AWS services, cloud adoption, consulting and we will get back to you quickly. To get started, go through our Expert Advisory page and Managed Services Package that is CloudThat’s offerings.


Leave a Reply