PageBlob 作为一个块上传:x-ms-range 无效
PageBlob upload as one chunk: x-ms-range invalid
这是 , I am now trying to use PutPage API 的后续问题,将文件作为一个块上传。保留 space 的第一部分有效。我在第二个 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:\Users\user\Downloads\npp.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
:
Each range of pages submitted with Put Page for an update operation
may be up to 4 MiB in size. The start and end range of the page must
be aligned with 512-byte boundaries. If you attempt to upload a range
of pages that is larger than 4MB, the service returns status code 413
(Request Entity Too Large).
请尝试以下代码:
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:\Users\user\Downloads\npp.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'[=10=]' * (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"))
这是 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:\Users\user\Downloads\npp.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
:
Each range of pages submitted with Put Page for an update operation may be up to 4 MiB in size. The start and end range of the page must be aligned with 512-byte boundaries. If you attempt to upload a range of pages that is larger than 4MB, the service returns status code 413 (Request Entity Too Large).
请尝试以下代码:
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:\Users\user\Downloads\npp.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'[=10=]' * (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"))