I am trying to write a simple python code to upload a file to Azure PageBlob. I am not sure what should I specify for the x-ms-blob-content-length
as I keep getting error. The documentation is not very clear.
My code tries to left pad the file with 0s to make sure its within 512 byte boundary but I don't know if I am on the right path. Thank you.
import sys
import os
import http.client
from urllib.parse import urlparse
sas_uri = '<SAS URI here>'
uri = urlparse(sas_uri)
conn = http.client.HTTPSConnection(uri.hostname, port=uri.port, timeout=3000)
file_path = r"C:\Users\user\Downloads\npp.Installer.exe"
with open(file_path, 'rb') as reader:
file = reader.read()
size = os.stat(file_path).st_size
boundary = size % 512
if boundary != 0:
file = file.ljust(boundary, b'\0')
size = size + boundary
headers = {
'Content-Type': 'application/octet-stream',
'Content-Length': 0,
'x-ms-blob-type': 'PageBlob',
'x-ms-blob-content-length': size
}
conn.request('PUT', sas_uri, file, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Error:
Connected to pydev debugger (build 211.7142.13) <?xml version="1.0" encoding="utf-8"?> <Error><Code>InvalidHeaderValue</Code><Message>The value for one of the HTTP headers is not in the correct format. RequestId:44c84519-501c-0004-5ecc-937c22000000 Time:2021-08-18T00:57:43.5973565Z</Message><HeaderName>x-ms-blob-content-length</HeaderName><HeaderValue>3991344</HeaderValue></Error>