- Published at
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
-
-
- Name
- James Lau
- Indie App Developer at Self-employed
-
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-dotenvanddjango-storagespackages 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 classAzureMediaStorage.STATICFILES_STORAGE: Specifies the storage backend for static files. We’re pointing it to a custom classAzureStaticStorage.STATIC_LOCATIONandMEDIA_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. Usepython-dotenvto load it from a.envfile.AZURE_CUSTOM_DOMAIN: Constructs the custom domain for your Azure Blob Storage.STATIC_URLandMEDIA_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,
AzureMediaStorageandAzureStaticStorage, that inherit fromAzureStorage. - We set the
account_name,account_key, andazure_containerattributes for each class. Theazure_containerspecifies the container name in your Azure Blob Storage account where the files will be stored. expiration_secsis set toNonewhich means that files will not expire.
Step 4: Configure Environment Variables (Recommended)
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.