问题描述
- 用存储的REST调用也是401错误~~
-
我按照教程使用REST调用BLOB的内容,也遇到的是401错误,这是什么情况?还有人也遇到了吗?
解决方案
您好,
请问您使用的是REST中的哪一个方法?据我所知,如果使用REST去调用blob中内容是,复杂的地方主要是在构造签名,首先我建议您先阅读下这篇文档:
https://msdn.microsoft.com/zh-cn/library/azure/dd179428.aspx
例如该方法:
public void PutBlob(String containerName, String blobName)
{
String requestMethod = "PUT";
String urlPath = String.Format("{0}/{1}", containerName, blobName);
String storageServiceVersion = "2012-02-12";
String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
String content = "Andrew Carnegie was born in Dunfermline";
UTF8Encoding utf8Encoding = new UTF8Encoding();
Byte[] blobContent = utf8Encoding.GetBytes(content);
Int32 blobLength = blobContent.Length;
const String blobType = "BlockBlob";
String canonicalizedHeaders = String.Format(
"x-ms-blob-type:{0}nx-ms-date:{1}nx-ms-version:{2}",
blobType,
dateInRfc1123Format,
storageServiceVersion);
String canonicalizedResource = String.Format("/{0}/{1}", AzureStorageConstants.Account, urlPath);
String stringToSign = String.Format(
"{0}nnn{1}nnnnnnnnn{2}n{3}",
requestMethod,
blobLength,
canonicalizedHeaders,
canonicalizedResource);
String authorizationHeader = Utility.CreateAuthorizationHeader(stringToSign);
Uri uri = new Uri(AzureStorageConstants.BlobEndPoint + urlPath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = requestMethod;
request.Headers.Add("x-ms-blob-typeobType);
request.Headers.Add("x-ms-dateteInRfc1123Format);
request.Headers.Add("x-ms-versionorageServiceVersion);
request.Headers.Add("AuthorizationthorizationHeader);
request.ContentLength = blobLength;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(blobContent, 0, blobLength);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
String ETag = response.Headers["ETag"];
}
}
我们需要构造出shared key,同时注意header中的各个信息。
处理rest问题和调试rest错误时,我个人建议您可以使用fiddler去troubleshoot您的reqest请求,去解决问题。
同时,建议您参考这个博客:https://convective.wordpress.com/2010/08/18/examples-of-the-windows-azure-storage-services-rest-api/
Regards,
Will
如果您想进一步了解Windows Azure, Windows Azure 官网欢迎您的访问
时间: 2024-09-16 12:28:46