jschneier / django-storages

https://django-storages.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
2.7k stars 847 forks source link

AZURE_CUSTOM_DOMAIN has no effect on generated URL #1405

Closed fritzfs closed 2 months ago

fritzfs commented 2 months ago

Hi!

When I call url property on file, I get the full URL as if I did not set AZURE_CUSTOM_DOMAIN, for example:

https://something.blob.core.windows.net/mediafiles/cache/54/07/54077efe688aecfb1ef32ab868b85717.jpg

I want django-storages to work all operations without a custom domain, but I want this URL with a custom domain that represents my CDN (azure front door).

While debugging azure_storage.py inside django-storages, I see that custom_domain is loaded inside the class and custom_service_client exists, but url method still returns original azure blob url here:

https://github.com/jschneier/django-storages/blob/master/storages/backends/azure_storage.py#L332

I'm using

Python-3.11.0
Django-3.2
Django-Storages 1.14.2
Azure-Storage-Blob 12.19.1

I have the following code in settings.py:

AZURE_CONNECTION_STRING = "..."
AZURE_CONTAINER = "..."
AZURE_CUSTOM_DOMAIN = "blabla.com"
fritzfs commented 2 months ago

PS, I am aware of this PR and if I understood it correctly, it should work as I described above? https://github.com/jschneier/django-storages/pull/1176

fritzfs commented 2 months ago

Found solution! If we're using AZURE_CUSTOM_DOMAIN, we cannot use AZURE_CONNECTION_STRING because _get_service_client method is not fully executed, see this return: https://github.com/jschneier/django-storages/blob/master/storages/backends/azure_storage.py#L160

So, instead of using a connection string for auth (https://django-storages.readthedocs.io/en/latest/backends/azure.html#authentication-settings), we used this:

export AZURE_ACCOUNT_NAME=...
export AZURE_ACCOUNT_KEY=...
export AZURE_CUSTOM_DOMAIN=blabla.com
export AZURE_CONTAINER=...