File upload to Google Cloud Storage using a Flask API

Part 2/3 — Implementing & testing the upload endpoint

Image for post
Image for post
  1. Adding an endpoint for uploading the image

Adding an image

The filedepot library comes with an UploadedFileField class, which we will import and use to create our avatar column. This class will take care of uploading the file when assigning a value and committing the model. It also provides access to the file url. You can pass an upload_storage (i.e. depot name) to the UploadedFileField’s constructor to use a custom configured depot. Right now, we only have a single storage with the name “avatar” configured in my_app/config/ So, by default the UploadedFileField object will use this storage and you don’t need to provide the upload_storage explicitly. Hence, adding the avatar boils down to only a couple lines of code:

Adding the upload endpoint

Model-wise we are well-prepared now. In order to make our uploading functionality available for an API client, we still need an upload endpoint.

Adding a depot middleware

When running pytest tests/users/ again, you should now see the upload test passing. But, wait, we now get another Error:

RuntimeError: No WSGI middleware currently registered

Adding thumbnails

Before we come to the end of this post, we’ll cover a common use case for image uploading. You might want to upload an image and auto-generate different-sized versions an the fly. So let’s briefly look into how you can create thumbnails for your uploaded avatar image.


Creator of 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