gpt4 book ai didi

azure - 使用 python 创建 Azure 存储授权 header

转载 作者:行者123 更新时间:2023-12-03 05:01:27 25 4
gpt4 key购买 nike

我正在尝试创建使用 Azure 存储 REST API 的授权 header 。什么样的恶梦。我尝试这样做的原因是因为我尝试使用工作流构建器 (Alteryx) 来调用 API,因此我唯一的编程选项是 Alteryx、python 或命令行。

我想我已经很接近了,但我只是不明白这篇文章之后的最后三行代码 - https://learn.microsoft.com/en-us/azure/storage/common/storage-rest-api-auth?toc=%2fazure%2fstorage%2fblobs%2ftoc.json

//现在将其转换为字节数组。byte[] SignatureBytes = Encoding.UTF8.GetBytes(MessageSignature);

//创建 HMACSHA256 版本的存储 key 。HMACSHA256 SHA256 = 新 HMACSHA256(Convert.FromBase64String(storageAccountKey));

//计算 SignatureBytes 的哈希值并将其转换为 Base64 字符串。字符串签名 = Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));

所以,如果我正确遵循这一点,我必须创建存储 key 的 SHA256 版本,然后我创建签名字节的 SHA256 哈希值的 SHA256 哈希值?

我目前正在谷歌搜索,但没有走得太远,但基本上尝试使用 python 在 .net 中执行上面相同的操作。

最佳答案

在Python中,你可以只使用这行代码:

signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()

这里是使用 List blobs api 的完整代码:

import requests
import datetime
import hmac
import hashlib
import base64

storage_account_name = 'xx'
storage_account_key = 'xxx'
container_name='aa1'
api_version = '2017-07-29'
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 +'/'+container_name+ '\ncomp:list\nrestype:container'
}

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 = ('https://' + storage_account_name + '.blob.core.windows.net/'+container_name+'?restype=container&comp=list')

r = requests.get(url, headers = headers)
print(r.status_code)
print('\n\n'+r.text)

测试结果:

enter image description here

关于azure - 使用 python 创建 Azure 存储授权 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56369753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com