Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Azure CDN #611

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')