Q91.How to use Windows Azure Blob Storage Service in .NET?
Programming Blob storage service can be in two ways:
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.
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.
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-level public access. Anonymous clients can read blob content and metadata and container metadata, and can list the blobs within the container.
No anonymous access. Only the account owner can access resources in this container.
blob.UploadFromStream() which eventually invoking PutBlob REST-API
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