【发布时间】:2020-02-29 07:54:00
【问题描述】:
我无法使用 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)
【问题讨论】:
标签: python azure rest azure-blob-storage azure-storage-emulator