For now, we’ll concentrate purely on the operations
that you can perform against a storage account using the REST API.
Although we’ve already looked at these operations using the
StorageClient library, it’s still useful to look at the REST API.
Ultimately, the StorageClient library is just a wrapper library for the
calls we’re about to look at. Over the next few sections, we’ll look at
the following operations:
Listing tables
Deleting tables
Creating tables
The URI of the Table service endpoint uses the following
structure:
http://<storageaccount>.table.core.windows.net/
If your storage account was named silverlightukstorage, your URI would be the following:
http://silverlightukstorage.table.core.windows.net/
For the development storage Table service, you’d use the following URI:
http://127.0.0.1:10002/devstoreaccount1/
Now that you know what the URIs will look like, let’s try using them.
1. Listing tables in the development storage account using the REST API
We looked at a small console application that listed all the containers
in a BLOB storage account using the REST API. In this section, you’ll
create a similar console application that will list all the tables in a development storage account. Figure 1 shows the output of this console application.
If you look at the output in figure 1 you can see that the Products table is returned in the list of storage accounts.
As you may have already gathered, normal people don’t create APIs that output XML like what you see in figure 1. You need a standard to generate that level of verboseness and complexity.
The standard that the Table service uses to expose its data is known as AtomPub. We’ll discuss it in more detail later.
If you’re interested in being
able to identify AtomPub documents in the wild, you can always look at
the XML namespace. As you can see in figure 1, it’s referencing the Atom namespace:
xmlns=http://www.w3.org/2005/Atom
|
If you wanted to list all the tables in a storage account using the StorageClient library, you could do the following:
var storageAccount =
CloudStorageAccount.Parse(
ConfigurationManager.AppSettings["DataConnectionString"]);
CloudTableClient tableClient =
storageAccount.CreateCloudTableClient();
tableClient.ListTables();
Let’s now take a look at how
this could be done using the REST API directly. You might not usually
do this directly with REST, but we want you to appreciate what’s really
happening behind the scenes.
In order to get a list of all
tables that exist in the storage account, all you need to do is write
some code that will perform a GET request against the following URIs:
http://127.0.0.1:10002/devstoreaccount1/Tables (for dev storage)
http://<storageaccount>.table.core.windows.net/Tables (for live)
To create the application that generated the output shown in figure 1,
you’ll need to create a new console application in Visual Studio. To
keep the example simple, we’ll reuse the StorageClient library’s
credential-signing method.
The following listing contains the code for the console application.
Listing 1. Listing the tables in a storage account
The code displayed in listing 1 will make a GET request to the development Table service asking for a list of all the tables in the storage account (http://127.0.0.1:10002/devstoreaccount1/Tables).
At , you generate the HTTP request by calling the CreateHttpRequest method. This method creates the HttpRequest for the given URI (http://127.0.0.1:10002/devstoreaccount1/Tables) and HTTP verb (GET) and returns the request to the calling method.
You’ve probably noticed that listing 12.1 makes use of the storage account credentials from the StorageClient library ,
even though it’s using the REST API. The major reason for this is that
signing the HTTP request manually is hard and horrible. Rather than
writing that nasty code, it’s easier to use the StorageClient library
method.
|
Finally, with the request generated and signed, you can make the request to the development service .
The Table service will return an XML response listing all of the tables
in your account, which is written to the console window, as you saw in figure 12.1.
Switching to the Live Service
In listing 1
you made a request to the development storage Table service. If you
wanted to change the application to query a live service (such as a
silverlightukstorage storage account) you’d need to change the URI at to http://silverlightukstorage.table.core.windows.net/Tables.
It’s worth pointing out that although the URI in listing 1 is hardcoded, you could extract it from the storageAccount object:
storageAccount.TableEndpoint.AbsoluteUri.ToString();
This is just the base URI; you’ll still need to append /tables to access the Table service.