Azure PageBlob上载新文件:x-ms-blob-content-length错误 [英] Azure PageBlob upload a new file: x-ms-blob-content-length error

查看:37
本文介绍了Azure PageBlob上载新文件:x-ms-blob-content-length错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的python代码来将文件上载到Azure PageBlob。我不确定应该为x-ms-blob-content-length指定什么,因为我一直收到错误。The documentation不是很清楚。

我的代码试图用0向左填充文件,以确保它在512字节边界内,但是我不知道我是否在正确的路径上。谢谢您。

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

sas_uri = '<SAS URI here>'
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()

    size = os.stat(file_path).st_size
    boundary = size % 512
    if boundary != 0:
        file = file.ljust(boundary, b'')
        size = size + boundary
    headers = {
        'Content-Type': 'application/octet-stream',
        'Content-Length': 0,
        'x-ms-blob-type': 'PageBlob',
        'x-ms-blob-content-length': size
    }

    conn.request('PUT', sas_uri, file, headers)

    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

错误:

Connected to pydev debugger (build 211.7142.13)
<?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:44c84519-501c-0004-5ecc-937c22000000
Time:2021-08-18T00:57:43.5973565Z</Message><HeaderName>x-ms-blob-content-length</HeaderName><HeaderValue>3991344</HeaderValue></Error>

推荐答案

本质上问题出在以下代码行:

size = size + boundary

如果将此数字除以512,您会注意到该数字不能被512完全整除。

为了将页面blob的内容长度设置为512的倍数,您需要使用以下逻辑:

size = size + 512 - boundary

请试一试。它应该可以工作。

这篇关于Azure PageBlob上载新文件:x-ms-blob-content-length错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆