Published at

Configuring Azure Blob Storage for Django Static and Media Files

Configuring Azure Blob Storage for Django Static and Media Files

Configure Azure Blob Storage to serve static and media files in a Django project. Includes settings.py and custom_azure.py configurations.

Authors
  • avatar
    Name
    James Lau
    Twitter
  • Indie App Developer at Self-employed
Sharing is caring!
Table of Contents

This post explains how to configure Azure Blob Storage to serve static and media files in a Django project. This approach is beneficial for scalability, content delivery, and separating concerns in your application architecture.

Prerequisites

  • An Azure account with an active subscription.
  • An Azure Storage account created.
  • A Django project set up.
  • The python-dotenv and django-storages packages installed (pip install python-dotenv django-storages).

Step 1: Install Required Packages

Make sure you have the necessary packages installed in your Django project:

pip install django-storages python-dotenv

Step 2: Configure settings.py

Modify your settings.py file to use Azure Blob Storage for static and media files. Add the following settings:

# settings.py

import os
from dotenv import load_dotenv

load_dotenv()

DEFAULT_FILE_STORAGE = "myproject.custom_azure.AzureMediaStorage"
STATICFILES_STORAGE = "myproject.custom_azure.AzureStaticStorage"

STATIC_LOCATION = "static"
MEDIA_LOCATION = "media"

AZURE_ACCOUNT_NAME = "mystorageaccount"  # Replace with your storage account name
ACCOUNT_KEY = os.getenv("AZURE_STORE_KEY")  # Set environment variable for security
AZURE_CUSTOM_DOMAIN = f"{AZURE_ACCOUNT_NAME}.blob.core.windows.net"

STATIC_URL = f"https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION}/"
MEDIA_URL = f"https://{AZURE_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/"

Explanation:

  • DEFAULT_FILE_STORAGE: Specifies the storage backend for media files. We’re pointing it to a custom class AzureMediaStorage.
  • STATICFILES_STORAGE: Specifies the storage backend for static files. We’re pointing it to a custom class AzureStaticStorage.
  • STATIC_LOCATION and MEDIA_LOCATION: Define the folder names within the Azure Blob container where static and media files will be stored.
  • AZURE_ACCOUNT_NAME: Your Azure Storage account name.
  • ACCOUNT_KEY: Your Azure Storage account key. It’s highly recommended to store this in an environment variable for security reasons. Use python-dotenv to load it from a .env file.
  • AZURE_CUSTOM_DOMAIN: Constructs the custom domain for your Azure Blob Storage.
  • STATIC_URL and MEDIA_URL: Define the base URLs for serving static and media files.

Step 3: Create custom_azure.py

Create a file named custom_azure.py within your Django project (e.g., myproject/custom_azure.py) to define the custom storage classes:

# myproject/custom_azure.py

from storages.backends.azure_storage import AzureStorage
from django.conf import settings


class AzureMediaStorage(AzureStorage):
    account_name = settings.AZURE_ACCOUNT_NAME
    account_key = settings.ACCOUNT_KEY
    azure_container = "media"
    expiration_secs = None


class AzureStaticStorage(AzureStorage):
    account_name = settings.AZURE_ACCOUNT_NAME
    account_key = settings.ACCOUNT_KEY
    azure_container = "static"
    expiration_secs = None

Explanation:

  • We create two classes, AzureMediaStorage and AzureStaticStorage, that inherit from AzureStorage.
  • We set the account_name, account_key, and azure_container attributes for each class. The azure_container specifies the container name in your Azure Blob Storage account where the files will be stored.
  • expiration_secs is set to None which means that files will not expire.

Create a .env file in the root of your Django project and add your Azure Storage account key:

AZURE_STORE_KEY=your_azure_storage_account_key

Important: Never commit your .env file to version control. Add it to your .gitignore file.

Step 5: Collect Static Files

Run the collectstatic command to copy your static files to the Azure Blob Storage container:

python manage.py collectstatic

Step 6: Update your project

Make sure your STATIC_ROOT is properly configured. An example would be:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Conclusion

By following these steps, you can successfully configure Azure Blob Storage to serve static and media files in your Django project. This setup provides a scalable and reliable solution for managing your project’s assets.

Sharing is caring!