我按照此 MSDN 引用 ( https://learn.microsoft.com/en-us/rest/api/storageservices/fileservices/put-block ) 来实现 Put Block 的休息调用。
我正在使用 Java 进行编码,并在签名之前形成了下面的授权字符串和 URL。
PUT
364070
x-ms-blob-type:BlockBlob
x-ms-date:Fri, 20 Jan 2017 12:57:06 GMT
x-ms-version:2016-05-31
/xyz/mycontainer/imageBlock1
comp:block
sun.net.www.protocol.https.DelegateHttpsURLConnection:https://xyz.blob.core.windows.net/mycontainer/imageBlock1?comp=block&blockid=YmxvY2stMQ==
Error I am getting:
403
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
我读了高拉夫咒语帖子http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/ 。但是,它对我不起作用。
Is there anything wrong with the string I am sending to sign or URL or below httpConn Request Header.
我设置的Http header 是:
httpConn.setRequestMethod("PUT");
httpConn.setRequestProperty("x-ms-blob-type", blobType);
httpConn.setRequestProperty("x-ms-date", date);
httpConn.setRequestProperty("x-ms-version", storageServiceVersion);
httpConn.setRequestProperty("Authorization", authorizationHeader);
httpConn.setRequestProperty("Content-Length",String.valueOf(blobLength) );
System.out.println(httpConn);
DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream());
wr.write(bytes);
wr.flush();
wr.close();
int response = httpConn.getResponseCode();
最佳答案
据我所知,Put Block是针对 block Blob 的操作。因此,我们不需要指定 x-ms-blob-type header 。如果您在 HTTP header 中指定此项,则需要遵循以下 tutorial关于构建规范化 header 字符串:
检索以 x-ms- 开头的资源的所有 header ,包括 x-ms-date header 。
将每个 HTTP header 名称转换为小写。
按 header 名称的字典顺序对 header 进行升序排序。每个 header 在字符串中只能出现一次。
最后,将换行符附加到结果列表中的每个规范化 header 。通过将此列表中的所有 header 连接成一个字符串来构造 CanonicalizedHeaders 字符串。
因此,根据您的代码,您的规范化 header
字符串如下所示:
x-ms-blob-type:BlockBlob\nx-ms-date:2017 年 1 月 20 日星期五 12:57:06 GMT\nx-ms-version:2016-05-31\n
此外,您构建的 CanonicalizedResource
不正确。根据您的代码,它应该如下所示:
/{帐户名称}/{容器名称}/{blob-名称}\nblockid:{block-id}\ncomp:block
注意:有关构建规范化资源
字符串的更多详细信息,您可以引用此官方document .
StringToSign
看起来像这样:
Fiddler捕获的流量如下:
关于rest - 实现 Azure Rest api 的 Put block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41764514/