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

编程入门 行业动态 更新时间:2024-10-28 20:26:24
本文介绍了无法通过 python 连接到 azure 文件服务 REST api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过 python 请求访问我的文件服务,由 列出共享页面,我是 REST 和 python 请求的新手.我的输入是

I want to access my file service by python Request, guided by the list shares page, I'm new to REST and python Request. my input is

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

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

But get error, the output of r.content:

b'xefxbbxbf<?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. RequestId:ef98f282-f01a-0042-3e24-d62397000000 Time: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+xb0PpmXrjClkTD3RfcQDcYtcYbXFcFcQdFcYcTvCypFcXuTcFcYdcYtcYfcXuTycFc=', 'x-ms-version': '2017-07-29'},因为我不知道Authorization和account是否在同一行.我还尝试了许多版本的 x-ms-version.但是所有 400 或 403 都响应.

I have also try the header {'x-ms-date': '2018-04-17 06:22:15.181620', 'Authorization': 'SharedKey zedongnewstorageaccount:NJYYtabOIj5D1R+xb0PpmXrjCldf6NA6oLkYReAKFiTzubX9tvC3G3PcDYzR2cX/TMjkOu4JhsvQffS+xTDDBQ==', 'x-ms-version': '2017-07-29'}, because i don't know if the Authorization and account in the same line. I also tried many version of x-ms-version. But there all the 400 or 403 respond.

我已阅读Azure 存储服务的身份验证但很困惑.

错误在哪里?有没有案例我可以学习通过Azure REST API构建我的应用程序?(我使用关键字google,所有页面都是关于构建REST API和azure官方文档)

Where is the error? And is there case I can learn to build my app by Azure REST API?(I use the keyword to google, all the pages are about build REST API and azure official docs)

推荐答案

我也推荐使用 Python SDK 但出于教育目的:

I also recommend to use Python SDK but for educational purposes:

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 + ' x-ms-version:' + api_version + ' ', 'CanonicalizedResource': '/' + storage_account_name + '/ comp:list' } string_to_sign = (string_params['verb'] + ' ' + string_params['Content-Encoding'] + ' ' + string_params['Content-Language'] + ' ' + string_params['Content-Length'] + ' ' + string_params['Content-MD5'] + ' ' + string_params['Content-Type'] + ' ' + string_params['Date'] + ' ' + string_params['If-Modified-Since'] + ' ' + string_params['If-Match'] + ' ' + string_params['If-None-Match'] + ' ' + string_params['If-Unmodified-Since'] + ' ' + string_params['Range'] + ' ' + 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 = ('' + storage_account_name + '.file.core.windows/?comp=list') r = requests.get(url, headers = headers) print(r.content)

输出:

<?xml version="1.0" encoding="utf-8"?><EnumerationResults ServiceEndpoint="teststorageaccount.file.core.windows/"><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>

更多推荐

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

本文发布于:2023-10-07 10:20:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   文件   python   azure   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!