python - 通过 REST API 和 Python 连接到 Azure 存储模拟器
问题描述
我无法使用 REST 和 Python 连接到 Azure 存储模拟器。
我可以使用 Python SDK
我想尝试进行一些 REST 调用,以更好地理解功能、比较速度并考虑在云功能中使用以减小图像大小。
我尝试使用此处找到的代码https://stackoverflow.com/a/49881347/9201100,但是当与模拟器一起使用时,我不断返回 AuthenticationFailed
我在 github https://github.com/Azure-Samples/storage-dotnet-rest-api-with-auth上找到了一个最近的 C# 项目。如果我包含模拟器 account_name 和 account_key 它运行得很好。
所以我稍微修改了代码,更新了 api_version = '2017-04-17' 和规范化资源以匹配,但仍然没有好处。
请帮忙
import datetime
import hmac
import hashlib
import base64
storage_account_name = "devstoreaccount1"
storage_account_key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
api_version = '2017-04-17'
request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
string_params = {
'verb': 'GET',
'Content-Encoding': '',
'Content-Language': '',
'Content-Length': '',
'Content-MD5': '',
'Content-Type': '',
'Date': '',
'If-Modified-Since': '',
'If-Match': '',
'If-None-Match': '',
'If-Unmodified-Since': '',
'Range': '',
'CanonicalizedHeaders': 'x-ms-date:' + request_time + '\nx-ms-version:' + api_version + '\n',
'CanonicalizedResource': '/' + storage_account_name + '/' + storage_account_name + '\ncomp=list'
}
string_to_sign = (string_params['verb'] + '\n'
+ string_params['Content-Encoding'] + '\n'
+ string_params['Content-Language'] + '\n'
+ string_params['Content-Length'] + '\n'
+ string_params['Content-MD5'] + '\n'
+ string_params['Content-Type'] + '\n'
+ string_params['Date'] + '\n'
+ string_params['If-Modified-Since'] + '\n'
+ string_params['If-Match'] + '\n'
+ string_params['If-None-Match'] + '\n'
+ string_params['If-Unmodified-Since'] + '\n'
+ string_params['Range'] + '\n'
+ string_params['CanonicalizedHeaders']
+ string_params['CanonicalizedResource'])
signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()
authHV= {'SharedKey ' + storage_account_name + ':' + signed_string}
headers = {
'x-ms-date' : request_time,
'x-ms-version' : api_version,
'Authorization' : authHV
}
url = ('http://localhost:10000/' + storage_account_name + '?comp=list')
#
r = requests.get(url, headers = headers)
print(r.content)
解决方案
您的代码中有 2 个错误:
1.在'CanonicalizedResource'中,你应该使用'\ncomp:list'
而不是'\ncomp=ist'
2.对于变量authHV
,您应该将其定义为authHV=('SharedKey ' + storage_account_name + ':' + signed_string)
并且还记得import requests
模块。
请使用下面的代码,它适用于我:
import requests
import datetime
import hmac
import hashlib
import base64
storage_account_name = 'devstoreaccount1'
storage_account_key = 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=='
#api_version = '2016-05-31'
api_version = '2017-04-17'
request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
string_params = {
'verb': 'GET',
'Content-Encoding': '',
'Content-Language': '',
'Content-Length': '',
'Content-MD5': '',
'Content-Type': '',
'Date': '',
'If-Modified-Since': '',
'If-Match': '',
'If-None-Match': '',
'If-Unmodified-Since': '',
'Range': '',
'CanonicalizedHeaders': 'x-ms-date:' + request_time + '\nx-ms-version:' + api_version + '\n',
'CanonicalizedResource': '/' + storage_account_name +'/'+storage_account_name + '\ncomp:list' #note, it should be '\ncomp:list', no '/'
}
string_to_sign = (string_params['verb'] + '\n'
+ string_params['Content-Encoding'] + '\n'
+ string_params['Content-Language'] + '\n'
+ string_params['Content-Length'] + '\n'
+ string_params['Content-MD5'] + '\n'
+ string_params['Content-Type'] + '\n'
+ string_params['Date'] + '\n'
+ string_params['If-Modified-Since'] + '\n'
+ string_params['If-Match'] + '\n'
+ string_params['If-None-Match'] + '\n'
+ string_params['If-Unmodified-Since'] + '\n'
+ string_params['Range'] + '\n'
+ string_params['CanonicalizedHeaders']
+ string_params['CanonicalizedResource'])
signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()
headers = {
'x-ms-date' : request_time,
'x-ms-version' : api_version,
'Authorization' : ('SharedKey ' + storage_account_name + ':' + signed_string)
}
url = ('http://127.0.0.1:10000/' + storage_account_name + '?comp=list')
r = requests.get(url, headers = headers)
print(r.status_code)
print(r.content)
测试结果如下:
推荐阅读
- javascript - 无法使用 switch 语句在函数之间切换
- flutter - 如何创建 StreamBuilder 的实例
- javascript - 如何在 Chrome 扩展程序中启用和禁用事件侦听器?
- tensorflow - Tensorflow 对象检测 api - 获取训练和评估数据集的准确性和损失
- python - Python Pandas 从 excel 导入数据后删除空白区域
- php - 在 PHP 中获取单个 FileMaker 记录 ID
- angular - 如何根据所选国家/地区以角度显示城市
- reactjs - getServerSideProps 返回布尔值时序列化错误
- javascript - V 选择下拉位置看起来不正确
- linux-kernel - 使用 allmodconfig 编译时未定义对 ipv6_dev_find 的引用,使用 allyesconfig 编译时不存在