PageBlob作为一个区块上载:x-ms-range无效

2022-03-03 00:00:00 python azure blob

问题描述

这是my previous question的后续问题,我现在正在尝试使用PutPage API将文件作为一个块上传。预留空间的第一部分工作。我在第二个API调用中收到错误,指出x-ms-range值有问题

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

sas_uri = '<SAS URI>'
uri = urlparse(sas_uri)

conn = http.client.HTTPSConnection(uri.hostname, port=uri.port, timeout=3000)

file_path = r"C:UsersuserDownloads
pp.Installer.exe"

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

    file_size = os.stat(file_path).st_size
    block_size = file_size
    boundary = block_size % 512
    if boundary != 0:
        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-Type': 'application/octet-stream',
        'Content-Length': file_size,
        'x-ms-blob-type': 'PageBlob',
        'x-ms-page-write': 'update',
        'x-ms-range': f'bytes=0-{file_size-1}'
    }
    conn.request('PUT', sas_uri + '&comp=page', file, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

错误:

<?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:c3c776d3-e01c-00b7-80de-9386a5000000
Time:2021-08-18T03:11:14.5181971Z</Message><HeaderName>x-ms-range</HeaderName><HeaderValue>bytes=0-3991191</HeaderValue></Error>

解决方案

您的代码失败的原因是您要上载的数据没有与512字节边界对齐。从此link

使用PUT Page提交的用于更新操作的每个页面范围 大小可以高达4 MiB。页面的开始和结束范围必须 与512字节边界对齐。如果您尝试上载范围 对于大于4MB的页面,服务返回状态代码413 (请求实体太大)。

请尝试以下代码:

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:UsersuserDownloads
pp.Installer.exe"
with open(file_path, 'rb') as reader:
    file = reader.read()
    file_size = os.stat(file_path).st_size
    block_size = file_size
    boundary = block_size % 512
    if boundary != 0:
        padarray = b'' * (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"))

相关文章