File upload to Google Cloud Storage using a Flask API
Part 1/3 — Setting up filedepot & different work environments
--
This is the first part of our little post series where we explore how to build a Flask JSON API for uploading files to Google Cloud Storage. Today we are going to deal with some requirements, descisions, and setting up our basic app.
This is going to be a rather extensive tutorial. If you want to dive into the code along with reading: it’s available on Github.
Recently at work, I wanted to add a simple file upload to one of our Flask APIs. It took me a while to figure out how to do it. It seems like in addition to some special requirements we had, the available packages lack some easy to understand documentation. That’s why I want to share some insights on how you can integrate file uploading into your Python app.
So, this was our list of requirements to be covered:
- Upload files to Google Cloud Storage
- Customize the directory structure of the uploaded files and their names
- Upload an image, store the original version and two resized versions
- The solution should be easy to extend to allow uploading other file types than images (e.g. pdf, audio files)
- Integration in a Flask JSON API
- It should seamlessly work with SQLAlchemy as ORM
- File upload should be testable with automated tests (using pytest)
Choosing a package
First of all I had to decide which package I wanted to use for uploading images. A web search quickly made clear that there was not much information out there on how to upload files to Google Cloud storage. The most fitting packages were sqlalchemy_media and filedepot.
Neither of them (at the time of writing) provides a specific storage for uploading to Google Cloud Storage nor any documentation for how to use it with Google Cloud Storage. Yet, Google claims that migrating from Amazon S3 should be pretty easy and straight forward by doing the following:
- Changing the request endpoint of the lib you use to request the Cloud Storage endpoints