Using Azure Storage SDK with Django (and removing dependency on django-storages entirely) Using Azure Storage SDK with Django (and removing dependency on django-storages entirely) azure azure

Using Azure Storage SDK with Django (and removing dependency on django-storages entirely)


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 = Trueexcept:    print(sys.exc_info()[1])    result = Falsereturn 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))