I'm trying to upload document in Azure blob storage through REST API but getting authentication error.
Sharing request header and url-
message = bytes(sas_token, 'utf-8')
secret = bytes(key, 'utf-8')
signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest())
signature = str(signature)
header = {
"x-ms-version": "2020-04-08",
"x-ms-date":"Fri, 12 Mar 2021 23:39:12 GMT",
"Content-Type": "application/pdf; charset=UTF-8" ,
"Content-Length": "1048576",
"x-ms-copy-source" : ""
"x-ms-blob-type": "BlockBlob",
"Authorization": "SharedKey myaccount:" + signature,
}
URL - "https://"+ account_name+".blob.core.windows.net/"+container_name+"/"+name
response- requests.put(url, headers=headers)
Response - <Response [400]>
<?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidAuthenticationInfo</Code><Message>Authentication information is not given in the correct format. Check the value of Authorization header.
Also I'm not sure how should I pass document if my document location is OBJECT_LOCATION = "/home/meera/Downloads/download.pdf"
I want to upload this download.pdf file in blob storage.
Documentation following - https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob
Looks like you're using Shared Access Signature (SAS)
instead of storage account key for authorization. If that's the case, then you don't really need to compute the signature as the signature is already calculated in SAS.
Your code would be something like:
header = {
"Content-Type": "application/pdf; charset=UTF-8" ,
"Content-Length": "1048576",
"x-ms-blob-type": "BlockBlob"
}
URL - "https://"+ account_name+".blob.core.windows.net/"+container_name+"/"+name+"?"+sas_token
response- requests.put(url, headers=headers)
UPDATE
Please try this code.
account_name = "account-name"
container_name = "container-name"
name = "file-name.pdf"
sas_token = "?shared-access-signature-token"
filepath = "full path of the file-name.pdf"
with open(filepath, 'r') as f:
file_content = f.read() # Read whole file in the file_content string
headers = { "Content-Type": "application/pdf; charset=UTF-8", "x-ms-blob-type": "BlockBlob" }
url = "https://"+ account_name+".blob.core.windows.net/"+container_name+"/"+name+sas_token
response = requests.put(url, headers=headers, data=file_content)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.