You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PageBlobClient.UploadPages抛出InvalidHeaderValue错误求助

Azure Blob Storage UploadPages InvalidHeaderValue错误排查与解决

问题重现

执行以下C#代码调用PageBlobClient.UploadPages时抛出InvalidHeaderValue错误:

var blobServiceClient = new BlobServiceClient(connectionString, options);
var blobContainerClient = blobServiceClient.GetBlobContainerClient("somecontainer");
var pageBlobClient = blobContainerClient.GetPageBlobClient("someblob");
await blobContainerClient.CreateIfNotExistsAsync(cancellationToken: default);
await pageBlobClient.CreateIfNotExistsAsync(0, cancellationToken: default);
pageBlobClient.UploadPages(new MemoryStream(Encoding.ASCII.GetBytes("Test")), 0);

错误详情

其中一个HTTP标头的值格式不正确。
RequestId:0761d774-b01e-0002-6a3a-a256ea000000
时间:2022-07-28T04:25:28.8145993Z
状态: 400(其中一个HTTP标头的值格式不正确。)
错误代码: InvalidHeaderValue

附加信息:
标头名称: Range
标头值: bytes=0-3

内容:

return (<Code>InvalidHeaderValue</Code>)
其中一个HTTP标头的值格式不正确。
RequestId:0761d774-b01e-0002-6a3a-a256ea000000
时间:2022-07-28T04:25:28.8145993ZRangebytes=0-3

标头:
Server: Windows-Azure-Blob/1.0,Microsoft-HTTPAPI/2.0
x-ms-request-id: 0761d774-b01e-0002-6a3a-a256ea000000
x-ms-client-request-id: ec21d32d-74ec-4942-9321-341da37e7a63
x-ms-version: 2021-08-06
x-ms-error-code: InvalidHeaderValue
Date: Thu, 28 Jul 2022 04:25:27 GMT
Content-Length: 320
Content-Type: application/xml

错误原因

Azure Page Blob有强制约束:每个页面的大小必须是512字节的整数倍,上传内容的长度、偏移量都要对齐512字节边界。

你上传的内容仅4字节("Test"),对应的Range头为bytes=0-3,不符合512字节的要求,触发了Blob服务的格式校验,返回InvalidHeaderValue错误。同时,初始创建的Page Blob大小为0,也会导致页面范围超出Blob有效大小。

解决办法

1. 调整内容与Blob大小至512字节倍数

将上传内容填充至512字节的整数倍,同时确保创建的Page Blob大小至少覆盖上传的页面范围。

2. 修正后的代码示例

var blobServiceClient = new BlobServiceClient(connectionString, options);
var blobContainerClient = blobServiceClient.GetBlobContainerClient("somecontainer");
var pageBlobClient = blobContainerClient.GetPageBlobClient("someblob");
await blobContainerClient.CreateIfNotExistsAsync(cancellationToken: default);

// 创建大小为512字节的Page Blob,确保页面范围有效
await pageBlobClient.CreateIfNotExistsAsync(512, cancellationToken: default);

// 准备512字节的内容,不足部分用空字节填充
var content = Encoding.ASCII.GetBytes("Test");
var buffer = new byte[512];
Array.Copy(content, buffer, content.Length);

await pageBlobClient.UploadPagesAsync(new MemoryStream(buffer), 0);

3. 可选:启用自动扩展

若不想预先指定Blob大小,可通过相关重载方法允许上传时自动扩展Blob大小,确保上传的页面范围始终有效。

内容的提问来源于stack exchange,提问作者SYL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 09:45:35