4. Creating a table using the REST API
You created a table using the following StorageClient library call:
var storageAccount =
CloudStorageAccount.Parse(
ConfigurationManager.AppSettings["DataConnectionString"]);
CloudTableClient tableClient =
storageAccount.CreateCloudTableClient();
tableClient.CreateTableIfNotExist("ShoppingCartTable");
Ultimately the StorageClient
library just wraps the REST API that’s exposed by the Table service.
Let’s take a look at how this is done.
Creating a Table Using AtomPub
To create a new table, you must perform a POST request against the URI you used earlier to list and delete tables in the silverlightukstorage storage account.
POST http://silverlightukstorage.table.core.windows.net/Tables
Because the Table service implements the AtomPub protocol, the body of the POST
request needs to be in the Atom document format. The following Atom
document instructs the Table service to create a new table called
ShoppingCartTable in the storage account:
As you can see from this
verbose piece of junk, creating a simple table requires a whole bunch of
useless information that’s never used.
This
is a complete rant, and we do apologize for it, but it has to be said.
The REST API method of creating tables is unnecessarily complex and
verbose. We know you probably won’t care because you’ll use the
StorageClient library to create tables rather than the REST API. We also
know that it’s not Microsoft’s fault—they’re just following the
standard. But could we not have a simpler API call?
The method of deleting the ShoppingCartTable is pretty simple; it’s the HTTP verb DELETE with the appropriate URI, such as this one:
http://silverlightukstorage.table.core.windows.net/Tables ('ShoppingCartTable')
It’s pretty simple, isn’t it?
Why does AtomPub have that mad method of creating a table? We can’t
help thinking that a simpler method of creating tables would be to use
the same URI as DELETE and change the HTTP verb from DELETE to POST.
|
Instead of using AtomPub to
create tables with a crazy amount of XML, you can do the same thing with
the slightly easier-to-use REST API. We’ll eventually get to the
easiest way to create a table, which is with the StorageClient library.
Creating the Table Using the REST API in a Console Application
It’s time for you to
create a small console application that will generate the
ShoppingCartTable in a storage account using AtomPub and the REST API.
The listing that follows contains the code for the console application.
Listing 2. Creating tables using the REST API and AtomPub
The console application in listing 2 is pretty much the same as the one in listing 12.1
that listed tables. But there are a few differences between the two
applications. In this case, you use the URI of your table endpoint, and
you need to change the HTTP verb from GET to DELETE. You also need to convert the Atom XML you used earlier from a string to a byte array. Finally, you write the Atom XML byte[] to the request body.
Over the past few
sections, we’ve looked at how you can interact with the REST API
directly. In each example, you’ve used the StorageClient library to sign
the request, but we haven’t spent any time explaining that. Let’s take a
little time out to do that now.