我已经成功地在我的容器中创建了Blob,但是当我尝试设置x-ms-blob-content-type标头时,没有创建Blob并且我收到403错误。这是我的代码:
var authorizationHeader = CreateAuthorizationHeader(stringToSign);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(blobEndpoint + urlPath);
request.Method = requestMethod;
request.Headers["x-ms-blob-type"] = blobType;
request.Headers["x-ms-date"] = dateInRfc1123Format;
request.Headers["x-ms-version"] = storageServiceVersion;
request.Headers["Authorization"] = authorizationHeader;
request.ContentLength = blobLength;
try
{
using (Stream requestStream = await request.GetRequestStreamAsync())
{
requestStream.Write(blobContent, 0, blobLength);
}
using(HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
{
var eTag = response.Headers["ETag"];
}
}
但每当我尝试添加其中任何一行时,我都会得到403:
request.Headers["x-ms-blob-content-type"] = "image/jpeg";
//or
request.ContentType = "image/jpeg";
有没有人遇到过这个?感谢任何反馈,谢谢!
更新:在下面添加stringToSign代码。
var canonicalizedHeaders = String.Format(
"x-ms-blob-type:{0}\nx-ms-date:{1}\nx-ms-version:{2}",
blobType,
dateInRfc1123Format,
storageServiceVersion);
var canonicalizedResource = String.Format("/{0}/{1}", account, urlPath);
var stringToSign = String.Format(
"{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n{2}\n{3}",
requestMethod,
blobLength,
canonicalizedHeaders,
canonicalizedResource);
答案 0 :(得分:3)
您是否在x-ms-blob-content-type
和canonicalizedHeaders
中添加了request headers
?将其添加到canonicalizedHeaders
时,请确保这是第一个条目,因为标题需要排序。我拿了你的代码并添加了标题,它工作得非常好。这是我的代码:
private static async Task UploadBlob()
{
var blobType = "BlockBlob";
var dateInRfc1123Format = DateTime.UtcNow.ToString("R");
var storageServiceVersion = "2014-02-14";
var blobContentType = "image/png";
var canonicalizedHeaders = String.Format("x-ms-blob-content-type:{0}\nx-ms-blob-type:{1}\nx-ms-date:{2}\nx-ms-version:{3}", blobContentType, blobType, dateInRfc1123Format, storageServiceVersion);
var urlPath = "test-test/AlarmClock1.png";
var canonicalizedResource = String.Format("/{0}/{1}", accountName, urlPath);
var requestMethod = "PUT";
var fileContents = File.ReadAllBytes(@"D:\images\images\AlarmClock1.png");
var blobLength = fileContents.Length;
var stringToSign = String.Format("{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n{2}\n{3}", requestMethod, blobLength, canonicalizedHeaders, canonicalizedResource);
var authorizationHeader = SignThis(stringToSign);
var blobEndpoint = "https://myaccountname.blob.core.windows.net/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(blobEndpoint + urlPath);
request.Method = requestMethod;
request.Headers["x-ms-blob-content-type"] = blobContentType;
request.Headers["x-ms-blob-type"] = blobType;
request.Headers["x-ms-date"] = dateInRfc1123Format;
request.Headers["x-ms-version"] = storageServiceVersion;
request.Headers["Authorization"] = authorizationHeader;
request.ContentLength = blobLength;
try
{
using (Stream requestStream = await request.GetRequestStreamAsync())
{
requestStream.Write(fileContents, 0, blobLength);
}
using(HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
{
var eTag = response.Headers["ETag"];
}
}
catch (Exception excep)
{
}
}