gpt4 book ai didi

rest - 无法通过python连接到azure文件服务REST api

转载 作者:行者123 更新时间:2023-12-01 04:43:18 25 4
gpt4 key购买 nike

我想通过 python 请求访问我的文件服务,由 list shares page 引导,我是 REST 和 python Request 的新手。我的输入是

headers= {'x-ms-date': '2018-04-17 06:22:15.181620', 'Authorization': 'SharedKey zedongstorageaccount:codecodecodeFiTzubX9tvC3G3PcDYzR2cX/TMjkOu4JhsvQffS+xTDDBQ==', 'x-ms-version': '2017-07-29'}
url = 'https://zedongstorageaccount.file.core.windows.net/?comp=list'
r=requests.get(url,headers=headers)

但是出现错误,r.content的输出:

b'\xef\xbb\xbf<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:ef98f282-f01a-0042-3e24-d62397000000\nTime:2018-04-17T08:16:21.9635335Z</Message><AuthenticationErrorDetail>The Date header in the request is incorrect.</AuthenticationErrorDetail></Error>'

我还尝试了标题 {'x-ms-date': '2018-04-17 06:22:15.181620', 'Authorization': 'SharedKey zedongnewstorageaccount:NJYYtabOIj5D1R+xb0PpmXrjCldf6NA6oLkYReAKFiTzubX9tvC3G3PcDYz R2cX/TMjkOu4JhsvQffS+xTDDBQ= =', 'x-ms-version': '2017-07-29'},因为我不知道Authorizationaccount是否在同一条线。我还尝试了许多版本的x-ms-version。但所有 400 或 403 都做出了回应。

我已阅读 Authentication for the Azure Storage Services但很困惑。

哪里出错了?我是否可以学习通过Azure REST API构建我的应用程序?(我使用关键字谷歌,所有页面都是关于构建REST API和azure官方文档)

最佳答案

我还建议使用 Python SDK,但出于教育目的:

import requests
import datetime
import hmac
import hashlib
import base64

storage_account_name = 'teststorageaccount'
storage_account_key = 'D4x6YChpyfqWk9a.......'
api_version = '2016-05-31'
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 + '/\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()

headers = {
'x-ms-date' : request_time,
'x-ms-version' : api_version,
'Authorization' : ('SharedKey ' + storage_account_name + ':' + signed_string)
}

url = ('https://' + storage_account_name + '.file.core.windows.net/?comp=list')

r = requests.get(url, headers = headers)

print(r.content)

输出:

<?xml version="1.0" encoding="utf-8"?><EnumerationResults ServiceEndpoint="https://teststorageaccount.file.core.windows.net/"><Shares><Share><Name>myfileshare</Name><Properties><Last-Modified>Tue, 17 Apr 2018 13:43:00 GMT</Last-Modified><Etag>"0x8D5A46922CA8BDA"</Etag><Quota>10</Quota></Properties></Share></Shares><NextMarker /></EnumerationResults>

关于rest - 无法通过python连接到azure文件服务REST api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49874783/

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