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

前端 未结 1 1512
广开言路
广开言路 2021-01-13 00:51

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

相关标签:
1条回答
  • 2021-01-13 01:31

    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))
    
    0 讨论(0)
提交回复
热议问题