Skip to content

Commit

Permalink
[azure] Add overrideable method to customize parameters on a per-key …
Browse files Browse the repository at this point in the history
…basis (#898)
  • Loading branch information
drnextgis committed Dec 15, 2020
1 parent 7771782 commit 3fbad31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions docs/backends/azure.rst
Expand Up @@ -165,3 +165,8 @@ The following settings are available:

A variable to set the Cache-Control HTTP response header. E.g.
``AZURE_CACHE_CONTROL = "public,max-age=31536000,immutable"``

``AZURE_OBJECT_PARAMETERS``

Use this to set content settings on all objects. To set these on a per-object
basis, subclass the backend and override ``AzureStorage.get_object_parameters``.
37 changes: 28 additions & 9 deletions storages/backends/azure_storage.py
Expand Up @@ -129,6 +129,7 @@ def get_default_settings(self):
return {
"account_name": setting("AZURE_ACCOUNT_NAME"),
"account_key": setting("AZURE_ACCOUNT_KEY"),
"object_parameters": setting("AZURE_OBJECT_PARAMETERS", {}),
"azure_container": setting("AZURE_CONTAINER"),
"azure_ssl": setting("AZURE_SSL", True),
"upload_max_conn": setting("AZURE_UPLOAD_MAX_CONN", 2),
Expand Down Expand Up @@ -242,11 +243,7 @@ def size(self, name):
def _save(self, name, content):
cleaned_name = clean_name(name)
name = self._get_valid_path(name)
guessed_type, content_encoding = mimetypes.guess_type(name)
content_type = (
_content_type(content) or
guessed_type or
self.default_content_type)
params = self._get_content_settings_parameters(name, content)

# Unwrap django file (wrapped by parent's save call)
if isinstance(content, File):
Expand All @@ -257,10 +254,7 @@ def _save(self, name, content):
container_name=self.azure_container,
blob_name=name,
stream=content,
content_settings=ContentSettings(
content_type=content_type,
content_encoding=content_encoding,
cache_control=self.cache_control),
content_settings=ContentSettings(**params),
max_connections=self.upload_max_conn,
timeout=self.timeout)
return cleaned_name
Expand All @@ -287,6 +281,31 @@ def url(self, name, expire=None):
protocol=self.azure_protocol,
**make_blob_url_kwargs)

def _get_content_settings_parameters(self, name, content=None):
params = {}

guessed_type, content_encoding = mimetypes.guess_type(name)
content_type = (
_content_type(content) or
guessed_type or
self.default_content_type)

params['cache_control'] = self.cache_control
params['content_type'] = content_type
params['content_encoding'] = content_encoding

params.update(self.get_object_parameters(name))
return params

def get_object_parameters(self, name):
"""
Returns a dictionary that is passed to content settings. Override this
method to adjust this on a per-object basis to set e.g ContentDisposition.
By default, returns the value of AZURE_OBJECT_PARAMETERS.
"""
return self.object_parameters.copy()

def get_modified_time(self, name):
"""
Returns an (aware) datetime object containing the last modified time if
Expand Down

0 comments on commit 3fbad31

Please sign in to comment.