使用python中的请求模块连接到AzureDatalakesgen2时出现问题

2024-10-03 11:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试连接到azuredatalakes-gen2,使用python从存储在里面的json文件中获取信息。听说用于python的azuredatalakes模块不适用于gen2(我自己也有问题),我接着通过restapi和python中的requests包进行连接。然而,阅读微软留下的参考资料和所需的身份验证标头让我更加困惑该怎么做。在

虽然我对python有一个大致的了解,但在更高级的项目上我还是个业余爱好者,而且总是需要查资料,但是这是我第一次问问题寻求帮助,而不是搜索,直到找到答案(所以请耐心等待)。在

我发现michalpawlikowski提供了一个有用的链接,解释了如何通过powershell进行连接,这有助于解释许多松散的结尾,但仍然存在两个问题,第一,我不确定如何正确编码身份验证头。特别是“通过在UTF-8编码的签名字符串上使用HMAC-SHA256算法对这个字符串进行编码”,第二个原因是这将只列出目录中找到的文件,而不是文件中包含的信息。在

这是我试过的


date = "Wed, 15 May 2019 14:28:01 GMT"

string_to_sign = 'GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:'+date+'\nx-ms-version:2018-11-09\n/'+STORAGE_ACCOUNT_NAME+'/'+FILE_SYSTEM_NAME+'\nrecursive:true\nresource:fileststem'

signature = #Encoded string_to_sign + key, am unsure how to approach

auth_header = "SharedKey "+STORAGE_ACCOUNT_NAME+":"+signature

headers = {"Authorization" : auth_header, "x-ms-version" : "2018-11-09", "x-ms-date" : date}


req = requests.get("https://"+STORAGE_ACCOUNT_NAME+".dfs.core.windows.net/" + FILE_SYSTEM_NAME + "?recursive=true&resource=filesystem", headers=headers)

我希望如此需求文本为了包含json文件中的信息,我总是会收到一个403错误,声明要确保我的头格式正确。在


Tags: 文件to字符串name身份验证json编码date
1条回答
网友
1楼 · 发布于 2024-10-03 11:26:35

如果要读取文件内容,应使用Read api。在

以下代码适用于我:

import requests
import datetime
import hmac
import hashlib
import base64

storage_account_name = 'storage_account_name'
storage_account_key = 'storage_account_key'
api_version = '2018-11-09'
request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')

#the file path on adls gen2
FILE_SYSTEM_NAME='dd1/11.txt'

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,
    'CanonicalizedResource': '/' + storage_account_name+'/'+FILE_SYSTEM_NAME
    }

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']+'\n'
                  + 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 + '.dfs.core.windows.net/'+FILE_SYSTEM_NAME)
r = requests.get(url, headers = headers)

#print out the file content
print(r.text)

试验结果:

enter image description here

相关问题 更多 >