我想在azure存储帐户中访问我的表。
import requests
import hashlib
import base64
import hmac
import datetime
storageAccountName = 'rishistorage1234' # your storage account name
storageKey='my-account-key'# your storage account access key
url = 'https://' + storageAccountName + '.table.core.windows.net/table1'
version = '2016-05-31' # x-ms-version
date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT") #x-ms-date
parameters = 'table1'
CanonicalizedResources = '/' + storageAccountName + '/' + parameters
CanonicalizedHeaders = 'x-ms-date:' + date
stringToSign = 'GET\n\n\n\n\n' + CanonicalizedHeaders + '\n' + CanonicalizedResources
# note the b64decode of the storageKey
signature = base64.b64encode(hmac.new(base64.b64decode(storageKey),
msg=stringToSign, digestmod=hashlib.sha256).digest())
headers = {'x-ms-date': date,
'x-ms-version': version,
'Authorization': 'SharedKeyLite ' + storageAccountName + ':' +
signature}
# send the request
#print signature
response = requests.get(url, headers=headers)
print response
print response.headers
print response.content
抱歉,我无法将所有内容复制为"代码"并请求忽略缩进错误。
表的名称是table1
存储帐户的名称为rishistorage1234
Acces键1为my-account-key
。
我得到的回应是
<Response [403]>
{'Content-Length': '419', 'Access-Control-Expose-Headers': 'x-ms-request-id,Content-Length,Date,Transfer-Encoding', 'x-ms-request-id': 'e3b01b8c-0002-0024-0d3d-b71dc2000000', 'Server': 'Microsoft-HTTPAPI/2.0', 'Date': 'Mon, 17 Apr 2017 05:40:23 GMT', 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/xml'}
<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code>AuthenticationFailed</m:code><m:message xml:lang="en-US">Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:e3b01b8c-0002-0024-0d3d-b71dc2000000
Time:2017-04-17T05:40:24.3250398Z</m:message></m:error>
答案 0 :(得分:1)
如果有人正在寻找使用REST API从azure表存储查询表的工作python代码,这里是代码
import requests
import hashlib
import base64
import hmac
import datetime
storageAccountName = 'my-account-name' # your storage account name
storageKey='my-account-key'# your storage account access key
url = 'https://' + storageAccountName + '.table.core.windows.net/table-name'
version = '2016-05-31' # x-ms-version
date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT") #x-ms-date
parameters = 'table-name'
CanonicalizedResources = '/' + storageAccountName + '/' + parameters
CanonicalizedHeaders = 'x-ms-date:' + date
stringToSign = date + '\n' + CanonicalizedResources
# note the b64decode of the storageKey
signature = base64.b64encode(hmac.new(base64.b64decode(storageKey),
msg=stringToSign, digestmod=hashlib.sha256).digest())
headers = {'x-ms-date': date,
'x-ms-version': version,
'Authorization': 'SharedKeyLite ' + storageAccountName + ':' +
signature,
'Accept': 'application/json;odata=nometadata '}
# send the request
#print signature
response = requests.get(url, headers=headers)
print response
print response.headers
print response.content
有关更多操作,请参阅此处 https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/table-service-rest-api
答案 1 :(得分:0)
根据您的错误信息&amp;代码,我确定问题是由于StringToSign
格式中使用不正确的SharedKeyLite
格式导致Azure Table服务导致的,StringToSign
格式适用于Blob,而不适用于Table,如下所示
您的错误代码&amp;信息属于AuthenticationFailed
,请参阅here。
Azure Table服务的正确StringToSign
格式应如下所示,例如StringToSign = Date + "\n"+ CanonicalizedResource
。