diff --git a/storages/backends/azure_storage.py b/storages/backends/azure_storage.py index c62c91dce..f2fd5e68a 100644 --- a/storages/backends/azure_storage.py +++ b/storages/backends/azure_storage.py @@ -1,4 +1,5 @@ import os.path +import mimetypes from django.core.files.base import ContentFile from django.core.exceptions import ImproperlyConfigured @@ -57,9 +58,20 @@ def size(self, name): return properties["content-length"] def _save(self, name, content): + if hasattr(content.file, 'content_type'): + content_type = content.file.content_type + else: + content_type = mimetypes.guess_type(name)[0] + + if hasattr(content, 'chunks'): + content_data = b''.join(chunk for chunk in content.chunks()) + else: + content_data = content.read() + self.connection.put_blob(self.azure_container, name, - content, "BlockBlob") + content_data, "BlockBlob", + x_ms_blob_content_type=content_type) return name def url(self, name): - return "%s/%s" % (self.azure_bucket, name) + return "{}{}/{}".format(setting('MEDIA_URL'), self.azure_container, name)