|
| 1 | +from azure import WindowsAzureMissingResourceError |
| 2 | +from azure.storage import BlobService |
| 3 | + |
| 4 | +from django.core.files.storage import Storage |
| 5 | +from django.conf import settings |
| 6 | + |
| 7 | + |
| 8 | +class AzureStorage(Storage): |
| 9 | + """ |
| 10 | + Custom file storage system for Azure |
| 11 | + """ |
| 12 | + |
| 13 | + container = settings.AZURE_STORAGE['CONTAINER'] |
| 14 | + account_name = settings.AZURE_STORAGE['ACCOUNT_NAME'] |
| 15 | + account_key = settings.AZURE_STORAGE['ACCOUNT_KEY'] |
| 16 | + cdn_host = None |
| 17 | + use_ssl = settings.AZURE_STORAGE['USE_SSL'] |
| 18 | + |
| 19 | + def __init__(self, account_name=None, account_key=None, container=None, |
| 20 | + use_ssl=None): |
| 21 | + |
| 22 | + if account_name is not None: |
| 23 | + self.account_name = account_name |
| 24 | + |
| 25 | + if account_key is not None: |
| 26 | + self.account_key = account_key |
| 27 | + |
| 28 | + if container is not None: |
| 29 | + self.container = container |
| 30 | + |
| 31 | + if use_ssl is not None: |
| 32 | + self.use_ssl = use_ssl |
| 33 | + |
| 34 | + def __getstate__(self): |
| 35 | + return dict(account_name=self.account_name, |
| 36 | + account_key=self.account_key, container=self.container, |
| 37 | + cdn_host=self.cdn_host, use_ssl=self.use_ssl) |
| 38 | + |
| 39 | + def _get_protocol(self): |
| 40 | + if self.use_ssl: |
| 41 | + return 'https' |
| 42 | + |
| 43 | + return 'http' |
| 44 | + |
| 45 | + def _get_service(self): |
| 46 | + if not hasattr(self, '_blob_service'): |
| 47 | + self._blob_service = BlobService(account_name=self.account_name, |
| 48 | + account_key=self.account_key, protocol=self._get_protocol()) |
| 49 | + |
| 50 | + return self._blob_service |
| 51 | + |
| 52 | + def _get_container_url(self): |
| 53 | + if self.cdn_host is not None: |
| 54 | + return self.cdn_host |
| 55 | + |
| 56 | + if not hasattr(self, '_container_url'): |
| 57 | + self._container_url = '%s://%s/%s' % (self._get_protocol(), |
| 58 | + self._get_service()._get_host(), self.container) |
| 59 | + |
| 60 | + return self._container_url |
| 61 | + |
| 62 | + def _get_properties(self, name): |
| 63 | + return self._get_service().get_blob_properties(self.container, |
| 64 | + name) |
| 65 | + |
| 66 | + def _get_file_obj(self, name): |
| 67 | + """ |
| 68 | + Helper function to get retrieve the requested Cloud Files Object. |
| 69 | + """ |
| 70 | + return self._get_service().get_blob(self.container, name) |
| 71 | + |
| 72 | + def _open(self, name, mode='rb'): |
| 73 | + """ |
| 74 | + Return the AzureStorageFile. |
| 75 | + """ |
| 76 | + |
| 77 | + from django.core.files.base import ContentFile |
| 78 | + |
| 79 | + contents = self._get_service().get_blob(self.container, name) |
| 80 | + |
| 81 | + return ContentFile(contents) |
| 82 | + |
| 83 | + def _save(self, name, content): |
| 84 | + """ |
| 85 | + Use the Azure Storage service to write ``content`` to a remote file |
| 86 | + (called ``name``). |
| 87 | + """ |
| 88 | + import mimetypes |
| 89 | + |
| 90 | + content.open() |
| 91 | + |
| 92 | + content_type = None |
| 93 | + |
| 94 | + if hasattr(content.file, 'content_type'): |
| 95 | + content_type = content.file.content_type |
| 96 | + else: |
| 97 | + content_type = mimetypes.guess_type(name)[0] |
| 98 | + |
| 99 | + if hasattr(content, 'chunks'): |
| 100 | + content_str = ''.join(chunk for chunk in content.chunks()) |
| 101 | + else: |
| 102 | + content_str = content.read() |
| 103 | + |
| 104 | + cache_control = self.get_cache_control(self.container, name, |
| 105 | + content_type) |
| 106 | + |
| 107 | + self._get_service().put_blob(self.container, name, content_str, |
| 108 | + x_ms_blob_type="BlockBlob", |
| 109 | + x_ms_blob_content_type=content_type, |
| 110 | + cache_control=cache_control, |
| 111 | + x_ms_blob_cache_control=cache_control) |
| 112 | + |
| 113 | + content.close() |
| 114 | + |
| 115 | + return name |
| 116 | + |
| 117 | + def listdir(self, path): |
| 118 | + """ |
| 119 | + Lists the contents of the specified path, returning a 2-tuple of lists; |
| 120 | + the first item being directories, the second item being files. |
| 121 | + """ |
| 122 | + |
| 123 | + files = [] |
| 124 | + |
| 125 | + if path and not path.endswith('/'): |
| 126 | + path = '%s/' % path |
| 127 | + |
| 128 | + path_len = len(path) |
| 129 | + |
| 130 | + blob_list = self._get_service().list_blobs(self.container, prefix=path) |
| 131 | + |
| 132 | + for name in blob_list: |
| 133 | + files.append(name[path_len:]) |
| 134 | + |
| 135 | + return ([], files) |
| 136 | + |
| 137 | + def exists(self, name): |
| 138 | + """ |
| 139 | + Returns True if a file referenced by the given name already exists in |
| 140 | + the storage system, or False if the name is available for a new file. |
| 141 | + """ |
| 142 | + try: |
| 143 | + self._get_properties(name) |
| 144 | + |
| 145 | + return True |
| 146 | + except WindowsAzureMissingResourceError: |
| 147 | + return False |
| 148 | + |
| 149 | + def delete(self, name): |
| 150 | + """ |
| 151 | + Deletes the file referenced by name. |
| 152 | + """ |
| 153 | + |
| 154 | + try: |
| 155 | + self._get_service().delete_blob(self.container, name) |
| 156 | + except WindowsAzureMissingResourceError: |
| 157 | + pass |
| 158 | + |
| 159 | + def get_cache_control(self, container, name, content_type): |
| 160 | + """ |
| 161 | + Get the Cache-Control value for a blob, used when saving the blob on |
| 162 | + Azure. Returns `None` by default to remain compatible with the |
| 163 | + default setting for the SDK. |
| 164 | + """ |
| 165 | + |
| 166 | + return None |
| 167 | + |
| 168 | + def size(self, name): |
| 169 | + """ |
| 170 | + Returns the total size, in bytes, of the file referenced by name. |
| 171 | + """ |
| 172 | + |
| 173 | + try: |
| 174 | + properties = self._get_properties(name) |
| 175 | + |
| 176 | + return int(properties['content-length']) |
| 177 | + except WindowsAzureMissingResourceError: |
| 178 | + pass |
| 179 | + |
| 180 | + def url(self, name): |
| 181 | + """ |
| 182 | + Returns the URL where the contents of the file referenced by name can |
| 183 | + be accessed. |
| 184 | + """ |
| 185 | + |
| 186 | + return '%s/%s' % (self._get_container_url(), name) |
| 187 | + |
| 188 | + def modified_time(self, name): |
| 189 | + """ |
| 190 | + Returns a datetime object containing the last modified time. |
| 191 | + """ |
| 192 | + |
| 193 | + import datetime |
| 194 | + |
| 195 | + try: |
| 196 | + properties = self._get_properties(name) |
| 197 | + |
| 198 | + return datetime.datetime.strptime(properties['last-modified'], |
| 199 | + '%a, %d %b %Y %H:%M:%S %Z') |
| 200 | + except WindowsAzureMissingResourceError: |
| 201 | + pass |
0 commit comments