我正在使用VS2013,C#来开发azure应用程序。我使用REST API将图像上传到Azure blob,如果未设置ContentType,则工作正常(默认为application / octet-stream)。但是当将contentType设置为image / jpeg时会抛出异常。
exception:远程服务器返回错误:(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();
}
答案 0 :(得分:1)
错误说403禁止,因此很可能是授权问题,我的猜测是因为你没有在签名字符串中包含非默认内容类型为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;