Windows Azure Storage的Service第三方存取是採用REST API的方式,至於Windows Azure Storage Service跟REST爾後會再有文章特別介紹。
欲透過REST API方式抓取自己Azure Storage上有哪些table,有幾點必須要知道:
- HTTP Method:GET
- Request URI:http://myaccount.table.core.windows.net/Tables。myaccount部分為自己的storage name,或是從azure管理介面,不論是網頁版還是MMC,亦可直接取得Table URL。
- Header部分:Authorization, Date or x-ms-date, x-ms-version。
var account = “myaccount”;
HttpWebRequest httpWebRequest =
(HttpWebRequest)HttpWebRequest.Create(uri);
httpWebRequest.Method = “GET”;
httpWebRequest.ContentLength = 0;
httpWebRequest.Headers.Add(“x-ms-date”, DateTime.UtcNow.ToString(“R”, CultureInfo.InvariantCulture));
var sharedKey = Convert.FromBase64String(“your primary access key”);
var resource = httpWebRequest.RequestUri.PathAndQuery;
if (resource.Contains(“?”))
{
resource = resource.Substring(0, resource.IndexOf(“?”));
}
string stringToSign = string.Format(“{0}\n/{1}{2}”, httpWebRequest.Headers[“x-ms-date”], account, resource);
var hasher = new HMACSHA256(sharedKey);
string signedSignature = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
string authorizationHeader = string.Format(“{0} {1}:{2}”, “SharedKeyLite”, account, signedSignature);
httpWebRequest.Headers.Add(“Authorization”, authorizationHeader);
//不能加
//httpWebRequest.Headers.Add(“x-ms-version”, “2009-09-19”);
httpWebRequest.ContentType = “application/xml”;
var response = request.GetResponse();
比較詭異的是加了x-ms-version的header便會無法正常取得response。
參考連結:
http://blog.einbu.no/2009/08/authenticating-against-azure-table-storage/