3. Creating a container
Now you’re going to create the web page shown in figure 1. In your podcast sample web role project, create a new ASPX page called containers.aspx.
At this stage, you only want to
write the code that will create your container; you don’t need to see
the list of containers. At present, your UI needs to display only the
new container name text box and the Create Container button.
The following listing shows the ASPX required for the create-container section of the page.
Listing 1. ASPX for creating a container
You’ve
defined your UI. Now you need to create the code that handles the
button click. See the following listing, which contains the code-behind
for the create button click event.
Listing 2. Creating a new container
Before we explain this code, we
want to remind you of its purpose. The user will type in the new
container name and then click the button to create the new container.
Now let’s look at the code.
Storage Account
The first thing you need to do is retrieve an object that allows you to work with the BLOB storage account. Using the CloudStorageAccount object that you used earlier to extract your credentials, you can now instantiate a CloudBlobClient object that will allow you to mess with things at an account level by issuing the following call:
CloudBlobClient blobClient =
account.CreateCloudBlobClient();
After you’ve retrieved the CloudBlobClient object, you can perform the following operations at an account level on BLOB storage:
Return a list of all containers in the account (ListContainers)
Get a specific container (GetContainerReference)
List BLOBs (ListBlobsWithPrefix)
Get a specific BLOB (GetBlobReference)
As well as performing these operations, you can also set some general policies, including the following ones:
In this example, because
you’re creating a new container, you need to grab a reference to the
container that you want to create. Use the GetContainerReference method, passing in the name of your new container:
CloudBlobContainer container =
blobClient.GetContainerReference(txtContainerName.Text.ToLower());
In this example, you’re setting the container name to whatever the user types in the text box.
Note
The name of the container
is converted to lowercase because the BLOB storage service doesn’t allow
uppercase characters in the container name.
So far you’ve just set up the container you want to create; you haven’t made any communication with the storage service. The CloudBlobContainer object that has been returned by GetContainerReference can perform the following operations:
Create a container (Create)
Delete a container (Delete)
Get and set any custom metadata you want to associate with the container
Get properties associated with the container (for example, ETag and last modified time)
Get and set container permissions
List BLOBs (ListBlobs)
Get a specific BLOB
Now make a call to create the container:
As soon as you call the Create method, the storage client generates an HTTP request to the BLOB storage service, requesting that the container be created.
In the Create
container call, you didn’t specify any permissions on the container to
be created. By default, a container is created as private access only,
meaning that only the account owner can access the container or any of
the BLOBs contained within it. In the next chapter, we’ll look at how
you can set permissions on containers and BLOBs.
|
You should now be able to run
your web role and create some containers in your development storage
account. At this point, you won’t be able to see the containers that
you’ve created in your web page, but you can check that they’re there by
running a SQL query against the BlobContainer table in the development
storage database.
Now that you can create a
container from your web page, you need to modify the page so that you
can display all the containers in your storage account.