通过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)
a281409081 回答:通过REST API和Python连接到Azure存储模拟器

您的代码中有2个错误:

1。在“规范化资源”中,您应该使用'\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)

测试结果如下:

enter image description here

本文链接:https://www.f2er.com/3169586.html

大家都在问