PageBlob upload as one chunk: x-ms-range invalid(PageBlob作为一个区块上载:x-ms-range无效)
本文介绍了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无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:PageBlob作为一个区块上载:x-ms-range无效


基础教程推荐
猜你喜欢
- 将 YAML 文件转换为 python dict 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01