gpt4 book ai didi

python - 如何使用rest API查询Azure表存储

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

我想访问我的 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访问 key 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>

最佳答案

如果有人正在寻找使用 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://learn.microsoft.com/en-us/rest/api/storageservices/fileservices/table-service-rest-api

关于python - 如何使用rest API查询Azure表存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43422340/

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