【发布时间】:2018-10-25 04:56:04
【问题描述】:
我是 Azure 的新手,我正在尝试根据 documentation 实现 Put Block List 操作。
以下是我上传文件的步骤
- 从本地文件夹中读取文件
- 使用
Put block操作上传。它成功了。 - 然后我使用
Get Block List查看我的块是否已上传,在回复中我可以看到
<BlockList><CommittedBlocks /><UncommittedBlocks><Block><Name>MDAwMDAwMDAwMA==</Name><Size>17</Size></Block></UncommittedBlocks></BlockList>
我有一个uncommited 块,ID 为MDAwMDAwMDAwMA==。
- 最后我使用
Put Block List提交blob。 在这里我得到一个错误代码 403“禁止”。
我的签名如下
"PUT\n
\n
\n
110\n
\n
\n
\n
\n
\n
\n
\n
\n
x-ms-date:Tue, 15 May 2018 10:54:08 GMT\nx-ms-version:2017-07-29\n
/storagekaren/dbstore/ddd.txt\ncomp:blocklist"
这里是 uri
https://storagekaren.blob.core.windows.net/dbstore/ddd.txt?comp=blocklist
请求内容
"<?xml version=\"1.0\" encoding=\"utf-8\"?
>\r\n<BlockList>\r\n<Uncommitted>MDAwMDAwMDAwMA==</Uncommitted>
</BlockList>\r\n"
authorizationHeader 使用此方法计算
public static String CreateAuthorizationHeader(String canonicalizedString)
{
string signature;
using (var hmacSha256 = new
HMACSHA256(Convert.FromBase64String(STORAGE_ACCOUNT_KEY)))
{
var dataToHmac = Encoding.UTF8.GetBytes(canonicalizedString);
signature =
Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
}
var authorizationHeader = String.Format(
CultureInfo.InvariantCulture,
"{0} {1}:{2}",
"SharedKey",
ACCOUNT_NAME,
signature
);
return authorizationHeader;
}
"SharedKey storagekaren:eIHacFz/PWypTWg6SN/4BOuqlCLVLctABhi6Ay7TYiA="
这是我发出请求的 HttpClient 对象
{System.Net.Http.HttpClient}
BaseAddress: null
DefaultRequestHeaders: {x-ms-date: Tue, 15 May 2018 11:07:51 GMT
x-ms-version: 2017-07-29
Authorization: SharedKey storagekaren:eIHacFz/PWypTWg6SN/4BOuqlCLVLctABhi6Ay7TYiA=
}
MaxResponseContentBufferSize: 2147483647
Timeout: {01:00:00}
这是有错误的响应
HTTP/1.1 403 Server failed to authenticate the request. Make sure the value
of Authorization header is formed correctly including the signature.
Content-Length: 686
Content-Type: application/xml
Server: Microsoft-HTTPAPI/2.0
x-ms-request-id: 7a96deee-201e-00fc-78de-ec0ffc000000
x-ms-error-code: AuthenticationFailed
Date: Wed, 16 May 2018 06:22:35 GMT
<?xml version="1.0" encoding="utf-8"?><Error>
<Code>AuthenticationFailed</Code><Message>Server failed to authenticate the
request. Make sure the value of Authorization header is formed correctly
including the signature.
RequestId:7a96deee-201e-00fc-78de-ec0ffc000000
Time:2018-05-16T06:22:36.0842958Z</Message><AuthenticationErrorDetail>The
MAC signature found in the HTTP request
'E9M4w8nHaBbAsgW3Qhf+u5nHipvmxMvLp09AFdaxYZg=' is not the same as any
computed signature. Server used following string to sign: 'PUT
110
text/plain
x-ms-date:Wed, 16 May 2018 06:22:34 GMT
x-ms-version:2017-07-29
/storagekaren/dbstore/ddd.txt
comp:blocklist'.</AuthenticationErrorDetail></Error>
请帮助我理解我做错了什么?
【问题讨论】:
-
您是否在授权标头中使用了实际的存储帐户密钥?如果是这种情况,请立即更改。您需要计算授权标头。请分享代码。
-
没有。它不是实际的密钥,它是使用 HMACSHA256 Base64 编码计算的。我已经按照你说的分享了代码
-
我在您的
DefaultRequestHeaders中看不到Content-Length标头。不确定它是否会自动添加。您能否编辑您的问题并包含有关该错误的更多详细信息。我猜您的签名字符串与在服务器端创建的签名字符串略有不匹配。错误消息将包含服务器使用的字符串。 -
Content-Length是在content.Headers中添加的,不需要在DefaultRequestHeaders中添加,错误详情在问题中添加。但我也想注意,这个签名适用于Put Block操作,这让我更加困惑。谢谢。 -
感谢您更新您的问题并提供错误详情。我想我知道你为什么会收到这个错误。让我尽快给出答案。
标签: c# rest azure httpclient azure-blob-storage