Windows Azure Training Series: Writing to Blob Storage

My last couple posts covered the basics of Azure storage. See, Windows Azure Training Series:  Understanding Azure Storage and Windows Azure Training Series:  Creating an Azure Storage Account. Now let’s write some code.

Accessing Blob Storage

Blob storage is accessed using the CloudBlobClient class. When creating it, specify the configuration information, as shown in the last post. Notice below, a property encapsulates the creation of this class. All methods that read or write to blob storage will utilize this property.

private static CloudBlobClient cloudBlobClient{ get { CloudStorageAccount cloudStorageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); return cloudStorageAccount.CreateCloudBlobClient(); } } 

Creating a Blob Container

Before we can write a blob, we need to create a container for it. It’s just 2 lines of code as shown below. The “containerName” variable is just a string, which represents the name of the container. A container is like a folder on a hard disk.

CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); container.CreateIfNotExist(); 

Uploading Blobs to Containers

The following function writes a file to a blob container. Files can be uploaded using an html file upload control. Notice, you need to first get a reference to the container, and then use the UploadFromStream() method to write the file.

public static void CreateBlob(string containerName, HttpFileCollection files) { CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); foreach (string name in files) { var file = files[name]; string blobName = System.IO.Path.GetFileName(file.FileName); CloudBlob cloudBlob = container.GetBlobReference(blobName); cloudBlob.Metadata["rightanswer"] = file.FileName; cloudBlob.UploadFromStream(file.InputStream); } } 

Listing the Files in a Blob Container

The following code will list all the files in a container specified.

CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); return container.ListBlobs().ToList(); 

Accessing Blobs

The following code can be used to access a blob. In this case, the first blob in the container specified.

CloudBlob blob; var blobs = cloudBlobClient.GetContainerReference(containerName).ToList(); blob = blobs[0] as CloudBlob; 

Or, we can access a blob by its name as shown below.

CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); CloudBlob cloudBlob = container.GetBlobReference(blobName); 

Deleting Blobs

The following code can be used to delete a blob.

CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); CloudBlob cloudBlob = container.GetBlobReference(blobName); cloudBlob.Delete(); 

In the next post, we’ll take a look at the code required to write to Windows Azure Table storage.

Doug Rehnstrom

To learn more about Windows Azure, check out Learning Tree’s course, Windows Azure Platform Introduction:  Programming Cloud-Based Applications.

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.