File upload to Google Cloud Storage using a Flask API

Part 1/3 — Setting up filedepot & different work environments

Image for post
Image for post
  • 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.

  1. Replacing the AWS access key and secret key with the corresponding Google developer keys

Setting up filedepot

First you have to add filedepot and boto3 to your app’s requirements.txt. Boto3 provides us with the interface to AWS or more important for us, Google Cloud Storage later on. If we didn’t install it filedepot will complain and we won’t be able to start our Flask app in production mode (with the TESTING config set to True you wouldn’t need it at this stage, though).

#...
filedepot==0.8.0
boto3==1.17.0
pip install -r requirements.txt
pipenv install filedepot boto3
└── my_app
├── my_app
│ └── config
│ │ ├── __init__.py
│ │ └── depot.py
│ ├── users
│ │ ├── __init__.py
│ │ ├── models.py
│ │ └── views.py
│ ├── __init__.py
│ ├── app.py
│ └── …
├── tests
│ └── …
├── …

Adding a test config

Filedepot comes with mutliple storages. There is a LocalFileStorage, a GridFSStorage, an S3Storage, and a MemoryFileStorage. Since the MemoryFileStorage stores the “uploaded” files in your local memory, it’s just perfect for our testing purposes.

Adding a production config

As I briefly mentioned above, we can use depot.io.boto3.S3Storage to connect to our Google Cloud Storage.

GOOGLE_CLOUD_STORAGE_ACCESS_KEY
GOOGLE_CLOUD_STORAGE_SECRET_KEY
GOOGLE_CLOUD_STORAGE_BUCKET

Creator of adoptoposs.org. Authoring code @MindDoc. Comics addict and fan of bad jokes.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store