Add azure blob storage to django

This commit is contained in:
Lorenz Padberg 2023-05-17 18:00:41 +02:00 committed by Daniel Egger
parent d0f48866f5
commit 6e264b1480
4 changed files with 32 additions and 0 deletions

Binary file not shown.

View File

View File

@ -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

View File

@ -214,6 +214,20 @@ if USE_AWS:
# https://wagtail.org/blog/amazon-s3-for-media-files/ # https://wagtail.org/blog/amazon-s3-for-media-files/
MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage" 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: else:
MEDIA_URL = "/server/media/" MEDIA_URL = "/server/media/"