django-storages Documentation

repository·master·Indexed 25 days ago

https://github.com/jschneier/django-storages

A library providing a variety of storage backends for Django, allowing developers to switch between storage services using a unified interface. Supported backends include Amazon S3 (via boto3), Microsoft Azure Storage, and Apache Libcloud (supporting Google Cloud Storage, Nimbus.io, Ninefold, and Rackspace Cloudfiles).

Tokens
7K
Snippets
19
Records
46
Agent score
83%

What's inside django-storages

  1. Configure Signed URLs using IAM Sign Blob API

    master

    When running on Google Compute Services (App Engine, Cloud Run, Cloud Functions, etc.), the Python GCS client often requires a service account private key file to generate signed URLs. Since these services typically use access tokens from a metadata server instead of key files, you should use the IAM Sign Blob API.

    To enable this, set iam_sign_blob or GS_IAM_SIGN_BLOB to True. You can optionally use sa_email or GS_SA_EMAIL to override the service account used for signing.

  2. Configure Cloudflare R2 as a Django storage backend

    master

    To use Cloudflare R2 with django-storages, you must first create an R2 bucket and generate authentication tokens via the Cloudflare dashboard. You should also configure a Custom Domain in Cloudflare to serve as your Static URL, ensuring it matches the STATIC_URL setting in your Django settings.py (e.g., static.domain_name.com).

    In your Django settings.py, configure the S3-compatible backend with these specific settings:

    • bucket_name: The name of your Cloudflare R2 bucket.
    • endpoint_url: https://<ACCOUNT_ID>.r2.cloudflarestorage.com (replace <ACCOUNT_ID> with your actual Cloudflare Account ID).
    • access_key: Your Cloudflare R2 access key.
    • secret_key: Your Cloudflare R2 secret key.
  3. Use Azurite for local development

    master

    Azurite is a local emulator for Azure Storage. To use it, install Azurite, then configure your Django settings with the default Azurite connection string. Note that you must create containers manually via Azurite CLI or Azure Storage Explorer as the backend will not create them automatically.

    STORAGES = {
        "default": {
            "BACKEND": "storages.backends.azure_storage.AzureStorage",
            "OPTIONS": {
                "connection_string": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;",
                "azure_container": "media",
            },
        },
    }
  4. Authenticate with Azure Managed Identity

    master

    Managed Identity is the recommended password-less authentication method for Azure services (App Services, Functions, etc.).

    1. Install azure-identity: pip install azure-identity
    2. Use DefaultAzureCredential from azure.identity in your STORAGES configuration under the token_credential option.
    from azure.identity import DefaultAzureCredential
    
    STORAGES = {
        "default": {
            "BACKEND": "storages.backends.azure_storage.AzureStorage",
            "OPTIONS": {
                "token_credential": DefaultAzureCredential(),
                "account_name": "mystorageaccountname",
                "azure_container": "media",
            },
        },
    }
  5. Configure Backblaze B2 as a django-storages backend

    master

    Backblaze B2 can be used as a storage backend via its S3 Compatible API. To set it up, follow these steps:

    1. Sign up for a Backblaze B2 account.
    2. Create a public or private bucket. Note: Object-level ACLs are not supported by B2; all objects inherit their bucket's ACLs.
    3. Create an application key (it is recommended to limit its access to your specific bucket).
    4. Configure your Django settings using the Amazon S3 backend pattern with these specific overrides:
      • Set region_name to your Backblaze B2 region (e.g., us-west-004).
      • Set endpoint_url to https://s3.${AWS_S3_REGION_NAME}.backblazeb2.com (replacing ${AWS_S3_REGION_NAME} with your actual region).
      • Set access_key to your application key ID.
      • Set secret_key to your application key.
  6. Configure FTPStorage for Django >= 4.2

    master

    In Django 4.2 and later, storage backends are configured using the STORAGES dictionary. You can define a default storage for media files and a staticfiles storage for static files. Settings are passed via the OPTIONS key.

    Warning: This FTP storage is not suitable for large files as it uses memory for temporary data storage. It also does not close FTP connections automatically (it opens them lazily and attempts to re-establish them if disconnected).

  7. Configure storage backends in Django

    master

    After installation, you must configure the specific storage backend you intend to use by adding its unique settings to your Django settings.py file. Available backend categories include:

    • Amazon S3
    • Apache Libcloud
    • Azure
    • Dropbox
    • FTP
    • Google Cloud (gcloud)
    • SFTP
    • S3 Compatible storage

    Refer to the specific documentation for your chosen storage engine to determine the required configuration keys.