Click here to Skip to main content
15,867,308 members
Articles / Hosted Services / Storage

Azure Storage : Azure Blob Storage for Developers

Rate me:
Please Sign up or sign in to vote.
2.89/5 (4 votes)
9 Sep 2018CPOL3 min read 20.3K   5   10
This article briefs you about the Azure blob storage and how to use it in less than 10 steps.

What is Azure Storage?

Azure storage is a cloud storage offering from Azure, which provides storage of binary, text, structured and unstructured data, messages, etc. which is exposed via REST API so that you can access anywhere.

Abstraction or Types/Options of Azure Storage

  • Blob
  • Queue
  • Table
  • File Storage

Blob - Unstructured data like images, documents, audio files, video files, etc.

Queue - Used for sending and receiving messages just like publisher and subscriber

Table - Schema less and no relationship but a structured data (no SQL Database storage) stored in the form of tables

File storage - Mapping a storage network drive from Azure to VMs created in Azure, similar to our shared network drive

Let's Talk about Azure Blob Storage in this Article

What is Azure Blob Storage?

Blob storage is a service (which falls under Azure storage) which stores unstructured data in cloud as objects and serves them directly to browser.

Common uses of blob storage include:

  • Storing data for analysis
  • To perform secure back up
  • Streaming video and audio
  • Storing files for distributed access

Different Types of Blobs

Page Blob - Generally used to store VHD files whose limit is upto 1TB
Block Blob - Generally used to store text and binary data files whose limit is upto 200GB

Image 1

How to Create a Blob Storage?

Step 1

Go to your Azure portal and create a storage account.

Image 2

Name: Name of your storage account (Name should be unique and small characters)

Deployment model: Resource manger - new portal, Classic - Old portal

Account Kind: General purpose (which includes Tables, Blobs, Queues and File storage) or Blob Storage Designed to store only blobs.

Note: Just hover on the small icon "i" you will get to know what are the categories.

Once Storage Account is created, it will look like this:

Image 3

Step 2

Now click on Blobs and click on add container, give the name of container and the access levels and click create. Once created, it will look like this:

Image 4

Step 3

Now click on the storage account and go to settings under it and click on keys. You can see 2 keys and their respective connection strings, copy one of those. We will be using them in code.

Image 5

Now let's do our coding part and store a blob from the our code, retrieve it and check its URI. So start creating it with me.

Step 4

Go to Visual Studio and create an empty console application.

Go to App.config and add this under <appSettings>.

XML
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;
AccountName=commonfilestorage;
AccountKey=T1y7Rb1R/QJ/3NE6oSO94wRkQZcJjl8LP5UgoYuJwBJzMbN1EOZfuptwyD3L2CBe2rqi/IyjBYxxAqc/XOsy4w==;
EndpointSuffix=core.windows.net" />

Note: The connection string given here is the one which got generated when i created a storage account so please use your connection string, this storage account will be deleted and no longer available by the time you read the article.

Step 5

Right click on project and click on Manage nuget packages and browse WindowsAzure.Storage and add it to the project.

Step 6

Open Program.cs and add these namespaces at the beginning:

C#
using Microsoft.WindowsAzure; // Namespace for CloudConfigurationManager
using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount
using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage types
using Microsoft.Azure;

Step 7

Now let's write some code to add blobs to our container.

C#
// Retrieve storage account from connection string.
   CloudStorageAccount storageAccount =
     CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

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

// Retrieve a reference to a container.
   CloudBlobContainer container = blobClient.GetContainerReference("doccontainer");

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

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

// Retrieve reference to a blob named "myblob".
  CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob.doc");

//Create or overwrite the "myblob" blob with contents from a local file.
  using (var fileStream = System.IO.File.OpenRead
             (@"C:\Users\sanath.js\Documents\Abhinandana S_Resume.docx"))
  {

     Console.WriteLine
     ("******************************* Uploading Blob **********************************************");
     blockBlob.UploadFromStream(fileStream);
     Console.WriteLine
     ("******************************* Successfully Uploaded Blob
       **********************************************");
     Console.ReadLine();
}

Note: Replace the blob you want to upload in this line.

C#
using (var fileStream = System.IO.File.OpenRead
    (@"C:\Users\sanath.js\Documents\Abhinandana S_Resume.docx"))

That's it! Run the application, now you will be able to upload the blob to cloud, try with some images, txt files as well. Now go to your container in Azure portal, you will be able to see your blob there.

Image 6

Step 8

To download a blob from container, add the following code:

C#
// Save blob contents to a file.
   using (var fileStream = System.IO.File.OpenWrite(@"C:\Users\sanath.js\Desktop\check.docx"))
   {
      Console.WriteLine("******************************* Downloading Blob
                         **********************************************");
      blockBlob.DownloadToStream(fileStream);
      Console.WriteLine("******************************* Downloaded Blob
                         **********************************************");
      Console.ReadLine();
   }

When you run the application, you will be able to see a check.docx file in your desktop which is the blob which we uploaded earlier.

Step 9

To see the URI of Blob, use this code:

C#
//list blobs in container
  foreach (IListBlobItem blob in container.ListBlobs())
  {
      if (blob.GetType() == typeof(CloudBlockBlob))
          Console.WriteLine(blob.Uri);
      Console.ReadLine();
  }

Image 7

You can clearly see our storage account and container name in the URI when you copy the link and hit that from browser, your blob will be downloaded.

That's it! Now you are familiar to start using Azure blob storage.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Software Engineer in Euromonitor Research & Consulting|Bengaluru
I Work on microsoft technologies(C#,Asp.net,MVC,Azure),Angular.js,Angular 2,Node.js,Mongodb etc..

I believe every person is capable of learning a new technology but just he/she needs a kick-start to start with the learning of any technology so most of my articles will be targeting to help begineers.

Comments and Discussions

 
QuestionPdf And Doc Files Pin
Mohsin Azam3-Sep-18 2:51
Mohsin Azam3-Sep-18 2:51 
AnswerRe: Pdf And Doc Files Pin
sanathjs6-Sep-18 18:45
professionalsanathjs6-Sep-18 18:45 
Question2 other comments Pin
Sacha Barber3-Sep-18 2:11
Sacha Barber3-Sep-18 2:11 
AnswerRe: 2 other comments Pin
sanathjs6-Sep-18 18:43
professionalsanathjs6-Sep-18 18:43 
Thanks those are nice topics.
QuestionYou must be quite brave Pin
Sacha Barber3-Sep-18 2:07
Sacha Barber3-Sep-18 2:07 
AnswerRe: You must be quite brave Pin
sanathjs6-Sep-18 18:37
professionalsanathjs6-Sep-18 18:37 
SuggestionI hope you changed those keys... Pin
dandy723-Sep-18 2:07
dandy723-Sep-18 2:07 
GeneralRe: I hope you changed those keys... Pin
Sacha Barber3-Sep-18 2:08
Sacha Barber3-Sep-18 2:08 
GeneralRe: I hope you changed those keys... Pin
sanathjs6-Sep-18 18:31
professionalsanathjs6-Sep-18 18:31 
GeneralRe: I hope you changed those keys... Pin
dandy727-Sep-18 3:34
dandy727-Sep-18 3:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.