如何使用python构造用于将文件上载到Azure blob存储的授权标头?
我需要使用REST将文件上载到Azure blob存储,但我还必须使用python。不过,我似乎无法计算出授权标题 下面的代码将显示我当前正在尝试的内容如何使用python构造用于将文件上载到Azure blob存储的授权标头?,python,azure,rest,blob,storage,Python,Azure,Rest,Blob,Storage,我需要使用REST将文件上载到Azure blob存储,但我还必须使用python。不过,我似乎无法计算出授权标题 下面的代码将显示我当前正在尝试的内容 import requests import datetime import hmac import hashlib import base64 url = 'https://mypoc.blob.core.windows.net/mycontainer/testpdf' blob_name = 'testpdf' blob_type = '
import requests
import datetime
import hmac
import hashlib
import base64
url = 'https://mypoc.blob.core.windows.net/mycontainer/testpdf'
blob_name = 'testpdf'
blob_type = 'BlockBlob'
storage_account_name = 'mypoc'
storage_account_key = 'thisisakey'
container_name='mycontainer'
api_version = '2018-03-28'
request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
string_params = {
'verb': 'PUT',
'Content-Encoding': '',
'Content-Language': '',
'Content-Length': '0',
'Content-MD5': '',
'Content-Type': '',
'Date': '',
'If-Modified-Since': '',
'If-Match': '',
'If-None-Match': '',
'If-Unmodified-Since': '',
'Range': '',
'CanonicalizedHeaders': 'x-ms-blob-type:' + blob_type + '\nx-ms-date:' + request_time + '\nx-ms-version:' + api_version,
'CanonicalizedResource': '/' + storage_account_name +'/'+container_name + '/' + blob_name
#'\ncomp:list\nrestype:container'
}
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()
Authorization = 'SharedKey ' + storage_account_name + ':' + signed_string
print(Authorization)
这会生成一个密钥,但当我尝试点击API时,它会说授权头是非直接形成的请使用下面的代码:
import requests
import datetime
import hmac
import hashlib
import base64
blob_name = 'mytestfile.txt'
blob_type = 'BlockBlob'
storage_account_name = 'yy3'
storage_account_key = 'xxx'
container_name='aa1'
api_version = '2018-03-28'
#the text upload to the file
data='this is a test text! have a nice day...'
content_len=str(len(data))
request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
string_params = {
'verb': 'PUT',
'Content-Encoding': '',
'Content-Language': '',
'Content-Length': content_len,
'Content-MD5': '',
'Content-Type': '',
'Date': '',
'If-Modified-Since': '',
'If-Match': '',
'If-None-Match': '',
'If-Unmodified-Since': '',
'Range': '',
'CanonicalizedHeaders': 'x-ms-blob-type:' + blob_type +'\nx-ms-date:' + request_time + '\nx-ms-version:' + api_version+'\n',
'CanonicalizedResource': '/' + storage_account_name +'/'+container_name + '/' + blob_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']
+ 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,
'Content-Length': content_len,
'x-ms-blob-type': blob_type,
'Authorization' : ('SharedKey ' + storage_account_name + ':' + signed_string)
}
url = 'https://' + storage_account_name + '.blob.core.windows.net/' + container_name + '/' + blob_name
print(url)
print("....start....")
r = requests.put(url,headers=headers,data=data)
print('....end....')
print(r.status_code)
运行代码后,检查azure portal中新上载的文件:
您好,您能告诉我们为什么在python中使用rest api吗?使用python存储sdk更容易:)。您能否共享完整的代码,以便我们可以看到您发送的实际请求头?我猜您的
内容长度
请求头有问题。另外,CanonicalizedHeaders
@IvanYang之后缺少了一个新行字符,我很想得到一些关于这方面的指导,而且我同意REST与SDK相比是一个非常头痛的问题。然而,由于某种原因,当我使用纯python方法时,我的公司抛出了SSL证书错误。我相信我们所说的是“中间人”证书。我需要用我的it功能来处理它,但我知道答案会很慢出现