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.
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.
In the following figure, choose "Empty" project.
Click “OK” button.
We can see, in our project, there are three types of categories items, properties, References, and Web.config.
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”.
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.
Now in the following figure, you can see the installation of the “Windows Azure Storage”.
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.
The installing process is going on.
Here in the following figure, click on “I Accept” button.
Now we successfully installed the “Windows Azure Storage” (in the small red circle).
Click on “Close” button.
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.
We successfully installed the “Azure Storage Client” package also.
Click on “Close” button.
Go to your project, here my project name is “Azure Blob Storage”.
Right click on to project, select “Add” and select “New Item”.
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.
After creating a form, you can see your project, where form will be created successfully with the name “MyForm.aspx”
In the “MyForm.aspx.cs” page, let me do some changes.
In the “MyForm.aspx.cs” page, I have added all namespaces which is minimum required.
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”.
You will find the following window, copy the “Storage Account Name” and “Primary Access Key” from here,
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:
Code
- string accountname = "nitindemostorage";
-
- string accesskey = "8TeQ8Li/b5U75aSPeTUvmg6w6s6emWSC5KKksNR7hRmSS5GmxzY8wxXFxJtNlwnwSbvlWGAX07dnR0W8jrb+Og==";
-
- try
-
- {
-
- StorageCredentialscreden = newStorageCredentials(accountname, accesskey);
-
- CloudStorageAccountacc = newCloudStorageAccount(creden, useHttps: true);
-
- CloudBlobClient client = acc.CreateCloudBlobClient();
-
- CloudBlobContainercont = client.GetContainerReference("mysample");
-
- cont.CreateIfNotExists();
-
- cont.SetPermissions(newBlobContainerPermissions
-
- {
-
- PublicAccess = BlobContainerPublicAccessType.Blob
-
- });
-
- CloudBlockBlobcblob = cont.GetBlockBlobReference("Sampleblob.jpg");
-
- using(Stream file = System.IO.File.OpenRead(@ "D:\amit\Nitin sir\Nitinpandit.jpg"))
-
- {
-
- cblob.UploadFromStream(file);
-
- }
-
- } catch (Exception ex)
-
- {
-
- }
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.
After clicking on “New”, you will see the following window.
Go to “COMPUTE” option, select “WEB APP”, and select “QUICK 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”.
We have created our Web App successfully. After doing this, we can see our created Web APP(“nitinblobstoragedemo”).
And it is in running mode.
We make some code in the Web Config file also.
Here in the <add value=”MyForm.aspx”/>, “My Form” is my Web 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”.
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.
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.
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”.
After clicking, your profile will be downloaded in your 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.
Now run the application, press F5.
So, finally we got our output.
We can see the changes of this in our Azure Explorer.
Now refresh the Azure Explorer to check your image.
“Sampleblob.jpg” is my image which is generated by programming,
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.
It can take few moments to download the image is in progress.
Here's the final output and you can see my image:
Thanks for reading this article and wait for next,
Connect(“Nitin Pandit”);