我按照教程使用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}\n\n\n{1}\n\n\n\n\n\n\n\n\n{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 官網歡迎您的訪問