Commits

Andrei Coman committed 1390fbd

Added initial support for Windows Azure Storage

Comments (0)

Files changed (2)

docs/backends/azure.rst

+Azure Storage
+====
+
+A custom storage system for Django using Windows Azure Storage backend.
+
+
+Settings
+====
+
+``DEFAULT_FILE_STORAGE``
+
+This setting sets the path to the Azure storage class::
+
+    DEFAULT_FILE_STORAGE = 'storages.backends.azure_storage.AzureStorage'
+
+
+``AZURE_ACCOUNT_NAME``
+
+This setting is the Windows Azure Storage Account name, which in many cases is also the first part of the url for instance: http://azure_account_name.blob.core.windows.net/ would mean::
+   
+   AZURE_ACCOUNT_NAME = "azure_account_name"
+
+``AZURE_ACCOUNT_KEY``
+
+This is the private key that gives your Django app access to your Windows Azure Account.
+
+``AZURE_CONTAINER``
+
+This is where the files uploaded through your Django app will be uploaded. 
+The container must be already created as the storage system will not attempt to create it.
+
+

storages/backends/azure_storage.py

+import os.path
+
+from django.conf import settings
+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")
+
+
+def clean_name(name):
+    return os.path.normpath(name).replace("\\", "/")
+
+
+def setting(name, default=None):
+    return getattr(settings, name, default)
+
+
+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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.