Here's my code to find Storage Containers:
var api = $"https://{storageAccountName}.blob.core.windows.net/?comp=list";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken); //token obtained from https://storage.azure.com/
client.BaseAddress = new Uri($"https://{storageAccountName}.blob.core.windows.net/");
using (var responseGet = client.GetAsync(api).Result)
{
if (responseGet.IsSuccessStatusCode)
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(responseGet.Content.ReadAsStringAsync().Result);
foreach (XmlNode a in xmlDocument.DocumentElement.SelectNodes("Containers/Container"))
{
containerNameList.Add(a.SelectSingleNode("Name").FirstChild.Value);
}
}
}
}
I got an error:
`StatusCode: 403, ReasonPhrase: 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.', Version: 1.1, Content:
System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
Server: Windows-Azure-Blob/1.0
Server: Microsoft-HTTPAPI/2.0
x-ms-request-id: 9d70d7ff-901e-0096-4c5b-aec38d000000
Date: Mon, 09 Dec 2019 06:38:16 GMT
Content-Length: 438
Content-Type: application/xml
}`
I obtained the access token from https://storage.azure.com/
And here's the code to remove the storage container:
var strApi = $"https://{storageAccountName}.blob.core.windows.net/{storageContainerName}?restype=container";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
client.BaseAddress = new Uri(BaseManagementUri);
using (var responseGet = client.DeleteAsync(strApi).Result)
{
if (responseGet.IsSuccessStatusCode)
{
log.LogInformation($"Deleted {storageAccountName}");
}
else
{
log.LogWarning($"Failed to deleted {storageAccountName}\n{responseGet.Content.ReadAsByteArrayAsync().Result}");
}
}
}
How to obtain the proper access token and what all headers required for the above operations ?