If you work with Python applications, there's a fair chance you've had to upload files to remote URLs at some point in time. Uploading files is a common part of almost every user-facing application. If you need the user to provide you with any type of information they can't type in manually, you would usually need them to upload it in the form of a file. Additionally, documents for verifying the identity of a user or for supporting a transaction or a discussion are usually uploaded directly in the form of files.
In this article, you'll learn how to upload files from your Python scripts and apps using six different methods, including how to upload files to Amazon S3 buckets.
Uploading Files in Python
To get started uploading files in Python, make sure you have a copy of Python locally installed. If not, head over to the official Python website and follow the instructions relevant to your host operating system to set it up. Additionally, you’ll need a sample file to test with each upload method; you can download some sample PDF files here.
You can find the complete code used in this tutorial, along with the sample files, in this GitHub repo.
Once you have all of the above, you can go ahead and try out the different methods.
Uploading a Single File
The requests Library
The output of this method will look similar to this:
The example above uploads files to a public service called httpbin. The response includes a confirmation message stating the upload was completed, followed by the contents of the response object. The response object contains the base64 encoded file data, a set of request headers describing the specifics of the request, and the origin and remote addresses.
The method is simple, does not require any extra setup steps, and works with most backend services.
The ftplib Module
Before you can use it, you first need an active FTP server. If you have access to a remote FTP server, you can use its host, port, username, and password to get started. If you don't have the credentials to a remote FTP server, you can create one locally and upload files to it:
This method works great for FTP servers in both remote and local setups. However, you can't upload files to HTTP servers or APIs using this method.
The Filestack API
Filestack is a simple and economical way to add file uploading functions to your web app. Filestack is a file uploader service that provides easy-to-use SDKs in most programming languages, including Python. You can use the Filestack SDK for Python to upload files to the Filestack server directly from your Python script.
To use the Filestack API, you first need to generate a Filestack API key. Go to the Filestack website and create a new account:
Once you’ve created a new account, you’ll see your API key in the top-right corner of your dashboard. Click the Copy API Key button to copy it. Once you have the API key, you can run the following command to install the Filestack API client:
Django Web App
Django is a popular Python framework used to build web applications. You can create a web application in Django that handles files uploaded by users from HTML forms.
Making use of an HTML form to upload files in a Python-based web app is handy when you’re building full-scale web applications and want to enable your users to upload files in your app. However, this method does not help you upload a file to any file server or bucket, unlike the other methods in this list.
While single file upload should be sufficient in most cases, you might run into situations where you need to upload multiple files at once. Note that this is different from running the single file upload operations in a loop for a group of files.
If you need to upload multiple files, you could use the single file upload method multiple times, but this would require multiple requests and could cause issues such as blocking bandwidth or incurring unnecessary expenses if you use a paid external file-handling server. Instead, it makes more sense to upload multiple files in the same API request.
This method is versatile as it supports uploads of both single files and multiple files to any remote or local server. You can use this method in most cases to solve your file upload issues.
Uploading Files to Amazon S3
As a final step before running the code, you need to ensure that:
- an AWS S3 bucket exists in your AWS account
- your AWS credentials have the right permissions to access S3
- you’ve updated the S3 bucket name in the code snippet above
Once you run the code snippet, you can check out your bucket to find the uploaded file:
If you are looking to manage and process documents from your Python app, Sensible might just be the right tool for you. It’s a document extraction platform for developers, and its well-documented REST API provides access to a plethora of powerful document processing features from within your Python app. Make sure to check out Sensible today!