使用 pageBlobClient 的简单 UploadPage azure blob

Simple UploadPage azure blob using pageBlobClient

我正在使用 Premium/Hot、LRS、StorageV2 Azure 存储并尝试编写一个简单的字符串,但我一直收到身份验证错误。

为了在门户中生成容器的 SAS URI,我去了: 存储资源 -> 容器 -> 我的容器 -> 共享访问令牌 -> 生成 SAS 令牌和 URL

// SAS URI of blob container
var sasUriStr = "https://storageaccountname.blob.core.windows.net/containername?sp=r&st=2021-08-10T00:34:00Z&se=2021-08-15T08:34:00Z&spr=https&sv=2020-08-04&sr=c&sig=ABCDEFGH/YJKLMNOP=";
var uri = new Uri(sasUriStr);
var pageBlobClient = new PageBlobClient(uri);
pageBlobClient.UploadPages(new MemoryStream(Encoding.UTF8.GetBytes("hello world")), 0);

Unhandled exception. Azure.RequestFailedException: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. Time:2021-08-12T20:22:44.0905117Z Status: 403 (Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.) ErrorCode: AuthenticationFailed

Additional Information: AuthenticationErrorDetail: Signature did not match. String to sign used was r

感谢任何帮助或提示。谢谢

更新:

将 /blob 名称添加到 SAS URI 后出现此错误:

Unhandled exception. Azure.RequestFailedException: The value for one of the HTTP headers is not in the correct format.
RequestId:7a741951-401c-00c9-3ce3-8f5076000000
Time:2021-08-13T01:38:13.4585107Z
Status: 400 (The value for one of the HTTP headers is not in the correct format.)
ErrorCode: InvalidHeaderValue

Additional Information:
HeaderName: x-ms-range
HeaderValue: bytes=0-10

Content:
<?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:7a741951-401c-00c9-3ce3-8f5076000000
Time:2021-08-13T01:38:13.4585107Z</Message><HeaderName>x-ms-range</HeaderName><HeaderValue>bytes=0-10</HeaderValue></Error>

Headers:
Server: Windows-Azure-Blob/1.0,Microsoft-HTTPAPI/2.0
x-ms-error-code: InvalidHeaderValue
x-ms-request-id: 7a741951-401c-00c9-3ce3-8f5076000000
x-ms-version: 2020-08-04
x-ms-client-request-id: 4366d771-7f70-4bbf-9677-6e9fcf3cb7a1
Date: Fri, 13 Aug 2021 01:38:12 GMT
Content-Length: 327
Content-Type: application/xml

解决方案

请将您的代码更改为:

var sasUriStr = "https://storageaccountname.blob.core.windows.net/containername?sp=r&st=2021-08-10T00:34:00Z&se=2021-08-15T08:34:00Z&spr=https&sv=2020-08-04&sr=c&sig=ABCDEFGH/YJKLMNOP=";
var uri = new Uri(sasUriStr);
BlobContainerClient containerClient = new BlobContainerClient(uri);
var pageBlobClient = containerClient.GetPageBlobClientCore("page-blob-name");
pageBlobClient.UploadPages(new MemoryStream(Encoding.UTF8.GetBytes("hello world")), 0);

在使用此代码之前,请确保该 blob 已经存在。

问题

您 运行 陷入问题的原因是因为您试图使用表示 blob 容器 SAS 的 URI 创建 PageBlobClient。因此,Azure 存储服务假定您的 blob 名称为 containername,容器为 $root。由于 SAS 令牌是为 containername blob 容器获取的,并且服务使用 $root blob 容器来验证 SAS 令牌,因此您收到授权失败错误。

通过创建 BlobContainerClient using the SAS URL and then creating a PageBlobClient using BlobContainerClient.GetPageBlobClientCore(String) 解决问题。