Using the Azure Storage SDK with Django (and completely eliminating the dependency on django storage)

Does anyone have any tips on using azure-storage directly with Django? I ask because I'm currently trying to configure Azure Cloud Storage for my Django application (hosted on Azure VM with Ubuntu OS), and django-storages does not seem to interact correctly with the Azure Storage SDK (known issue: see here ) . The fix pointed there will not work for me as my version of Django is <1.6.2.

Thus, I will need to use Azure storage directly with Django. Has anyone asked this before? I need to save images and mp3 to cloud storage.


Currently in my models.py I have:

 def upload_to_location(instance, filename): try: blocks = filename.split('.') ext = blocks[-1] filename = "%s.%s" % (uuid.uuid4(), ext) instance.title = blocks[0] return os.path.join('uploads/', filename) except Exception as e: print '%s (%s)' % (e.message, type(e)) return 0 class Photo(models.Model): description = models.TextField(validators=[MaxLengthValidator(500)]) submitted_on = models.DateTimeField(auto_now_add=True) image_file = models.ImageField(upload_to=upload_to_location, null=True, blank=True ) 

And then django-storages and boto take care of the rest. However, when I connect django storages to Azure Cloud Storage, I get the following error:

 Exception Value: 'module' object has no attribute 'WindowsAzureMissingResourceError' Exception Location: /home/mhb11/.virtualenvs/myvirtualenv/local/lib/python2.7/site-packages/storages/backends/azure_storage.py in exists, line 46 

And the corresponding code snippet:

 def exists(self, name): try: self.connection.get_blob_properties( self.azure_container, name) except azure.WindowsAzureMissingResourceError: return False else: return True 

The connection to the Azure container does not seem to work. In my settings.py, I have:

  DEFAULT_FILE_STORAGE = 'storages.backends.azure_storage.AzureStorage' AZURE_ACCOUNT_NAME = 'photodatabasestorage' AZURE_ACCOUNT_KEY = 'something' AZURE_CONTAINER = 'somecontainer' 

As described above, I need a solution that completely bypasses the django repository and just uses the Azure Storage SDK to do the job.

Note: ask me for more information if you need it.

+5
source share
1 answer

We can directly use the Azure-Storage python SDK in Django applications, similar to using sdk in regular python applications. You can refer to the official manual to get started.

Here is a snippet of test code in a Django application:

 def putfiles(request): blob_service = BlobService(account_name=accountName, account_key=accountKey) PROJECT_ROOT = path.dirname(path.abspath(path.dirname(__file__))) try: blob_service.put_block_blob_from_path( 'mycontainer', '123.jpg', path.join(path.join(PROJECT_ROOT,'uploads'),'123.jpg'), x_ms_blob_content_type='image/jpg' ) result = True except: print(sys.exc_info()[1]) result = False return HttpResponse(result) def listfiles(request): blob_service = BlobService(account_name=accountName, account_key=accountKey) blobs = [] result = [] marker = None while True: batch = blob_service.list_blobs('mycontainer', marker=marker) blobs.extend(batch) if not batch.next_marker: break marker = batch.next_marker for blob in blobs: result.extend([{'name':blob.name}]) return HttpResponse(json.dumps(result)) 
+2
source

Source: https://habr.com/ru/post/1238126/


All Articles