From c5e63102e2b8a3c91c465d96bec7734f22cd8c6b Mon Sep 17 00:00:00 2001 From: Roman Miroshnychenko Date: Thu, 4 Oct 2018 20:17:38 +0300 Subject: [PATCH] Add support for Azure CDN It adds support for Azure CDN endpoints connected to Azure Storage accounts. --- docs/backends/azure.rst | 10 ++++++++++ storages/backends/azure_storage.py | 3 +++ tests/test_azure.py | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/docs/backends/azure.rst b/docs/backends/azure.rst index c84bcde86..a71a549f2 100644 --- a/docs/backends/azure.rst +++ b/docs/backends/azure.rst @@ -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. diff --git a/storages/backends/azure_storage.py b/storages/backends/azure_storage.py index f476fd87a..428b06bd6 100644 --- a/storages/backends/azure_storage.py +++ b/storages/backends/azure_storage.py @@ -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 @@ -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 diff --git a/tests/test_azure.py b/tests/test_azure.py index 55e93813f..47c1f9252 100644 --- a/tests/test_azure.py +++ b/tests/test_azure.py @@ -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')