1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import os.path
from django.core.files.base import ContentFile
from django.core.files.storage import Storage
from django.core.exceptions import ImproperlyConfigured
try:
import azure
import azure.storage
except ImportError:
raise ImproperlyConfigured(
"Could not load Azure bindings. "
"See https://github.com/WindowsAzure/azure-sdk-for-python")
from storages.utils import setting
def clean_name(name):
return os.path.normpath(name).replace("\\", "/")
class AzureStorage(Storage):
account_name = setting("AZURE_ACCOUNT_NAME")
account_key = setting("AZURE_ACCOUNT_KEY")
azure_container = setting("AZURE_CONTAINER")
def __init__(self, *args, **kwargs):
super(AzureStorage, self).__init__(*args, **kwargs)
self._connection = None
@property
def connection(self):
if self._connection is None:
self._connection = azure.storage.BlobService(
self.account_name, self.account_key)
return self._connection
def _open(self, name, mode="rb"):
contents = self.connection.get_blob(self.azure_container, name)
return ContentFile(contents)
def exists(self, name):
try:
self.connection.get_blob_properties(
self.azure_container, name)
except azure.WindowsAzureMissingResourceError:
return False
else:
return True
def delete(self, name):
self.connection.delete_blob(self.azure_container, name)
def size(self, name):
properties = self.connection.get_blob_properties(
self.azure_container, name)
return properties["content-length"]
def _save(self, name, content):
self.connection.put_blob(self.azure_container, name,
content, "BlockBlob")
return name
def url(self, name):
return "%s/%s" % (self.azure_bucket, name)
|