简体   繁体   English

如果使用REST设置contentType,则Azure存储无法上传图像

[英]Azure Storage failed to upload image if set contentType using REST

I am using VS2013,C# to develop azure application. 我正在使用VS2013,C#开发Azure应用程序。 I upload an image to Azure blob using REST API, it works fine if the ContentType is not set(default is application/octet-stream). 我使用REST API将图像上传到Azure blob,如果未设置ContentType(默认为application / octet-stream),则效果很好。 But it throw an exception when set the contentType to image/jpeg. 但是将contentType设置为image / jpeg时会引发异常。

exception: The remote server returned an error: (403) Forbidden. 异常:远程服务器返回错误:(403)禁止。

private void UploadBlobImage()
{
  string uri = @"http://myaccount.blob.core.windows.net/samplecontainer/1.jpg";
      HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
      request.Method = "PUT";
      string date = DateTime.UtcNow.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
      request.Headers.Add("x-ms-blob-type", "BlockBlob");
      request.Headers.Add("x-ms-date", date);
      request.Headers.Add("x-ms-version", "2009-09-19"); 
      string file = @"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg";
      System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open);
      request.ContentLength = fs.Length;
      request.ContentType = "image/jpg"; //this line will cause excption
      //request.Headers.Add("x-ms-blob-content-type", "image/jpg");

      string canonicalizedHeader = "x-ms-blob-type:BlockBlob\nx-ms-date:" + date + "\nx-ms-version:2009-09-19\nx-ms-blob-content-type:image/jpg\n";
      string canonicalizedResource = "/myaccount/samplecontainer/1.jpg";
      string sign = String.Format("{0}\n\n\n{1}\n{5}\n\n\n\n{2}\n\n\n\n{3}{4}",
                  "PUT", request.ContentLength.ToString(),
                  "",//if match
                  canonicalizedHeader,
                  canonicalizedResource,
                  "" //md5
                  );

      byte[] SignatureBytes = System.Text.Encoding.UTF8.GetBytes(sign);
      System.Security.Cryptography.HMACSHA256 SHA256 = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(key));
      String authorizationHeader = "SharedKey " + acc + ":" + Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));
      request.Headers.Add("Authorization", authorizationHeader);
      System.IO.BinaryReader reader = new System.IO.BinaryReader(fs);

      byte[] bs = reader.ReadBytes((int)fs.Length);
      reader.Close();
      fs.Close();

      System.IO.Stream stream = request.GetRequestStream();
      stream.Write(bs, 0, bs.Length);
      HttpWebResponse response = request.GetResponse() as HttpWebResponse;
      response.Close();
   }

The error says 403 forbidden so it is most likely an authorization issue, my guess would be because you dont include the non-default contenttype in the signingstring as specified in documentation : 错误显示403禁止,因此很可能是授权问题,我的猜测是因为您没有在文档中指定的签名字符串中包含非默认内容类型:

           StringToSign = VERB + "\n" +
           Content-Encoding + "\n" +
           Content-Language + "\n" +
           Content-Length + "\n" +
           Content-MD5 + "\n" +
           Content-Type + "\n" +
           Date + "\n" +
           If-Modified-Since + "\n" +
           If-Match + "\n" +
           If-None-Match + "\n" +
           If-Unmodified-Since + "\n" +
           Range + "\n" +
           CanonicalizedHeaders + 
           CanonicalizedResource;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过REST API上传Azure Blob时无法设置contentType - Unable to set contentType when uploading Azure Blob via REST API 图片上传在Azure存储上不起作用 - Image upload not working on Azure Storage 如何使用 .NET v12 SDK 在 Azure Blob 存储中使用指定的 ContentType 上传 Blob? - How upload blob in Azure Blob Storage with specified ContentType with .NET v12 SDK? 使用指定的 ContentType 将 blob 上传到 Azure Blob 存储并同时覆盖(.NET v12 SDK)? - Upload blob into Azure Blob Storage with specified ContentType and overwrite same time (.NET v12 SDK)? 从URL将图像上传到Azure Blob存储 - Upload image to Azure Blob Storage from URL 使用rest api从Web应用程序上传文件到Azure文件存储 - upload files to Azure file storage from web app using rest api 如何在开发存储中使用REST上传blob? - how to upload blob using REST in development storage? 如何将发布在REST API中的文件上传到Azure存储 - How to upload a file posted in REST API to Azure Storage 如何为未知图像扩展设置ContentType? - How to set ContentType for unknown image extensions? 有没有办法在 Azure.Storage.Blobs 中获取 blob 的 ContentType? - Is there a way to get a blob's ContentType in Azure.Storage.Blobs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM