Django — Using Azure blob storage to handle static & media assets — from scratch

David Santiago
4 min readNov 27, 2018

In this tutorial you will learn how to use the Azure Blob service to handle static assets and the user uploaded files, that is, the media assets.

Create Resource Group + Storage Account + static & media containers

The best and fastest way to create anything in Azure is to use Azure CLI. Nevertheless, you can do it by using the portal if you prefer. I’ll present both ways.

I’ll use the following names :

  • Resource group name: DemoDjangoBlob
  • Storage Account name: djangoaccountstorage
  • Media container name: media
  • Static container name: static

Using Azure CLI

# Create resource group named DemoDjangoBlob 
$ az group create -l westeurope -n DemoDjangoBlob
# Create storage account named djangoaccountstorage
$ az storage account create -n djangoaccountstorage -g DemoDjangoBlob -l westeurope --sku Standard_LRS --https-only true
# (optional) Update storage account to V2
$ az storage account update -n djangoaccountstorage -g DemoDjangoBlob --set kind=StorageV2
# Create the "media" container
$ az storage container create -n media --account-name djangoaccountstorage --public-access blob
# Create the "static" container
$ az storage container create -n static --account-name djangoaccountstorage --public-access blob

Using Portal

Select your Subscription and display Resource group list. Click on “Create Resource group”.

Click on “Create”, go to the resource group and Click on “Create resources”.

Searchstorage account” and Click on “Create”. Configure the Storage Account as following:

EnableSecure transfer required” in Advanced tab and Create the resource.

Once it’s done, Go to the resource on “Blob service” item menu. Create static and media containers with “Blob” public access level :

It’s done. You’ll need Storage Account Key later, they are located in “Access Keys” item menu.

Install and configure Django

The easiest way is to install everything using pipenv :

# Basic Django installation
$ mkdir demo-django-azure_blob
$ cd demo-django-azure_blob/
$ pipenv install django
$ pipenv install django-storages[azure]
$ pipenv shell
(demo-django-azure_blob) $ django-admin.py startproject backend .
(demo-django-azure_blob) $ python manage.py migrate

Now, add storages to your INSTALLED_APPS in settings.py module:

Then, add the following lines at the end of settings.py module :

AZURE_ACCOUNT_NAME must be replaced by your storage account name.

Finally, create a custom_azure.py file in backend/ folder. Replace account_name and account_key.

You can get your storage account key by using Azure CLI :

$ az storage account keys list -n djangoaccountstorage -g DemoDjangoBlob

It’s almost finished. You just need to migrate static & media files now.

Migrate static files

collectstatic command will copy files to remote location now.

(demo-django-blob) $ python manage.py collectstaticYou have requested to collect static files at the destinationlocation as specified in your settings.This will overwrite existing files!Are you sure you want to do this?Type 'yes' to continue, or 'no' to cancel: yes119 static files copied

119 static files were copied but I have an “empty” project. In fact, Django Admin static files (css, fonts, js and img) were copied.

Test it

Let’s run the local server without static files, this is the best way to check it works :

(demo-django-blob) $ python manage.py runserver --nostatic

Finally navigate to http://127.0.0.1:8000/admin/login/?next=/admin/

You might have the following Django administration page :

Check source code:

Conclusions

Using Azure blob service with Django is pretty easy thanks to django-storages library. Moreover, features like Azure CDN will come soon 😃.

Next steps can be :

  • Store account_name and account_key inside environment variables.
  • Deal with Cross-Origin Ressource Sharing supported by Azure Storage Services

Hope this tutorial was helpful. Feel free to leave me a feedback 😏

PS: This tutorial was largely inspired by the tutorial from Vitor Freitas : How to Setup Amazon S3 in a Django Project.

--

--