How to upload File to azure BLOB

Tarun Kumar Chatterjee
 
Net – Technology Specialist
May 12, 2018
 
Rate this article
 
Views
2545

I had a requirement to upload a file to BLOB. Below is the piece of code I had to write.

Here are the steps I had to follow:

Create a web application

Install Windows Azure Storage from nuget library by using the below command:

PM> Install-Package WindowsAzure.Storage

It will resolve the dependencies and we can see that below dll references will be added to the solution

· Microsoft.Azure.KeyVault.Core

· Microsoft.Data.Edm

· Microsoft.Data.OData

· Microsoft.Data.Services.Client

· Microsoft.WindowsAzure.Configuration

· Microsoft.WindowsAzure.Storage

· System.Spatial

Create a BLOBManager class will be responsible to upload the file stream to BLOB.

using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Blob;

 using Microsoft.WindowsAzure.Storage;
 using Microsoft.WindowsAzure.Storage.Blob;
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
 namespace UploadFileToBLOB
 {
     public class BlobManager
     {
         public static Uri StrorageUri { get; set; }
         private static CloudBlobContainer GetContainerRef(string containerName)
         {
             var storageAccount = CloudStorageAccount.Parse(Properties.Resources.BlobStorageConnection);
 
             var blobClient = storageAccount.CreateCloudBlobClient();
 
             var container = blobClient.GetContainerReference(containerName);
 
             container.CreateIfNotExists();
 
             container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
 
             return container;
         }
 
         public static void Upload(string containerName, string blobName, string blobType, Stream stream)
         {
             var container = GetContainerRef(containerName);
 
             if (blobType.ToUpper().Equals("BLOCK"))
             {
                 var blockBlob = container.GetBlockBlobReference(blobName);
                 blockBlob.UploadFromStream(stream);
             }
             else if (blobType.ToUpper().Equals("APPEND"))
             {
                 var appendBlob = container.GetAppendBlobReference(blobName);
                 appendBlob.UploadFromStream(stream);
             }
             else if (blobType.ToUpper().Equals("PAGE"))
             {
                 var pageBlob = container.GetPageBlobReference(blobName);
                 pageBlob.UploadFromStream(stream);
             }
         }
     }
 }
 
 

In the BLOBManager Class we are fetching the value of Properties.Resources.BlobStorageConnection from resource file.

The format of the BLOB connection string will be something like this:

DefaultEndpointsProtocol=https;AccountName=<<BLOB Name>>;AccountKey=<< PRIMARY ACCESS KEY >>

BLOB storage can be created from: https://manage.windowsazure.com.

In below snapshot whatever we will be giving in the URL will be the BLOB name.

clip_image002

After BLOB created successfully, Click on MANAGE ACCESS KEY, it will give you the STORAGE ACCOUNT NAME and the PRIMARY ACCESS KEY. PRIMARY ACCESS KEY will be nothing but the AccountKey in BLOB connection string.

After BLOB created successfully we need to create Container where to store the file.

clip_image004

To upload the file we need to write the below piece of code

 using (var fStream = File.OpenRead(filePath))
  {
       BlobManager.Upload("blobcontainername", fileName.ToString(),"BLOCK", fStream);
  }
 
 

Hope it will give the basic idea about to create and upload file in Azure BLOB. Let me come up with more new future of Azure and the implementation in my future articles.

Happy Coding

Tarun Kumar Chatterjee

Author Info

Tarun Kumar Chatterjee
 
Net – Technology Specialist
 
Rate this article
 
Tarun has been working in IT Industry for over 12+ years. He holds a B-tech degree. He is passionate about learning and sharing the tricks and tips in Azure, .Net ...read more
 

Leave a comment