Put Block list Forbidden Azure

时间:2018-05-15 11:15:43

标签: c# rest azure httpclient azure-storage-blobs

我在Azure中很安静,我正在尝试根据documentation实施Put Block List操作。 以下是我上传文件的步骤

  1. 从本地文件夹中读取文件
  2. 使用Put block操作上传它。它取得了成功。
  3. 然后我用 Get Block List查看我的块是否上传以及我能看到的回复
  4. <BlockList><CommittedBlocks /><UncommittedBlocks><Block><Name>MDAwMDAwMDAwMA==</Name><Size>17</Size></Block></UncommittedBlocks></BlockList>
    

    我有一个标识为uncommited的{​​{1}}块。

    1. 最后我使用MDAwMDAwMDAwMA==提交blob。 在这里,我收到错误代码403&#34; Forbidden&#34;。
    2. 我的签名如下

      Put Block List

      这是uri

      "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"
      

      请求内容

      https://storagekaren.blob.core.windows.net/dbstore/ddd.txt?comp=blocklist
      

      使用此方法计算authorizationHeader

      "<?xml version=\"1.0\" encoding=\"utf-8\"? 
      >\r\n<BlockList>\r\n<Uncommitted>MDAwMDAwMDAwMA==</Uncommitted> 
      </BlockList>\r\n"
      

      这是我的HttpClient对象发出请求

      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="
      

      以下是有错误的回复

      {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}
      

      请帮助我理解我做错了什么?

1 个答案:

答案 0 :(得分:1)

基本上问题来了,因为HttpClient正在为text/plain标头添加Content-Type,而你为此传递一个空字符串。如果您将签名字符串更改为包含内容类型标题,我相信您不应该收到此错误。基本上,您的签名字符串应如下所示:

"PUT\n
\n
\n
110\n
\n
text/plain\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"