/// <summary> /// This is the method to call the REST API to retrieve a list of /// containers in the specific storage account. /// This will call CreateRESTRequest to create the request, /// then check the returned status code. If it's OK (200), it will /// parse the response and show the list of containers found. /// </summary> private static async Task ListContainersAsyncREST(string storageAccountName, string storageAccountKey, CancellationToken cancellationToken) { // Construct the URI. This will look like this: // https://myaccount.blob.core.windows.net/resource String uri = string.Format("https://{0}.blob.core.windows.net?comp=list", storageAccountName); // Set this to whatever payload you desire. Ours is null because // we're not passing anything in. Byte[] requestPayload = null; //Instantiate the request message with a null payload. using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri) { Content = (requestPayload == null) ? null : new ByteArrayContent(requestPayload) }) { // Add the request headers for x-ms-date and x-ms-version. DateTime now = DateTime.UtcNow; httpRequestMessage.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture)); httpRequestMessage.Headers.Add("x-ms-version", "2017-04-17"); // If you need any additional headers, add them here before creating // the authorization header. // Add the authorization header. httpRequestMessage.Headers.Authorization = AzureStorageAuthenticationHelper.GetAuthorizationHeader( storageAccountName, storageAccountKey, now, httpRequestMessage); // Send the request. using (HttpResponseMessage httpResponseMessage = await new HttpClient().SendAsync(httpRequestMessage, cancellationToken)) { // If successful (status code = 200), // parse the XML response for the container names. if (httpResponseMessage.StatusCode == HttpStatusCode.OK) { String xmlString = await httpResponseMessage.Content.ReadAsStringAsync(); XElement x = XElement.Parse(xmlString); foreach (XElement container in x.Element("Containers").Elements("Container")) { Console.WriteLine("Container name = {0}", container.Element("Name").Value); } } else { Console.WriteLine($"There was an error: {httpResponseMessage.StatusCode} {httpResponseMessage.ReasonPhrase}"); } } } }
/// <summary> /// This is the method to call the REST API to retrieve a list of /// containers in the specific storage account. /// This will call CreateRESTRequest to create the request, /// then check the returned status code. If it's OK (200), it will /// parse the response and show the list of containers found. /// </summary> private static async Task ListContainersAsyncREST(string storageAccountName, string storageAccountKey, CancellationToken cancellationToken) { // Construct the URI. This will look like this: // https://myaccount.blob.core.windows.net/resource String uri = string.Format("http://{0}.blob.core.windows.net?comp=list", storageAccountName); String blobUri = string.Format("http://{0}.blob.core.windows.net/documents?restype=container&comp=list", storageAccountName); // Set this to whatever payload you desire. Ours is null because // we're not passing anything in. Byte[] requestPayload = null; //Instantiate the request message with a null payload. //using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri) using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, blobUri) { Content = (requestPayload == null) ? null : new ByteArrayContent(requestPayload) }) { // Add the request headers for x-ms-date and x-ms-version. DateTime now = DateTime.UtcNow; httpRequestMessage.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture)); httpRequestMessage.Headers.Add("x-ms-version", "2017-07-29"); // If you need any additional headers, add them here before creating // the authorization header. // Add the authorization header. httpRequestMessage.Headers.Authorization = AzureStorageAuthenticationHelper.GetAuthorizationHeader( storageAccountName, storageAccountKey, now, httpRequestMessage); // Send the request. using (HttpResponseMessage httpResponseMessage = await new HttpClient().SendAsync(httpRequestMessage, cancellationToken)) { // If successful (status code = 200), // parse the XML response for the container names. if (httpResponseMessage.StatusCode == HttpStatusCode.OK) { var blobName = "documents"; String bloburiForDisplay = string.Format("http://{0}.blob.core.windows.net/{1}", storageAccountName, blobName); String xmlString = await httpResponseMessage.Content.ReadAsStringAsync(); XElement x = XElement.Parse(xmlString); //foreach (XElement container in x.Element("Containers").Elements("Container")) //{ // Console.WriteLine("Container name = {0}", container.Element("Name").Value); //} int n = 0; foreach (XElement container in x.Element("Blobs").Elements("Blob")) { //Console.WriteLine("Blob name = {0}", container.Element("Name").Value); //Console.WriteLine("Blob URL = {0}", container); //Console.WriteLine("Properties = {0}", container.Element("Properties").Value); if (container.Element("Name").Value.Contains("custom_files")) { n++; Console.WriteLine("Custom -Blob name = {0} and Url = {1}/{0}", container.Element("Name").Value, bloburiForDisplay); } //else //{ // Console.WriteLine("Non-Custom Blob name = {0}", container.Element("Name").Value); //} } Console.WriteLine("Total = " + n); } } } }