简体   繁体   English

Azure存储容器API请求身份验证因Django应用程序失败

[英]Azure storage container API request authentication failing with Django app

I am trying to sync the static files of my django application to Azure storage. 我正在尝试将django应用程序的静态文件同步到Azure存储。 I am getting an error when I try to write static files to the storage container when running the manage.py collectstatic command. 我在运行manage.py collectstatic命令时尝试将静态文件写入存储容器时遇到错误。

I am getting the error. 我得到了错误。 The MAC signature found in the HTTP request is not the same as any computed signature. HTTP请求中找到的MAC签名与任何计算出的签名都不相同。

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. 我正在使用django软件包django-azure-storageazure-sdk-for-python格式化请求。

Here is a gist of the http request and responses generated when trying to connect to the azure storage container. 这是尝试连接到Azure存储容器时所生成的http请求和响应的摘要。

Is there anything that seems wrong from these outputs? 这些输出中有什么地方看起来有问题吗?

I have downloaded the django packages and Azure SDK following your description. 我已经按照您的描述下载了django软件包和Azure SDK。 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). 下面是我已完成的步骤:设置环境:Python 2.7和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. 1.尝试使用django-azure-storage令人沮丧的是,由于这是我第一次使用它,因此没有将其成功导入到项目中。 Usually, I leverage Azure Python SDK directly . 通常,我直接利用Azure Python SDK This time I copied storage.py as AzureStorage class in my project. 这次我在项目中将storage.py复制为AzureStorage类。

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

在此处输入图片说明 2.You many want to know how to use Azure SDK for Python directly, below code snippet for your reference: 2.您很多人想知道如何直接在Python代码段下面使用Azure SDK,以供参考:

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. 如果您还有其他疑问,请随时告诉我们。

I was incorrectly using the setting DEFAULT_FILE_STORAGE instead of STATICFILES_STORAGE to override the storage backend used while syncing static files. 我错误地使用了设置DEFAULT_FILE_STORAGE而不是STATICFILES_STORAGE来覆盖同步静态文件时使用的存储后端。 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. 尝试使用django-storages时 ,我也遇到了问题,它在其文档中指定使用DEFAULT_FILE_STORAGE设置。 However, using STATICFILES_STORAGE with this package also fixed the issue I was having. 但是,将此包与STATICFILES_STORAGE一起使用也可以解决我遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM