繁体   English   中英

如何使用Rest API查询Azure表存储

[英]How to query Azure table storage using rest API

我想在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>

如果有人在寻找可工作的python代码,以使用REST API从azure表存储中查询表,则为以下代码

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/zh-cn/rest/api/storageservices/fileservices/table-service-rest-api

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

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

在此处输入图片说明

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM