16.10. Accessing the REST API Directly

Now that you have worked with the StorageClient, I think it is useful to understand what is happening behind the scenes. In the previous example, you created a container to store your images, called pictures. You will now create an application to list all the containers in your local Azure storage by constructing the raw HTTP request.

16.10.1. How Do You Work with the REST API?

To interface with the Azure storage REST API, you will construct a request using the WebRequest classes. You need to do the following:

  1. Make an HTTP request to a URL and port. The following URL, for example, is used to retrieve a list of containers held in Azure storage:

    http://127.0.0.1:10000/devstoreaccount1/devstoreaccount1?comp=list

  2. Set a number of headers in the request.

  3. Set the HTTP verb of the request to describe what you are doing (e.g., GET, PUT).

  4. Calculate a hash of the headers you added and a hidden key. This ensures no one can modify the request and allows Azure storage to authenticate you.

  5. Azure storage will then return your results as XML.

Azure storage authenticates each user by hashing the headers (using SHA 256) with a shared secret key. If anyone tampers with a header or the wrong key is used, then the hash will not match what Azure is expecting, and it will return an HTTP 403 error (not authenticated). Note that, for additional security, Azure messages expire after 15 minutes and will be rejected.

16.10.2. Working with Azure Storage with Raw HTTP Requests

Create a new console application called Chapter16.AzureRawHttp, and then follow these steps:

  1. Add the following using directive:

    using System.Net;

  2. Add the following code to the Main() method. This code constructs an HTTP request and sends it to Azure local storage to list containers:

    //Gets a list of containers
                string AccountName = "devstoreaccount1";
                string AccountSharedKey = "<YOUR_SHARED_KEY";
                string Address = "http://127.0.0.1";
                string Port = "10000";
    
                //Action to perform e.g. ?comp=list
                string QueryString="?comp=list";
    
                string uri = Address + ":" + Port + "/" + AccountName + QueryString;
                string MessageSignature = "";
    
                //Build Request
                HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(uri);
                Request.Method = "GET";
                Request.ContentLength = 0;
                Request.Headers.Add("x-ms-date", DateTime.UtcNow.ToString("R"));

    //Create signaure of message contents
                MessageSignature+="GET
    "; //Verb
                MessageSignature+="
    "; //MD5 (not used)
                MessageSignature+="
    "; //Content-Type
                MessageSignature+="
    "; //Date optional if using x-ms-date header
                MessageSignature += "x-ms-date:" + Request.Headers["x-ms-date"] + "
    "; //Date
                MessageSignature+="/"+AccountName+"/"+AccountName+QueryString; //resource
    
                //Encode signature using HMAC-SHA256
                byte[] SignatureBytes = System.Text.Encoding.UTF8.GetBytes(MessageSignature);
    
                System.Security.Cryptography.HMACSHA256 SHA256 =
                  new System.Security.Cryptography.HMACSHA256(
                    Convert.FromBase64String(AccountSharedKey)
                  );
    
                // Now build the Authorization header
                String AuthorizationHeader =  "SharedKey " + AccountName + ":"
                  + Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));
    
                // And add the Authorization header to the request
                Request.Headers.Add("Authorization", AuthorizationHeader);
    
                //Get response
                HttpWebResponse Response = (HttpWebResponse) Request.GetResponse();
    
                using (System.IO.StreamReader sr =
                  new System.IO.StreamReader(Response.GetResponseStream()))
                {
                    Console.WriteLine(sr.ReadToEnd());
                }
                Console.ReadKey();

  3. Press F5 to run your application.

You should have a response like the following (in my example I have two blob containers: blobs and pictures):

<?xml version="1.0" encoding="utf-8"?>
<EnumerationResults AccountName="http://127.0.0.1:10000/devstoreaccount1">
<Containers>
<Container>
<Name>blobs</Name>
<Url>http://127.0.0.1:10000/devstoreaccount1/blobs</Url>
<LastModified>Mon, 16 Nov 2009 02:32:13 GMT</LastModified>
<Etag>0x8CC347C240E3FE0</Etag>
</Container>
<Container>
<Name>pictures</Name>

<Url>http://127.0.0.1:10000/devstoreaccount1/pictures</Url>
<LastModified>Mon, 16 Nov 2009 09:16:40 GMT</LastModified>
<Etag>0x8CC34B4A41BA4B0</Etag>
</Container><Container>
<Name>wad-control-container</Name>
<Url>http://127.0.0.1:10000/devstoreaccount1/wad-control-container</Url>
<LastModified>Mon, 16 Nov 2009 09:16:21 GMT</LastModified>
<Etag>0x8CC34B498B195D0</Etag>
</Container>
</Containers>
<NextMarker />
</EnumerationResults>

If you want to know more about working with the REST API directly, please refer to the SDK documentation, which specifies the format of requests. David Lemphers also has a good series of articles on working with Azure storage (based on preview versions, so they may be a bit out-of-date now):

http://blogs.msdn.com/davidlem/archive/2008/12/20/windows-azure-storage-exploring-blobs.aspx.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.245.167