diff --git a/env_secrets/production_azure.env b/env_secrets/production_azure.env index 46a22673..a6c56c0b 100644 Binary files a/env_secrets/production_azure.env and b/env_secrets/production_azure.env differ diff --git a/server/backend/__init__.py b/server/backend/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/server/backend/custom_azure.py b/server/backend/custom_azure.py new file mode 100644 index 00000000..fe0c9b08 --- /dev/null +++ b/server/backend/custom_azure.py @@ -0,0 +1,18 @@ + +from storages.backends.azure_storage import AzureStorage +from environs import Env + +env = Env() +env.read_env() +class AzureMediaStorage(AzureStorage): + account_name = env("AZURE_STORAGE_ACCOUNT") + account_key = env("AZURE_STORAGE_KEY") + azure_container = 'media' + expiration_secs = None + +class AzureStaticStorage(AzureStorage): + account_name = env("AZURE_STORAGE_ACCOUNT") + account_key = env("AZURE_STORAGE_KEY") + azure_container = 'static' + expiration_secs = None + diff --git a/server/config/settings/base.py b/server/config/settings/base.py index 7cfba7f1..3fc03441 100644 --- a/server/config/settings/base.py +++ b/server/config/settings/base.py @@ -214,6 +214,20 @@ if USE_AWS: # https://wagtail.org/blog/amazon-s3-for-media-files/ MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage" +elif env(USE_AZURE_BLOB_STORAGE): + # https://medium.com/@DawlysD/django-using-azure-blob-storage-to-handle-static-media-assets-from-scratch-90cbbc7d56be + DEFAULT_FILE_STORAGE = 'backend.custom_azure.AzureMediaStorage' + STATICFILES_STORAGE = 'backend.custom_azure.AzureStaticStorage' + + STATIC_LOCATION = "static" + MEDIA_LOCATION = "media" + + AZURE_ACCOUNT_NAME = "djangoaccountstorage" + 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}/' + + else: MEDIA_URL = "/server/media/"