使用 Python SDK 的 Azure 存储错误

Vit*_*sov 3 python azure

我已将适用于 python 的 Azure SDK 下载到我的 Ubuntu(工具版本 0.8.16)。我正在尝试运行此代码

from azure.storage import BlobService
blob_service = BlobService(account_name='Real_Name', account_key='Real_Key')
blob_service.create_container('mycontainer')
blob_service.create_container('mycontainer', x_ms_blob_public_access='container') 

blob_service.put_block_blob_from_path(
    'mycontainer',
    'myblob',
    'sunset.png',
    x_ms_blob_content_type='image/png'
)
Run Code Online (Sandbox Code Playgroud)

我已经将它保存在 Test.py 下,我正在尝试运行它

python Test.py
Run Code Online (Sandbox Code Playgroud)

在终端中,我收到此错误

Traceback (most recent call last):
  File "Test.py", line 3, in <module>
    blob_service.create_container('mycontainer')
  File "/home/parallels/azure-sdk-for-python/azure/storage/blobservice.py", line 203, in create_container
    _dont_fail_on_exist(ex)
  File "/home/parallels/azure-sdk-for-python/azure/__init__.py", line 525, in _dont_fail_on_exist
    raise error
azure.WindowsAzureError: Unknown error (Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.)
<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:98ce37f9-0001-003f-350e-7d3666000000
Time:2015-03-24T10:07:00.3385327Z</Message><AuthenticationErrorDetail>Request date header too old: 'Tue, 24 Mar 2015 09:06:59 GMT'</AuthenticationErrorDetail></Error>
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题,我做的一切都正确吗?

谢谢

cba*_*are 9

从睡眠状态恢复 WSL 2(Linux 的 Windows 子系统)会话然后从 bash/zsh 命令行使用 Azure cli 后,由于时钟偏差,我遇到了类似的错误。这修复了时钟:

sudo hwclock -s -v
Run Code Online (Sandbox Code Playgroud)

请参阅:使用 WSL 2 修复时钟偏差

更新:此错误似乎已在 WSL 内核版本 5.10.16.3-microsoft-standard-WSL2 中修复。升级后就没有看到了。请参阅:https ://github.com/microsoft/WSL/issues/5014#issuecomment-605243281


Gau*_*tri 6

正如错误中提到的:

请求日期标头太旧:“2015 年 3 月 24 日星期二 09:06:59 GMT”

如果您计算机上的日期/时间(以 UTC 为单位)与 Azure 存储服务器上的日期/时间之间的差异超过 15-20 分钟(您计算机的日期/时间落后于 Azure 的日期/时间),您将收到此错误。

更新

本质上,当请求发送到 Azure 存储服务(例如创建容器)时,SDK 获取计算机的本地日期/时间,将其转换为 UTC,然后将其与请求一起发送到 Azure(作为请求标头)日期或 x-ms-date)。收到请求后,Azure 存储服务会将此日期/时间与其日期/时间进行比较,如果差异超过 15-20 分钟(您的时钟落后),Azure 存储服务会抛出您遇到的错误。这是作为一种安全措施完成的,以便在有人掌握您的请求时无法一遍又一遍地重播请求。