Create Blob in Storage Emulator

In this article we are trying to create a blob in the storage emulator. The same blob creation can be done on storage account too.

Some Theories on Blob in Azure

  • Blob represents Binary Large Object
  • It can contain text and binary data
  • Containers are required for storing Blobs
  • Containers can be private and public
  • Blob can have metadata

The storage account created should be having containers for storing blobs. The containers can be viewed as folders.

Eg: Documents, Images etc.

The private containers are private to the storage account and only the account holder can access it. The urls in public containers are accessible over the internet.

The Blob can have metadata as collection of key value pair. We can store the information like Author, Date, and Description etc. for the blob.

Advantages of Blob

The blob helps us to store image files, documents etc. which are normally unstructured data. If the same is stored in database, it results in performance problems.

Without a blob service, the manual programming requires much effort to manage the large sized files.

The activities include:

  • Creating a File/Folder infrastructure in the web application space
  • Create with multipart uploads for uploading large files
  • Create a database table for managing the metadata
  • Create the corresponding entities
  • Manage with backup and restore of the large files involving high network traffics

Thanks to Microsoft, the blob service does all the things for us at a lower cost.


Steps in creating Blob

Following are the steps in creating a blob.

clip_image002

The first step (Create Storage Account) was already done from our side and we need to proceed with the further steps to create the blob.

There are two ways of creating container and blob

  • Through Program
  • Using Windows Azure Management Tool

In this article we are proceeding with the first way and the second way will be discussed in another upcoming article.

Following are the steps involved in the creation of container and blob.


Step 1: Create new web azure project

Create a new Windows Azure project and add a web role into it. The solution explorer would look like below.

clip_image004

Step 2: Add reference to library files

We need to add specific reference to the following Windows Azure SDK dll file.

Dll: Microsoft.WindowsAzure.StorageClient.dll

Location: C:Program FilesWindows Azure SDKv1.5ref

clip_image006

Step 3: Create the container

Now open the code view of Default.aspx, place a label control on it and enter the following code.

protected void Page_Load(object sender, EventArgs e)

{

CloudStorageAccount account = CloudStorageAccount.DevelopmentStorageAccount;

CloudBlobClient client = account.CreateCloudBlobClient();

CloudBlobContainer container = client.GetContainerReference("documents");

container.CreateIfNotExist();

CloudBlob blob = container.GetBlobReference("File.txt");

blob.UploadFile(Server.MapPath("~/File.txt"));

// show the blobs

Label1.Text = "Blobs inside container: <br>";

foreach (IListBlobItem item in container.ListBlobs())

Label1.Text += item.Container + " " + item.Parent.ToString() + " " +

item.Uri.ToString() + " <br>";

}

Note: The container name as documents. We need to ensure the name should be lowercase and additional restrictions. An invalid naming convention will throw exception StorageClientExceptionwith message “One of the request inputs is out of range.”

There are two methods in class CloudBlobContainer for creating the container.

  • Create()
  • CreateIfNotExist()

Using the CreateIfNotExist() method is advised as it won’t throw the exception if a container already exists with the given name.

Step 4: Create the blob file

In the web role project add a file named File.txtwith some text contents. Execute the application and you can see the following result.

clip_image008

Note: The local Storage Emulator is used in the above code for creating the container and blob. In the real scenario of uploading to online storage account, we need to modify the line:

clip_image010

as shown below

clip_image012

Summary

In this article we have learned some information on blob, container and how to create them programmatically. The associated source code can be used to test blob creation in your local machine.

Leave a Reply

Your email address will not be published. Required fields are marked *