Azure Blob Storage Using DevelopmentStorageAccount

Microsoft provides Azure Storage Explorer and Azure Storage Emulator tools for developers who want to test and use storage accounts and services without getting a paid Azure subscription. In this article, I will discuss how to use Azure Blob Storage using the Development Storage Account, which is free.

Installation Process

Download Microsoft Azure Storage Explorer from below URL:

https://azure.microsoft.com/en-in/features/storage-explorer/

Download Microsoft Azure Storage Emulator - v5.8 (please download v5.8 version only)

https://azure.microsoft.com/en-in/downloads/

Note: Microsoft Azure Storage Explorer and Microsoft Azure Storage Emulator both should be active or running.

Start Explorer and Emulator

First, run your Explorer and Emulator.

See below. This will be your explorer.

Step 1.

On click of Storage Accounts => Click On Emulator - Default Ports => Then you will get Blob Containers.

How To Create Containers

Right, Click on Blob Containers option and select Create a Blob Container.

Name your container (name should be lowercase. Look at the below image. Here, I have created a container named as documents.

 

Step 2.

Now, we have a container that can be used to store blog content.

Now, let's write some code to upload files to it.

For Programming What are all the things required?

The below namespace is required. Run below NuGet command in your solution. 

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

Click on Tools => Hover On => NuGet Package Manager and select package manager console.

Execute the below command. 

Install-Package WindowsAzure.Storage -Version 9.3.3  

Please check the below screen. This is how it should look like.

 

This will be the default credentials that are being stored in the web config file.

DefaultEndpointsProtocol=http;
AccountName=devstoreaccount1;
AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==

Please add the below key in web config in <ConnectionStrings> section.

<connectionStrings>  
<add name="azurestorageconfigurationkey" connectionString="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==" providerName="System.Data.SqlClient"/>  
</connectionStrings>

Please Create one folder in the Visual Studio, name it images and save one image file in it.

Please use the below code. 

string path = HttpContext.Current.Server.MapPath("~/Images/img.jpg");  
Image img = Image.FromFile(path);  
//ImageConverter Class convert Image object to Byte array.  
byte[] bytes = (byte[])(new ImageConverter()).ConvertTo(img, typeof(byte[]));  
Stream filestream = new MemoryStream(bytes); // converting image file to filestream.  

In the below code, we are pushing our file to "documents" container by creating the "userimages" directory. 

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
CloudBlobContainer blobContaiber = blobClient.GetContainerReference("documnets");  
if (blobContaiber.Exists())  
{  
// creating directory under container and saving a file  
CloudBlobDirectory userimages = blobContaiber.GetDirectoryReference(year.ToString());  
CloudBlockBlob cloudBlob = userimages.GetBlockBlobReference("img.jpg");  
cloudBlob.UploadFromStream(filestream);  
string filelocation = cloudBlob.Uri.ToString();  
} 

Please observe the below screen for output, In figure one, I've marked folder name, which we've created in the code. 

Please observe the second screen, In this figure, I've marked the container, folder, and image. using a download option. You can download to your local machine also.

 

Here is the complete code:

using Microsoft.WindowsAzure.Storage;  
using Microsoft.WindowsAzure.Storage.Blob;  
using System;  
using System.Configuration;  
using System.Drawing;  
using System.IO;  
   
   
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;  
string connectionString = ConfigurationManager.ConnectionStrings["azurestorageconfigurationkey"].ToString();  
// code for file upload  
string path = HttpContext.Current.Server.MapPath("~/Images/img.jpg");  
Image img = Image.FromFile(path);  
//ImageConverter Class convert Image object to Byte array.  
byte[] bytes = (byte[])(new ImageConverter()).ConvertTo(img, typeof(byte[]));  
Stream filestream = new MemoryStream(bytes);  
try  
{  
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
CloudBlobContainer blobContaiber = blobClient.GetContainerReference("documnets");  
if (blobContaiber.Exists())  
{  
// creating directory under container and saving a file  
CloudBlobDirectory userimages = blobContaiber.GetDirectoryReference("UserImage");  
CloudBlockBlob cloudBlob = userimages.GetBlockBlobReference("img.jpg");  
cloudBlob.UploadFromStream(filestream);  
string filelocation = cloudBlob.Uri.ToString();  
}  
}  
catch (Exception e)  
{  
}

I hope you found it useful. Eat-> Code->Sleep->Repeat.