Azure storage container API request authentication failing with Django app

826 views Asked by At

I am trying to sync the static files of my django application to Azure storage. I am getting an error when I try to write static files to the storage container when running the manage.py collectstatic command.

I am getting the error. The MAC signature found in the HTTP request is not the same as any computed signature.

The common cause for this error is mismatched time signatures on the two servers, but this is not the problem in my case.

I am using the django packages django-azure-storage and azure-sdk-for-python to format the request.

Here is a gist of the http request and responses generated when trying to connect to the azure storage container.

Is there anything that seems wrong from these outputs?

2

There are 2 answers

0
Will Shao - MSFT On

I have downloaded the django packages and Azure SDK following your description. I have coded a sample to reproduce this issue, but it works fine on my side. Below are the steps that I have done: Set up the environment: Python 2.7 and Azure SDK(0.10.0).

1.Trying to use the django-azure-storage It is very frustrating that I didn't import it into my project successfully since this is the first time I used it. Usually, I leverage Azure Python SDK directly. This time I copied storage.py as AzureStorage class in my project.

#need import django contentfile type
from django.core.files.base import ContentFile

#import the AzureStorage Class form my project
from DjangoWP.AzureStorage import AzureStorage
# my local image path
file_path="local.png";
# my Azure storage blob file

def djangorplugin():
    azurestorage=AzureStorage(myaccount, mykey,"mycontainer")

    stream=open(file_path, 'rb')    
    data = stream.read()
    #need convert file to ContentFile
    azurestorage.save("Testfile1.png",ContentFile(data))

enter image description here 2.You many want to know how to use Azure SDK for Python directly, below code snippet for your reference:

from azure.storage.blobservice import BlobService
#my local image path
file_path="local.png";   
def upload():
    blob_service = BlobService(account_name=myaccount, account_key=mykey)   
    stream=open(file_path, 'rb')    
    data = stream.read()
    blob_service.put_blob("mycontainer","local.png",data,"BlockBlob")

If you have any further concerns, please feel free to let us know.

0
roob On

I was incorrectly using the setting DEFAULT_FILE_STORAGE instead of STATICFILES_STORAGE to override the storage backend used while syncing static files. Changing this setting solved this problem.

I was also encountering problems when trying to use django-storages, which specifies to use the DEFAULT_FILE_STORAGE setting in its documentation. However, using STATICFILES_STORAGE with this package also fixed the issue I was having.