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 V
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))