Exemplo n.º 1
0
        /// <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}");
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static HttpWebRequest CreateRESTRequest(string storageAccountName, string storageAccountKey,
                                                        string containerName, string fileName,
                                                        string method, string requestBody   = null,
                                                        SortedList <string, string> headers = null, string ifMatch = "", string md5 = "")
        {
            byte[]   byteArray = null;
            DateTime now       = DateTime.UtcNow;
            string   uri       = string.Format("https://{0}.blob.core.windows.net/{1}/{2}", storageAccountName, containerName, fileName);

            HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;

            request.Method        = method;
            request.ContentLength = 0;

            request.Headers.Add("x-ms-date", now.ToString("R", System.Globalization.CultureInfo.InvariantCulture));
            request.Headers.Add("x-ms-version", "2014-02-14");
            request.Headers.Add("x-ms-blob-type", "BlockBlob");
            //if there are additional headers required, they will be passed in to here,
            //add them to the list of request headers
            if (headers != null)
            {
                foreach (KeyValuePair <string, string> header in headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }
            //if there is a requestBody, add a header for the Accept-Charset and set the content length
            if (!String.IsNullOrEmpty(requestBody))
            {
                request.Headers.Add("Accept-Charset", "UTF-8");

                byteArray             = System.Text.Encoding.UTF8.GetBytes(requestBody);
                request.ContentLength = byteArray.Length;
            }

            request.Headers.Add("Authorization", AzureStorageAuthenticationHelper.AuthorizationHeader(storageAccountName, storageAccountKey, method, now, request, ifMatch, md5));

            //now set the body in the request object
            if (!String.IsNullOrEmpty(requestBody))
            {
                request.GetRequestStream().Write(byteArray, 0, byteArray.Length);
            }
            return(request);
        }
        /// <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);
                    }
                }
            }
        }