Skip to content Skip to sidebar Skip to footer

Pageblob Upload As One Chunk: X-ms-range Invalid

This is a follow up question to my previous question, I am now trying to use PutPage API to upload the file as one chunk. The first part that reserves a space works. I get an error

Solution 1:

The reason your code is failing is because the data you're uploading is not aligned with the 512 bytes boundary. From this link:

Each range of pages submitted with Put Page for an update operation may be up to 4 MiB in size. The start and end range of the page must be aligned with 512-byte boundaries. If you attempt to upload a range of pages that is larger than 4MB, the service returns status code 413 (Request Entity Too Large).

Please try the following code:

import sys
import os
import http.client
from urllib.parse import urlparse

sas_uri = '<SAS URI here>'
uri = urlparse(sas_uri)
conn = http.client.HTTPConnection(uri.hostname, port=uri.port, timeout=3000)

file_path = r"C:\Users\user\Downloads\npp.Installer.exe"withopen(file_path, 'rb') as reader:
    file = reader.read()
    file_size = os.stat(file_path).st_size
    block_size = file_size
    boundary = block_size % 512if boundary != 0:
        padarray = b'\0' * (512 - boundary)
        file = file + padarray
        block_size = block_size + 512 - boundary
    # Reserve a block space
    headers = {
        'Content-Type': 'application/octet-stream',
        'Content-Length': 0,
        'x-ms-blob-type': 'PageBlob',
        'x-ms-blob-content-length': block_size
    }
    conn.request('PUT', sas_uri, '', headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

    # Upload the file
    headers = {
        'Content-Length': block_size,
        'x-ms-page-write': 'update',
        'Range': f'bytes=0-{block_size-1}'
    }
    conn.request('PUT', sas_uri + '&comp=page', file, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

Post a Comment for "Pageblob Upload As One Chunk: X-ms-range Invalid"