Does anyone have any tips on using azure-storage directly with Django? I ask because currently I'm trying to set up Azure Cloud Storage for my Django app (hosted on Azure VM with Ubuntu OS), and django-storages
doesn't seem to be interfacing with Azure Storage SDK correctly (known issue: see here). The fix listed there won't work for me given my Django version is < 1.6.2.
Thus I'll need to use Azure-storage directly with Django. Has anyone set that up before? I need to save images and mp3s on the 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 hook django-storages up with 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 relevant snippet of the code is:
def exists(self, name):
try:
self.connection.get_blob_properties(
self.azure_container, name)
except azure.WindowsAzureMissingResourceError:
return False
else:
return True
Seems that the connection to the Azure container is failing. 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 earlier, I need a solution that bypasses django-storages entirely, and just relies on Azure Storage SDK to get the job done.
Note: ask me for more information in case you need it.
We can directly use Azure-Storage python SDK in the Django apps just like using the sdk in common python applications. You can refer to official guide for getting started.
Here is the test code snippet in the Django app:
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With