PageBlob作为一个区块上载:x

编程入门 行业动态 更新时间:2024-10-23 11:23:25
本文介绍了PageBlob作为一个区块上载:x-ms-range无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是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"))

这篇关于PageBlob作为一个区块上载:x-ms-range无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-13 17:37:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/620823.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区块   作为一个   PageBlob

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!