如何使用 rest API 查询 Azure table 存储

How to query Azure table storage using rest API

我想在 Azure 存储帐户中访问我的 table。

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

抱歉,我无法将所有内容复制为 "code",请忽略缩进错误。 table 的名称是 table1 存储帐户的名称是 risistorage1234 访问键 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>

根据您的错误信息和代码,我确定问题是由于在 SharedKeyLite 中为 Azure Table 服务使用了不正确的 StringToSign 格式引起的,您的 StringToSign 格式适用于 Blob,不适用于 Table,如下所示。

您的错误代码和信息属于AuthenticationFailed,请参阅here

Azure Table 服务的正确 StringToSign 格式应如下所示,例如 StringToSign = Date + "\n"+ CanonicalizedResource

如果有人正在寻找使用 REST API 从天蓝色 table 存储查询 table 的工作 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