PageBlob 上传为多个块:x-ms-range 无效

PageBlob upload as multiple chunks: x-ms-range invalid

这是 . What I realized is I can only upload 4 mega bytes max 到 Azure 的后续问题。所以我试图修改代码以分块上传到 pageblob。但是现在我在尝试上传第一个块时遇到错误。

sas_uri = '<SAS URI>'
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"

def chunk(msg, n):
    for i in range(0, len(msg), n):
        yield msg[i:i + n]


with open(file_path, 'rb') as reader:
    file = reader.read()

    file_size = len(file)
    block_size = file_size
    boundary = block_size % 512
    if boundary != 0:
        padding = b'[=10=]' * (512 - boundary)
        file = file + padding
        block_size = block_size + 512 - boundary    # needed to make the file on boundary

    headers = {
        'Content-Type': 'application/octet-stream',
        'Content-Length': 0,
        'x-ms-blob-type': 'PageBlob',
        'x-ms-blob-content-length': block_size
    }

    # Reserve a block space
    conn.request('PUT', sas_uri, '', headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

    CHUNK_MAX_SIZE = int(4e+6) # 4 mega bytes
    index = 0
    for chunk in chunk(file, CHUNK_MAX_SIZE):
        chunk_size = len(chunk)
        headers = {
            'Content-Type': 'application/octet-stream',
            'Content-Length': chunk_size,
            'x-ms-blob-type': 'PageBlob',
            'x-ms-page-write': 'update',
            'x-ms-range': f"bytes={index}-{index + chunk_size - 1}"
        }
        # Upload the file
        conn.request('PUT', sas_uri + '&comp=page', file, headers)
        res = conn.getresponse()
        data = res.read()
        print(data.decode("utf-8"))

        index = index + chunk_size

错误:

<?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:c312a91d-401c-0000-44e9-95bd08000000
Time:2021-08-21T17:33:47.2909476Z</Message><HeaderName>x-ms-range</HeaderName><HeaderValue>bytes=0-3999999</HeaderValue></Error>

更新:我更正了 CHUNK_MAX_SIZE 所以现在第一个块上传时没有错误,但后续块导致此错误:

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">Bad Request\r\n

Bad Request - Invalid Verb


HTTP Error 400. The request verb is invalid.

问题在于以下代码行:

CHUNK_MAX_SIZE = int(4e+6) # 4 mega bytes

它给你 4000000 这不是 4MB。 4MB 是 4194304 字节。

请尝试将您的代码更改为:

CHUNK_MAX_SIZE = 4 * 1024 * 1024 # or 4194304

您的代码应该可以正常工作。

更新

请更改以下代码行:

conn.request('PUT', sas_uri + '&comp=page', file, headers)

conn.request('PUT', sas_uri + '&comp=page', chunk, headers)

您应该可以上传文件了。本质上,我在您的代码中将 file 更改为 chunk,因为您只想上传块而不是整个文件。