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

AzureStorage does not allow creation of signed URLs for upload #1413

Closed
CorentinGoodays opened this issue Jun 27, 2024 · 0 comments · Fixed by #1414
Closed

AzureStorage does not allow creation of signed URLs for upload #1413

CorentinGoodays opened this issue Jun 27, 2024 · 0 comments · Fixed by #1414

Comments

@CorentinGoodays
Copy link
Contributor

CorentinGoodays commented Jun 27, 2024

Summary
The AzureStorage class in the django-storages module does not currently support generating signed URLs for uploads due to the hardcoded BlobSasPermissions(read=True) in the url() method. This limitation restricts users from creating SAS tokens with upload permissions.

Environment
django-storages version: All

Steps to Reproduce
Attempt to generate a signed URL for upload using the AzureStorage class.
Observe that the generated URL only has read permissions.

Expected Behavior
Users should be able to generate signed URLs with different permissions, including upload permissions.

Actual Behavior
The url() method only generates URLs with read permissions, as BlobSasPermissions(read=True) is hardcoded.

Proposed Solution
Allow the url() method to accept a mode parameter to specify different permissions. Here is a possible implementation:

diff --git a/storages/backends/azure_storage.py b/storages/backends/azure_storage.py
index 69b0213..435bc9a 100644
--- a/storages/backends/azure_storage.py
+++ b/storages/backends/azure_storage.py
@@ -309,9 +309,10 @@ class AzureStorage(BaseStorage):
     # 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
@@ -326,7 +327,7 @@ class AzureStorage(BaseStorage):
         name,
         account_key=self.account_key,
         user_delegation_key=user_delegation_key,
-        permission=BlobSasPermissions(read=True),
+        permission=permission,
         expiry=expiry,
         **params,
     )

Testing
I have tested this solution in my environment and confirmed that it allows generating signed URLs with different permissions, including upload permissions. Using these generated URLs, I successfully uploaded a file to Azure Storage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant