Django Storages
Django Storages provides a collection of custom storage backends for Django, enabling integration with various cloud storage providers like Amazon S3, Azure Blob Storage, and Google Cloud Storage. It simplifies file management by abstracting away the specifics of each cloud service. The current version is 1.14.6, with active development and regular releases for bug fixes and new features.
Warnings
- gotcha Forgetting to install the specific Python library for your chosen backend (e.g., `boto3` for S3, `google-cloud-storage` for GCS, `azure-storage-blob` for Azure) will result in `ModuleNotFoundError` or similar import errors when Django tries to initialize the storage backend.
- breaking Versions 1.12+ (including 1.14.x) of `django-storages` require Python 3.7+ and Django 3.2+. Older Python or Django versions are no longer supported and will lead to installation or runtime errors.
- gotcha Incorrect AWS IAM permissions or S3 bucket policy can lead to 'Access Denied' errors. Also, a mismatch between `AWS_S3_REGION_NAME` in your settings and the actual region of your S3 bucket is a common pitfall.
- gotcha Misconfiguration of `AWS_DEFAULT_ACL` or `AWS_QUERYSTRING_AUTH` can lead to files being unexpectedly public/private, or URLs that expire too quickly. For example, if `AWS_QUERYSTRING_AUTH` is `True` (default), all URLs will be signed and time-limited, which might not be desired for publicly accessible static assets.
Install
-
pip install django-storages
Imports
- S3Boto3Storage
from storages.backends.s3boto3 import S3Boto3Storage
- GoogleCloudStorage
from storages.backends.gcloud import GoogleCloudStorage
- AzureStorage
from storages.backends.azure_storage import AzureStorage
Quickstart
# In your Django project's settings.py
INSTALLED_APPS = [
# ... your other apps
'storages',
]
# --- Example for Amazon S3 Backend ---
# Make sure to install boto3: pip install boto3
# Set DEFAULT_FILE_STORAGE for media files
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
# Your AWS settings
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID', '')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY', '')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME', '')
AWS_S3_REGION_NAME = os.environ.get('AWS_S3_REGION_NAME', 'us-east-1') # e.g., 'us-east-1'
# Optional: Set a custom domain for your media files if using a CDN
# AWS_S3_CUSTOM_DOMAIN = 'your-cdn-domain.com'
# Optional: Control file access. 'public-read' makes files publicly accessible.
# AWS_DEFAULT_ACL = 'public-read'
# Optional: Don't sign URLs by default (often useful for public access)
# AWS_QUERYSTRING_AUTH = False
# --- Example for Google Cloud Storage Backend (Optional) ---
# Make sure to install google-cloud-storage: pip install google-cloud-storage
# For GCS, you typically provide a path to a service account JSON key file
# or rely on application default credentials.
# GOOGLE_CLOUD_STORAGE_BUCKET_NAME = os.environ.get('GCS_BUCKET_NAME', '')
# GOOGLE_CLOUD_STORAGE_JSON_KEY_FILE = os.environ.get('GCS_JSON_KEY_FILE', '/path/to/your/key.json')