简体   繁体   English

如何构造用于使用python将文件上传到Azure blob存储的授权标头?

[英]How do I construct the Authorization header for uploading a file to Azure blob storage using python?

I need to use REST to upload a file to Azure blob storage, but I also must use python. 我需要使用REST将文件上传到Azure blob存储,但是我也必须使用python。 I can't seem to work out the Authorization header though. 我似乎无法算出Authorization标头。

The code below will show what I am currently trying 下面的代码将显示我目前正在尝试的

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)

This generates a key but when I try to hit the API it says the Authorization header is formed incorectly 这会生成一个密钥,但是当我尝试访问API时,它说Authorization标头是错误地形成的

Please use the code below: 请使用以下代码:

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)

After run the code, check the new uploaded file in azure portal: 运行代码后,在azure门户中检查新上传的文件:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python 将文件上传到 azure blob 存储 - Uploading file to azure blob storage using python 使用python上载csv文件以使Blob存储蔚蓝 - Uploading csv file using python to azure blob storage 如何在python中针对Azure存储blob对用户进行身份验证? - How do I authenticate a user against an Azure storage blob in python? 如何使用Python将Azure Blob存储中的大型JSON文件拆分为每个记录的单个文件? - How can I split large JSON file in Azure Blob Storage into individual files for each record using Python? Azure存储Python SDK:将文件上传到Azure Blob存储而不将其写在磁盘上 - Azure Storage Python SDK : Uploading file to Azure blob storage without writting it on my disk 如何使用 Azure 函数从 Blob 存储中读取 json 文件 Python - How to read json file from blob storage using Azure Functions Blob Trigger with Python 如何直接从 Azure blob 存储读取文本文件而不将其下载到本地文件(使用 python)? - How can I read a text file from Azure blob storage directly without downloading it to a local file(using python)? 如何在 Python 中使用 Azure Functions 的 Azure Blob 存储绑定将 JSON 数据上传到 Azure 存储 blob - How to upload JSON data to Azure storage blob using Azure Blob storage bindings for Azure Functions in Python 如何使用 Python 和 Azure 函数在 Azure 存储容器中创建 blob - How to create a blob in an Azure Storage Container using Python & Azure Functions 使用python azure函数检查文件是否存在于blob存储中 - Check if file exists in blob storage using python azure functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM