2012年11月7日

[Azure]用REST API列出Storage Account下的Container

照著MSDN試了半天,終於發現順序還是很重要的。

假設我的Storage Account是slakchiprod,則我的request URL要是http:/slakchiprod.blob.core.windows.net/?comp=list

然後以下面這段程式碼來取得slakchiprod下的所有container。

            var utcTime = DateTime.UtcNow.ToString("R");
var xmsversion = "2011-08-18";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(URL));
req.Method = "GET";
//下面這兩個Header的順序不能改...
req.Headers["x-ms-version"] = xmsversion;
req.Headers["x-ms-date"] = utcTime;
string stringToSign = "GET\n" +
"\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*/
"x-ms-date:{0}\n" +
"x-ms-version:{1}\n" +
"/{2}/\n" + //URL
"{3}"; //Param
stringToSign = string.Format(stringToSign,
utcTime,
xmsversion,
storageAcct,/*slakchiprod*/
"comp:list");

HMACSHA256 crypt = newHMACSHA256(Convert.FromBase64String(key));
var buffer = crypt.ComputeHash(.UTF8.GetBytes(stringToSign));
var result = Convert.ToBase64String(buffer);
req.Headers["Authorization"] = "SharedKey " + string.Format("{0}:{1}", storageAcct, result);

req.GetResponse();//取得回應

沒有留言:

Blog Archive

About Me