Upload Files with Python

Python is eating the world! You will find many passionate Python programmers, and just as many critics, but there’s no denying that Python is a powerful, relevant, and constantly growing force in software development today.

A cartoon from xkcd joking about Python
Image credit: https://imgs.xkcd.com/comics/python.png

Python is just a language, though, and languages can’t solve business problems like workflow, architecture, and logistics; these things are up to you, the Developer! The packages you choose, the architecture you implement, and strategy you follow will all impact the success of your Python project. Let’s take a look at the logistics behind uploading a file to the cloud using Python. I’ll discuss some of the considerations every team faces when implementing a file upload and management solution, and then end with a concise recipe for you to upload files with Python using Filestack’s Python SDK.

The Problem: How do I manage user generated uploads?

You’ve built your MVP, and all the tests are passing. You have the ability to upload files via HTML forms on your site; that’s easy! Just a simple POST on form submit, and your server catches whatever your users throw at it. That’s only the beginning, though–next comes the hard part(s).

Ensuring that your users have a great experience when they upload, transform, and share their content means providing them with reliable, resumable, fast uploads. It means providing them with the tools they need to crop, resize, and otherwise adjust their uploads. It can mean converting files from one format to another. It certainly means optimizing the delivery so they are saving data and getting the highest quality content as fast as possible from a powerful, distributed CDN.

The Solution: Use Filestack’s Python SDK

When a project can separate the grunt work from the business logic, everybody wins. You could build everything from scratch with a small army of developers and a time machine, but why do that when you can use the Filestack API? And why call the API directly, implementing everything in your own code, when you could just pop in the SDK and call it a day?

Obtain the SDK

Using pip, run the following command:

$ pip install filestack-python

You should see a nice display of progress bars as the files are automatically loaded up for you:

A black and green screenshot of a terminal on macOS Mojave showing the pip install filestack-python command and its resulting output

Use the SDK

Once you have the Filestack Python SDK installed you’ll just need to bring it into your very own Python script, create an instance of the Client with your Filestack API key, and then call client.upload() with some parameters. Import it like this:

from filestack import Client

That Client does all the heavy lifting for you. Here’s a simple recipe to put it to work:

A Simple Recipe for Python File Uploads


  • Python
  • Pip
  • A Filestack API Key (get a free 2-week trial if you need one)

Create a file to test inside of the directory where you would like to try things out. I’ll call mine upyougo.py. Add an image in there for your upload example. Mine is the comic strip from xkcd with the flying Pythonista.

from filestack import Client
client = Client("APIKEY")

params = {'mimetype': 'image/png'}
new_filelink = client.upload(filepath="python.png", params=params)

Be sure to replace “APIKEY” with your actual API key.

Run this by passing the filename to python as an argument in the command line, like this:

$ python upyougo.py

That’s it! With this simple code we can successfully upload a file from our local machine to our Filestack app using Python. You should see a URL output to your terminal upon completion of the upload. Copy and paste that into your browser to view, and try out some of our transformation URLs to play with it further!

But, Docker…

Ok, here’s a ridiculously fast way to try this out on any machine: if you have Docker installed on your system, you can run the example upload shown above with the following command:

$ docker run -it --rm -e APIKEY=MYAPIKEYHERE filestack/python:example

This has just been a tiny peek at what you can do with Filestack’s JavaScript SDK on the server side with node. To learn more, check out https://www.filestack.com/products/file-upload/ and explore our docs for detailed features and implementation instructions.

Read More →