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

[azure] Allow creation of signed URLs for upload #1414

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ Azure
-----

- Fix ``collectstatic --clear`` (`#1403`_)
- Add ``mode`` kwarg to ``.url()`` to support creation of signed URLs for upload (`#1414`_)

.. _#1399: https://github.com/jschneier/django-storages/pull/1399
.. _#1381: https://github.com/jschneier/django-storages/pull/1381
.. _#1402: https://github.com/jschneier/django-storages/pull/1402
.. _#1403: https://github.com/jschneier/django-storages/pull/1403
.. _#1414: https://github.com/jschneier/django-storages/pull/1414


1.14.3 (2024-05-04)
Expand Down
5 changes: 3 additions & 2 deletions storages/backends/azure_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,10 @@ def _expire_at(self, expire):
# azure expects time in UTC
return datetime.utcnow() + timedelta(seconds=expire)

def url(self, name, expire=None, parameters=None):
def url(self, name, expire=None, parameters=None, mode="r"):
name = self._get_valid_path(name)
params = parameters or {}
permission = BlobSasPermissions.from_string(mode)

if expire is None:
expire = self.expiration_secs
Expand All @@ -326,7 +327,7 @@ def url(self, name, expire=None, parameters=None):
name,
account_key=self.account_key,
user_delegation_key=user_delegation_key,
permission=BlobSasPermissions(read=True),
permission=permission,
expiry=expiry,
**params,
)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ def test_url_expire(self, generate_blob_sas_mocked):
fixed_time = make_aware(
datetime.datetime(2016, 11, 6, 4), datetime.timezone.utc
)

with mock.patch("storages.backends.azure_storage.datetime") as d_mocked:
# Implicit read permission
d_mocked.utcnow.return_value = fixed_time
self.assertEqual(
self.storage.url("some blob", 100),
Expand All @@ -179,6 +181,26 @@ def test_url_expire(self, generate_blob_sas_mocked):
permission=mock.ANY,
expiry=fixed_time + timedelta(seconds=100),
)
called_args, called_kwargs = generate_blob_sas_mocked.call_args
self.assertEqual(str(called_kwargs["permission"]), "r")

# Explicit write permission
d_mocked.utcnow.return_value = fixed_time
self.assertEqual(
self.storage.url("some blob", expire=100, mode="w"),
"https://ret_foo.blob.core.windows.net/test/some%20blob",
)
generate_blob_sas_mocked.assert_called_with(
self.account_name,
self.container_name,
"some blob",
account_key=self.account_key,
user_delegation_key=None,
permission=mock.ANY,
expiry=fixed_time + timedelta(seconds=100),
)
called_args, called_kwargs = generate_blob_sas_mocked.call_args
self.assertEqual(str(called_kwargs["permission"]), "w")

@mock.patch("storages.backends.azure_storage.generate_blob_sas")
def test_url_expire_user_delegation_key(self, generate_blob_sas_mocked):
Expand Down
Loading