Q91.How to use Windows Azure Blob Storage Service in .NET?
Programming Blob storage service can be in two ways:
In this topic, we will use Windows Azure Storage Client Library to create a blob container, list all blobs in a container, perform Update and Delete operations against blobs.
Add a new Web Form to a project and place a FileUpload and button control in the page. The resulting aspx page code is shown below. To upload files from the web page, we are using the built-in ASP.Net FileUpload control.
In the code-behind file, Webform1.aspx.cs, use the following code to upload the blobs to the images container in the storage emulator. This example shows the basics of uploading of a block blob to the cloud from an ASP.Net application and make it publicly available.
I have used the following classes of Storage Client Library in the code.
CloudStorageAccount: This class represents the Storage Account. It reads the Storage account name for the configuration file which has to be created before deploying an application on the cloud. This class manages all the storage services.
CloudBlobClient: It provides a client for accessing the Windows Azure blob service.
CloudBlobContainer: This represents a container for a Blob.
The following listing is the code for the upload button click event. On click of the upload button, the file is captured and then uploaded to the BLOB storage.
a. We use the CloudStorageAccount type to represent the Storage Account information. If the Windows Azure project has a reference to Microsoft.WindowsAzure.CloudConfigurationManager, we can use the CloudConfigurationManager type to retrieve the storage connection string and storage account information from the Windows Azure service configuration file with the code given below.
b.If the connection string is located in the web.config or app.config file, then we have to use ConfigurationManager to retrieve the connection string with the code given below.
c. If you are using Windows Azure storage emulator for development purposes that means your application is working against the storage services locally. The below code works for this scenario.
Note that, there is a bug in v2.0. of the storage SDK. In such a case, use the code given below and refer the storage account directly.
Note that CloudStorageAccount.SetConfigurationSettingPublisher() is no longer supported, use CloudConfigurationManager.GetSetting() instead, like this:
We know that all blobs reside in a container. The CloudBlobClient type allows us to retrieve objects that represent containers and blobs stored within the Blob Storage Service. We use a CloudBlobContainer object to get a reference to the container. We can create the container if it doesn't exist. By default, the new container is private and we must specify the storage access key to download blobs from this container. We can set the container to ‘public’ and to make it available to everyone.
BlobContainerPublicAccessType Enumeration specifies the level of public access that is allowed on the container.
blob.UploadFromStream() which eventually invoking PutBlob REST-API
Summary :
Firstly we create CloudStorageAccount instance. Then, we call the account's CreateCloudBlobClient method to get the CloudBlobClient. We get a reference to the blob container and set the permission of the container. Get a reference to the blob by calling GetBlobReference and pass the file name and then call UploadFromStream to start the upload.
- Using Storage REST APIs
- Blob APIs - Windows Azure Storage Client Library
In this topic, we will use Windows Azure Storage Client Library to create a blob container, list all blobs in a container, perform Update and Delete operations against blobs.
Uploading of a block blob to the cloud from an ASP.Net application
Add a new Web Form to a project and place a FileUpload and button control in the page. The resulting aspx page code is shown below. To upload files from the web page, we are using the built-in ASP.Net FileUpload control.

In the code-behind file, Webform1.aspx.cs, use the following code to upload the blobs to the images container in the storage emulator. This example shows the basics of uploading of a block blob to the cloud from an ASP.Net application and make it publicly available.
I have used the following classes of Storage Client Library in the code.
CloudStorageAccount: This class represents the Storage Account. It reads the Storage account name for the configuration file which has to be created before deploying an application on the cloud. This class manages all the storage services.
CloudBlobClient: It provides a client for accessing the Windows Azure blob service.
CloudBlobContainer: This represents a container for a Blob.
The following listing is the code for the upload button click event. On click of the upload button, the file is captured and then uploaded to the BLOB storage.

1.Retrieving a connection string from Windows Azure service configuration file - ServiceConfiguration.cscfg
a. We use the CloudStorageAccount type to represent the Storage Account information. If the Windows Azure project has a reference to Microsoft.WindowsAzure.CloudConfigurationManager, we can use the CloudConfigurationManager type to retrieve the storage connection string and storage account information from the Windows Azure service configuration file with the code given below.
CloudStorageAccount account = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting(“StorageConnectionString”));
b.If the connection string is located in the web.config or app.config file, then we have to use ConfigurationManager to retrieve the connection string with the code given below.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse( ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
c. If you are using Windows Azure storage emulator for development purposes that means your application is working against the storage services locally. The below code works for this scenario.
CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(“StorageConnectionString”));
Note that, there is a bug in v2.0. of the storage SDK. In such a case, use the code given below and refer the storage account directly.
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
Note that CloudStorageAccount.SetConfigurationSettingPublisher() is no longer supported, use CloudConfigurationManager.GetSetting() instead, like this:
CloudStorageAccount storageaccount= CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(configurationSettingName));
2.How to Create a container and set the Permission at Container Level for Public Access
We know that all blobs reside in a container. The CloudBlobClient type allows us to retrieve objects that represent containers and blobs stored within the Blob Storage Service. We use a CloudBlobContainer object to get a reference to the container. We can create the container if it doesn't exist. By default, the new container is private and we must specify the storage access key to download blobs from this container. We can set the container to ‘public’ and to make it available to everyone.
BlobContainerPublicAccessType Enumeration specifies the level of public access that is allowed on the container.
Member Name | Description |
Blob | Blob-level public access. Anonymous clients can read the content and metadata of blobs within this container, but cannot read container metadata or list the blobs within the container. |
Container | Container-level public access. Anonymous clients can read blob content and metadata and container metadata, and can list the blobs within the container. |
Off | No anonymous access. Only the account owner can access resources in this container. |
blob.UploadFromStream() which eventually invoking PutBlob REST-API
Summary :
Firstly we create CloudStorageAccount instance. Then, we call the account's CreateCloudBlobClient method to get the CloudBlobClient. We get a reference to the blob container and set the permission of the container. Get a reference to the blob by calling GetBlobReference and pass the file name and then call UploadFromStream to start the upload.
See More Questions and Answers on - Azure Blobs and Queues
- Partitions and Queries?
- How to insert multiple records in a transaction using TableBatchOperation?
- Partition size in Azure table storage?
- What are the typical query types for table storage?
- Explain blobs data model?
- How to address resources in the storage emulator?
- Configure a .Net application for using storage emulator?
- How to create a container and upload a blob using the server explorer?
- Key benefits of SQL database service?