首页 > 解决方案 > PageBlob 作为多个块上传:x-ms-range 无效

问题描述

这是我上一个问题的后续问题。我意识到我最多只能将4 兆字节上传到 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'\0' * (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">错误请求\r\n

错误请求 - 无效动词


HTTP 错误 400。请求动词无效。

标签: pythonazureblob

解决方案


问题在于以下代码行:

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)

你应该能够上传文件。本质上,我在您的代码中更改filechunk,因为您只想上传块而不是整个文件。


推荐阅读