I want to get all blob containers from azure storage using http client request. I did one sample but i am facing forbidden 403 error.
I attached my code,
private const string ListofContainersURL = "https://{0}.blob.core.windows.net/?comp=list&maxresults=3"; //https://myaccount.blob.core.windows.net/?comp=list&maxresults=3
public string ListofContainersinBlob()
{
string Requesturl = string.Format(ListofContainersURL, storageAccount );
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Requesturl);
string now = DateTime.UtcNow.ToString("R");
request.Method = "GET";
request.Headers.Add("x-ms-version", "2015-12-11");
request.Headers.Add("x-ms-date", now);
request.Headers.Add("Authorization", AuthorizationHeader1("GET", now, request, storageAccount, storageKey));
var response = request.GetResponseAsync();
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
return resp.StatusCode.ToString();
}
}
private string AuthorizationHeader1(string method, string now, HttpWebRequest request, string storageAccount, string storageKey)
{
string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:{now}\nx-ms-version:2015-12-11";
string urlResource = $"/{storageAccount}";
String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, storageKey);
return AuthorizationHeader;
}