c# - Azure - call Storage rest api for list blobs -
what trying connect azure storage rest api list blobs. ref: http://msdn.microsoft.com/en-us/library/windowsazure/dd135734.aspx
i have tried follow http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx in order specify authorization header 403 error - forbidden.
code:
uri address = new uri("https://account.blob.core.windows.net/$logs?restype=container&comp=list"); httpwebrequest req = (httpwebrequest)httpwebrequest.create(address); req.headers["x-ms-date"] = "2013-09-04"; req.headers["x-ms-version"] = "2012-02-12"; req.method = "get"; 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: 2013-09-04\nx-ms-version:2012-02-12\n" // headers + "/account/blob\ncomp:list\nrestype:container"; // resources string accountname = "account"; string key = convert.tobase64string(encoding.default.getbytes(stringtosign)); req.headers["authorization"] = string.format("sharedkey {0}:{1}", accountname, key); httpwebresponse resp = req.getresponse() httpwebresponse;
can see mistakes? there tool can generate key? 1 thing not sure of encoding/hashing string correctly.
thanks, andrew
update latest code. code gives me forbidden error.
datetime dt = datetime.utcnow; 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: " + dt.tostring("r") + "\nx-ms-version:2012-02-12\n" // headers + "/account/$logs\ncomp:list\nrestype:container"; string auth = signthis(stringtosign, "accountkey", "account"); string method = "get"; string urlpath = "https://account.blob.core.windows.net/$logs?restype=container&comp=list"; uri uri = new uri(urlpath); httpwebrequest request = (httpwebrequest)webrequest.create(uri); request.method = method; request.headers.add("x-ms-date", dt.tostring("r")); request.headers.add("x-ms-version", "2012-02-12"); request.headers.add("authorization", auth); using (httpwebresponse response = (httpwebresponse)request.getresponse()) { }
there issues code above. before that, first thing need key storage account. can windows azure portal. click on storage account name in portal , click on "manage access keys" shown in screenshot below:
now issues:
the way you're creating authorization header incorrect. creating authorization header, need account name, account key , stringtosign
code above. try code:
private static string signthis(string stringtosign, string key, string account) { string signature = string.empty; byte[] unicodekey = convert.frombase64string(key); using (hmacsha256 hmacsha256 = new hmacsha256(unicodekey)) { byte[] datatohmac = system.text.encoding.utf8.getbytes(canonicalizedstring); signature = convert.tobase64string(hmacsha256.computehash(datatohmac)); } string authorizationheader = string.format( cultureinfo.invariantculture, "{0} {1}:{2}", "sharedkey", account, signature); return authorizationheader; }
the function above provide authorization header need pass authorization.
2nd thing noticed in code stringtosign
, you're not passing container name. stringtosign
should be:
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: 2013-09-04\nx-ms-version:2012-02-12\n" // headers + "/account/$logs\ncomp:list\nrestype:container"; // resources
you mentioned you're quite new windows azure. if may suggest - implementing rest api has been done number of folks earlier also. please take @ have done instead of trying same thing again. may find these links useful:
http://convective.wordpress.com/2010/08/18/examples-of-the-windows-azure-storage-services-rest-api/
http://azurestoragesamples.codeplex.com/ - @ rest api implementation in project.
update
here's working code (just change account name, key , container name)
static void listcontainers() { string account = "account"; string key = "key"; string container = "$logs"; datetime dt = datetime.utcnow; string stringtosign = string.format("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:" + dt.tostring("r") + "\nx-ms-version:2012-02-12\n" // headers + "/{0}/{1}\ncomp:list\nrestype:container", account, container); string auth = signthis(stringtosign, key, account); string method = "get"; string urlpath = string.format("https://{0}.blob.core.windows.net/{1}?restype=container&comp=list", account, container); uri uri = new uri(urlpath); httpwebrequest request = (httpwebrequest)webrequest.create(uri); request.method = method; request.headers.add("x-ms-date", dt.tostring("r")); request.headers.add("x-ms-version", "2012-02-12"); request.headers.add("authorization", auth); using (httpwebresponse response = (httpwebresponse)request.getresponse()) { } }
hope helps.
Comments
Post a Comment