Geeks With Blogs
Deeksha Sharma
uploading,lisiting ,downloading and deleting the blobs

azure blob storage is a service for storign large amounbts of 

unstructured data that can be accessed from anywhere in the world 

via HTTP or HTTPS.A single blob can be hundreds of gigabytes in 

size.

Common uses of Blob storage:-

1)Serving images or documents directly to a browser
2)Storing files for distributed access
3) Streaming video and audio
4) performing secure backup and disater recovery
5)Storing data for analysis by an on-premises or Azure-hosted 

service.



Things needed :-

Storage account:-   all access is done through it.

Container:-    A container provides a grouping of a set of blobs.All 

blob must  be a container. there can be multiple container for an 

account.

Blob:- A file of any type and size.there are two types of blobs that 

can be stored in azure storage: 

a) block 
b) page blobs

block blob size --200GB
page blob-1TB

URL format:- blobs are addressable using the following URL format:-

http://<storage account>.blob.core.windows.net/movies/MOV1.AVI







WORKING WITH BLOBS:-

1) create an azure storage account

a) Log into the Azure Management Portal
b) At the bottom of the navigation pane ,click NEW
c) Click on data services= > Storage = > Quick Create



URL - a sub domain name to use in the URI for the storage 

account.This value becomes the host name with in the URI that is 

used to address Blob,queue or table resource for the subscription.]
d) choose a Region/Affinity Group in which to locate the storage
e) Select the replication you desire for your account.Geo redundant 

replication is the default and provides maximum durability.


then click create storage account

SETUP A STORAGE CONNECTION STRING:-
the azure storage client library for .net supports using a storage 

connection string to configure endpoints and creadentials for 

accessing storage services.

Maintain connection string inside configuration file,
If your application runs in an azure cloud service ,save your 

connection string using the Azure service configuration system
(* .csdef and *.cscfg files).

If your application runs on Azure virtual machines ,or if you are 

building .net applications that will run outside of Azure,save your 

connectionstring using the .net configuration system(e.g. web.config 

or app.config).


Configuring your connection string from an Azure cloud service:-

azure Cloud Service has a unique service configuration mechanism 

that enables you to dynamically change configuration settings from 

the Azure Management Portal without redeploying your application.

To configure your connections string in the Azure service 

configuration:

1) Within the solution explorer of visual studio , in the ROles 

folder of your Azure Deployment Project,Right CLick you web role or 

worker role and CLick Properties

right click on the worker role = > click on properties
CLick the settings tab and press the Add Setting 

A new Setting1 entry will then show up the settings grid.

3) In the Type drop-down of the new Setting1 entry,choose Connection 

string 

4) CLick the ... button at the right end of the Setting1 entry .The 

Storage Account Connection string dialog will open.

5) choose whether you want to target the storage emulator or a 

storage account in the cloud.

Create storage connection string

select
Manually entered credentials

Account name 
use HTTPS

Change the entry Name from Setting1 to a friendlier name like 

storageConnectionString.




Configuring your connection string using .Net configuration

if you are writing an application that is not an Azure cloud 

service,use Web.config or app.config

Store the connection string using the <appsettings> element as 

follows:-

Replace account-name with the name of your storage account and 

account -key with your account access key.

<configuration>
    <appSettings>
        <add key="StorageConnectionString" 

value="DefaultEndpointsProtocol=https;AccountName=account-

name;AccountKey=account-key" />
    </appSettings>
</configuration>

or

<configuration>
    <appSettings>
        <add key="StorageConnectionString" 

value="DefaultEndpointsProtocol=https;AccountName=storagesample;Acco

untKey=nYV0gln9fT7bvY

+rxu2iWAEyzPNITGkhM88J8HUoyofpK7C8fHcZc2kIZp6cKgYRUM74lHI84L50Iau1+9

hPjB==" />
    </appSettings>
</configuration>


HOW to -Programmatically access Blob storage 

use nuget to obtain the Microsoft.WindowsAzure.Storage.dll assembly.


Microsoft.WindowsAzure.Storage.dll is also included in the Azure SDK 

for .NET, which can be downloaded from the .NET Developer Center. 

The assembly is installed to the %Program Files%\Microsoft SDKs

\Windows Azure\.NET SDK\<sdk-version>\ref\ directory.


using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;



If you are creating an application with no reference to 

Microsoft.WindowsAzure.CloudConfigurationManager, and your 

connection string is located in the web.config or app.config as show 

above, then you can use ConfigurationManager to retrieve the 

connection string. You will need to add a reference to 

System.Configuration.dll to your project, and add another namespace 

declaration for it:

using System.Configuration;
...
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    ConfigurationManager.ConnectionStrings

["StorageConnectionString"].ConnectionString);


A CloudBlobClient type allows you to retrieve objects that represent 

containers and blobs stored within the Blob Storage Service. The 

following code creates a CloudBlobClient object using the storage 

account object we retrieved above:

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container. 
CloudBlobContainer container = blobClient.GetContainerReference

("mycontainer");

// Create the container if it doesn't already exist.
container.CreateIfNotExists();

By default, the new container is private and you must specify your 

storage access key to download blobs from this container. If you 

want to make the files within the container available to everyone, 

you can set the container to be public using the following code:

container.SetPermissions(
    new BlobContainerPermissions { PublicAccess = 
    BlobContainerPublicAccessType.Blob }); 

Azure Blob Storage supports block blobs and page blobs. In the 

majority of cases, block blob is the recommended type to use.


// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting

("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference

("mycontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference

("myblob");

// Create or overwrite the "myblob" blob with contents from a local 

file.
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);

Deleting blob:-

/ Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting

("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference

("mycontainer");

// Retrieve reference to a blob named "myblob.txt".
CloudBlockBlob blockBlob = container.GetBlockBlobReference

("myblob.txt");

// Delete the blob.
blockBlob.Delete(); 

Posted on Wednesday, August 6, 2014 5:37 AM | Back to top


Comments on this post: How to create blob storage in azure

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Deeksha Sharma | Powered by: GeeksWithBlogs.net