Upload Image To Azure Blob Storage In ASP.NET: Part 2

Before reading this article, please go through the following article:

In this article, I’ll tell you the uses of Azure Blob Storage and how to use blob storage in ASP.NET Project. Firstly, create an ASP.NET project in Visual Studio, so open Visual Studio and follow these steps:

Step 1: Open Visual Studio and select the File menu, New, then Project.

new

Step 2: Go to Templates, Visual C#, then ASP.NET Web Application.

Step 3: In the name section, give a name to project. Here I have given the name “Azure Blob Storage”.

Step 4: Click on “OK” button to create a project.

project

In the following figure, choose "Empty" project.

Click “OK” button.

button

We can see, in our project, there are three types of categories items, properties, References, and Web.config.

refrences

Now we need to install the Windows Azure Storage package for using Azure Storage Access and all supported classes.

Now go to the “TOOLS” menu, select “NuGet Package Manager”, and select “Manage NuGet Packages for Solution”.

NuGet

In the following figure go to the search box and type “azure”. After this you will find the “Windows Azure Storage”, click on “Install” button.

Windows Azure Storage

Now in the following figure, you can see the installation of the “Windows Azure Storage”.

installation

Here in the following window,you will be asked to install “Windows Azure Storage” for the current project, so here we want to install the packages for current project.

Simply click on “OK” button.

Windows Azure Storage

The installing process is going on.

process

process

Here in the following figure, click on “I Accept” button.

Acceptance

Now we successfully installed the “Windows Azure Storage” (in the small red circle).

Click on “Close” button.

Close

Here we also need to install the azure storage client packages. So go to the search box, type the “Azure Storage Client”. After typing, click on “Install” button.

Azure Storage Client

We successfully installed the “Azure Storage Client” package also.

Click on “Close” button.

Close

Go to your project, here my project name is “Azure Blob Storage”.

Right click on to project, select “Add” and select “New Item”.

Add

Here, I am going to create a Web Form, so select a Web Form in the list and give the name to your Web Form; here I gave the name “MyForm” to my Web Form, you can give any name to your Web Form.

Click on “Add” button.

Add

After creating a form, you can see your project, where form will be created successfully with the name “MyForm.aspx

MyForm

In the “MyForm.aspx.cs” page, let me do some changes.

MyForm

In the “MyForm.aspx.cs” page, I have added all namespaces which is minimum required.

MyForm

Now we need “accountname” and “accesskey” to get connected with Azure Account, so go to Azure portal.

Go to “Storage” option where I have created a Storage Account with the name “nitindemostorage” in my previous article. You can visit my previous article of azure.

Here, after clicking “nitindemostroage” named Storage Account, go to bottom of the window and select “Manage Access Keys”.

Access

You will find the following window, copy the “Storage Account Name” and “Primary Access Key” from here,

Key

Go back in your project and make some changes over there.

In the “accountname” and “accesskey” paste the copied item over there.

And make some other code over there for getting connections with Azure storage account with “CloudStorageAccount” class object and at last convert your image into stream upload as in the following image:

CloudStorageAccount

Code

  1. string accountname = "nitindemostorage";  
  2.   
  3. string accesskey = "8TeQ8Li/b5U75aSPeTUvmg6w6s6emWSC5KKksNR7hRmSS5GmxzY8wxXFxJtNlwnwSbvlWGAX07dnR0W8jrb+Og==";  
  4.   
  5. try  
  6.   
  7. {  
  8.   
  9.     StorageCredentialscreden = newStorageCredentials(accountname, accesskey);  
  10.   
  11.     CloudStorageAccountacc = newCloudStorageAccount(creden, useHttps: true);  
  12.   
  13.     CloudBlobClient client = acc.CreateCloudBlobClient();  
  14.   
  15.     CloudBlobContainercont = client.GetContainerReference("mysample");  
  16.   
  17.     cont.CreateIfNotExists();  
  18.   
  19.     cont.SetPermissions(newBlobContainerPermissions  
  20.   
  21.         {  
  22.   
  23.             PublicAccess = BlobContainerPublicAccessType.Blob  
  24.   
  25.         });  
  26.   
  27.     CloudBlockBlobcblob = cont.GetBlockBlobReference("Sampleblob.jpg");  
  28.   
  29.     using(Stream file = System.IO.File.OpenRead(@ "D:\amit\Nitin sir\Nitinpandit.jpg"))  
  30.   
  31.     {  
  32.   
  33.         cblob.UploadFromStream(file);  
  34.   
  35.     }  
  36.   
  37. catch (Exception ex)  
  38.   
  39. {  
  40.   
  41. }  

One more thing, here we need to create a Web App Profile for publishing our app into the cloud.

Simply go to “WEB APPS” in your Azure Portal and click on “New” to add a new Web App.

Web App

After clicking on “New”, you will see the following window.

Go to “COMPUTE” option, select “WEB APP”, and select “QUICK CREATE".

Create

In the URL section, you can write your Web App name. Here I have given “nitinblobstoragedemo”, if the entered name is correct it will create a check sign (in blue color).

In the “APP SERVICE PLAN” click on “Default1 (East Asia, Shared)”.

Click on “CREATE WEB APP”.

Create WEB APP

We have created our Web App successfully. After doing this, we can see our created Web APP(“nitinblobstoragedemo”).

And it is in running mode.

nitinblobstoragedemo

We make some code in the Web Config file also.

Here in the <add value=”MyForm.aspx”/>, “My Form” is my Web form.

Form

In the “MyForm.aspx” you can give any message for output. Whenever you build your project, the typed message will be your output.

So, here in the div section I have given a message, “Creating Blob---By Nitin Pandit”.

blob

Now we need to publish our created project in the cloud. It will also save in the Azure Explorer. You can publish your project by two ways. Here, I am going to discuss both ways. Firstly, right click on your project. Here my project is “AzureBlobStorage”, so select the “Publish” option.

publish

In the following figure, you can see “nitinblobstoragedemo” is my Web App name, which we have created in the starting. Select your Web APP profile and click "Publish” button.

publish

After clicking on “publish” button the project would publish on cloud as well as in your Azure Explorer. The second way to publish your project is to go for cloud. You can download your profile from the Azure account in your Web Apps. For this click on your Web App profile, in the Publish your app section, click on “Download the publish profile”.

profile

After clicking, your profile will be downloaded in your system.

system

After this, now we can see the status of our publishing. So my “nitinblobstoragedemo” app service published successfully.

Now build your project and get your output.

output

Now run the application, press F5.

So, finally we got our output.

output

We can see the changes of this in our Azure Explorer.

Now refresh the Azure Explorer to check your image.

image

“Sampleblob.jpg” is my image which is generated by programming,

Sampleblob

Right click on image and open it and if you want to get that images by code on your view, you can see in my next article. So, wait for that and stay tuned.

click

It can take few moments to download the image is in progress.

progress

Here's the final output and you can see my image:

output

Thanks for reading this article and wait for next,

Connect(“Nitin Pandit”);

Up Next
    Ebook Download
    View all
    Learn
    View all