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

Improving azure storage #45

Merged
merged 4 commits into from
Sep 8, 2015
Merged
Changes from 3 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
15 changes: 13 additions & 2 deletions storages/backends/azure_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,20 @@ def size(self, name):
return properties["content-length"]

def _save(self, name, content):
if hasattr(content.file, 'content_type'):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes we have a ContentFile right? I know that's what we return from _open but is this going to be the case generally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As i have interpreted the Django docs[1] a custom storage should handle File objects [2]. Thus it should always have a file attribute. This code checks whether the file has a content_type, if not it tries to guess it.

Please correct me if I have not interpreted the docs correctly ;)

[1] https://docs.djangoproject.com/en/1.8/howto/custom-file-storage/
[2] https://docs.djangoproject.com/en/1.8/ref/files/file/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, sorry about that.

content_type = content.file.content_type
else:
content_type = mimetypes.guess_type(name)[0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot to import mimetypes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice spot. I'll fix :)


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)