I'm using Azure Storage account for storing my media files
Setting it is simple in settings.py like this:
DEFAULT_FILE_STORAGE = 'storages.backends.azure_storage.AzureStorage'
AZURE_ACCOUNT_NAME = 'my_account_name'
AZURE_ACCOUNT_KEY = 'my_account_key'
AZURE_CONTAINER = 'my-container'
However, I considered later that I want to use Azure CDN instead for serving my media files. How will I point it to the CDN URL instead? I tried setting it in the MEDIA_URL like
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload')
MEDIA_URL = '//my-media.azureedge.net/my-container/'
However my storage-account blob is the one being shown as default URL which is 'xxxxx.blob.core.windows.net' instead of my MEDIA URL..
How will I show the MEDIA_URL instead of 'xxxxx.blob.core.windows.net'?
Thankfully I already have an answer for this one. You need to override the storage backend class of Azure on the 'storages' library replacing the blob hostname with the CDN hostname.
settings.py
MEDIA_URL = '//my-media.azureedge.net/my-container/'
storages.py
import re
from jaguar import settings
from storages.backends.azure_storage import AzureStorage
class AzureCDNURL(AzureStorage):
def url(self, name):
ret = super(AzureCDNURL, self).url(name)
_ret = re.sub('//[a-z.0-9A-Z]*/', settings.MEDIA_URL, ret)
return _ret