Skip to content

Commit

Permalink
Add support for Azure CDN
Browse files Browse the repository at this point in the history
It adds support for Azure CDN endpoints connected to Azure Storage accounts.
  • Loading branch information
romanvm committed Oct 7, 2018
1 parent f9a0edf commit c5e6310
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/backends/azure.rst
Expand Up @@ -125,3 +125,13 @@ The following settings are available:
``AZURE_LOCATION``

Default location for the uploaded files. This is a path that gets prepended to every file name.

``AZURE_CDN_HOSTNAME``

Hostname (without protocol prefix) for Azure CDN endpoint connected to ``AZURE_ACCOUNT_NAME``,
e.g.::
AZURE_CDN_HOSTNAME = 'foobar.azureedge.net'

If ``AZURE_CDN_HOSTNAME`` is set, it will be used instead of Azure Blob Storage
hostname in static file URLs generated with ``static`` template tag.
3 changes: 3 additions & 0 deletions storages/backends/azure_storage.py
Expand Up @@ -147,6 +147,7 @@ class AzureStorage(Storage):
location = setting('AZURE_LOCATION', '')
default_content_type = 'application/octet-stream'
is_emulated = setting('AZURE_EMULATED_MODE', False)
cdn_hostname = setting('AZURE_CDN_HOSTNAME')

def __init__(self):
self._service = None
Expand All @@ -161,6 +162,8 @@ def service(self):
self.account_key,
is_emulated=self.is_emulated)
self._service = account.create_block_blob_service()
if self.cdn_hostname is not None:
self._service.primary_endpoint = self.cdn_hostname
return self._service

@property
Expand Down
6 changes: 6 additions & 0 deletions tests/test_azure.py
Expand Up @@ -265,3 +265,9 @@ def test_last_modified_of_file(self):
self.storage._service.get_blob_properties.return_value = Blob(props=props)
time = self.storage.modified_time("name")
self.assertEqual(accepted_time, time)

@mock.patch('storages.backends.azure_storage.CloudStorageAccount')
def test_azure_cdn(self, _):
storage = azure_storage.AzureStorage()
storage.cdn_hostname = 'foobar.azureedge.net'
self.assertEqual(storage.service.primary_endpoint, 'foobar.azureedge.net')

0 comments on commit c5e6310

Please sign in to comment.