Eric
Eric

Reputation: 1

Azure Storage failed to upload image if set contentType using REST

I am using VS2013,C# to develop azure application. I upload an image to Azure blob using REST API, it works fine if the ContentType is not set(default is application/octet-stream). But it throw an exception when set the contentType to image/jpeg.

exception: The remote server returned an error: (403) Forbidden.

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();
   }

Upvotes: 0

Views: 371

Answers (1)

jakobandersen
jakobandersen

Reputation: 1409

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:

           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;

Upvotes: 1

Related Questions