Azure Blob Storage - Upload and Download files in .NET C#

 In this article, we will see how to upload and download file in the azure blob storage using c#. This code can be used in MVC5 as well as Dot net core. But for demo purpose, I am using Console Application.

Prerequisites:

1. Visual Studio 2019

2. Azure Account

Let's perform some basic operation for azure blob storage :-

    1. Create an azure storage account and blob container

    2. Get connection string (Access key) 

    3. Create Console Application

    4. Add NuGet Package (Azure.Storage.Blobs)

    5. Upload file in azure blob storage.

    6. Download file from azure blob storage.

Let's go one by one.

1. Create an Azure storage account and blob storage-

  Go to azure portal and search Storage Account under the Azure services.

 

Click on Storage Account. It's go to Storage Account window. On this window click on Create

Button.

Once click on create button, we will move on the next window where we have the some option for our storage account. We have to select as per our requirement.

First we have to choose our subscription if we have otherwise left it as Free trial. Then  choose resource group if already created otherwise we can create new resource group. For this demo, creating new resource group from this window. For this, just click on Create New and create Resource Group.


 

After that, going to enter storage account name as 'mydemoblobs' but you can enter any name whatever you want. Next Select the location (Region) whichever is right for you but for demo, I am using (US) East US. Left all the options as it is and Click on Review + Create Button.

After clicking on the Review + create here we can see the validation passed status and along with the review page with all the options which we had opted for and then click on create a button at the bottom to create a storage account. Once this process completed, Your Blob Storage Account is created now.

Create a blob container under the storage account

Purpose of blob container: - To store the actual documents and files, we need a blob container under the same storage account. 

Let's create a new blob container under this storage account. Open the new created Storage account 'mydemoblobs' or just click on Go to resource button on the above screen. On Storage Account window, under the left side options, you will find the name Container, Click on that where it gives the option to create a new container. 

Enter the container name as you want but here I am giving the container name as "mydemocontainer".  Select the public access level as per your requirement but for demo I am choosing the option as "Container (anonymous read access for containers and blobs)"  because anonymous user can read container and its blob data for demo purpose. Please follow these steps for creating container.



  2. Get connection string (Access key)

After creation of blob container, we can use this container for uploading/downloading files. We can also use this from code ( c#). To incorporate the Azure Storage account in the project, we need the get the Access Keys to communicate our project with azure storage account.

Access keys: - Created default when we setup the storage account.

For getting access key, go to your blob storage account which is newly created and follow the below steps.

From this screen, copying the highlighted connection string for implement blob storage in our c# application.

3. Create Console Application

Open the visual studio. Choose the project as Console application. Give the proper name and choose the location for the project and create the project. 

4. Add Nuget Package (Azure.Storage.Blobs)

Once the project is created, Right click on your Project Solution and click on Manage NuGet Packages.. Search for Azure.Storage.Blobs NuGet package. Select the mention NuGet Package and Install. 

Please follow the step below.



5. Upload file in azure blob storage.

Now, We have configured the required package for uploading/downloading the files in/from azure storage account. 

Go to Progam.cs class of your Console application.

using Azure.Storage.Blobs;

using System;

namespace myblobstoragedemo

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("File Processing!");

            string connectionstring ="PUT YOUR CONNECTION STRING HERE";

            //Upload File in Azure blob

            string pathfile = @"E:\BlobStorage\myimage.jpg"; //Location in your system

            BlobClient objblob = new BlobClient(connectionString: connectionstring, blobContainerName: "mydemocontainer", blobName: "myimage" + DateTime.Now.Ticks + ".jpg");

            objblob.Upload(pathfile);

        }

    }

}

Note: For  connectionstring, use your blob storage connection string which is discussed above. Go to storage account and click on access key and copy connection string, use that connection string here.

Path File: From where, you want to upload the file/images in azure blob storage. It is in your local system.

Once you run the code, Files/Images is uploaded on azure blob storage. 

To see this file, Go to your blob storage account, click on your container where you see the your file uploaded.


 6. Download file from azure blob storage.

using Azure.Storage.Blobs;

using System;

namespace myblobstoragedemo

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("File Processing!");

            string connectionstring = "PUT YOU COPYING CONNECTION STRING WHICH IS MENTION ABOVE";

            string containerName = "mydemocontainer";

            BlobContainerClient bc = new BlobContainerClient(connectionString: connectionstring, blobContainerName: containerName);

            BlobClient objd = bc.GetBlobClient("downloadimage.jpg");

            objd.DownloadTo(@"E:\BlobStorage\downloadimagefromazure.jpg");

        }

    }

}

After running the code, File is downloaded in your given path. Just go to that path and check, It is downloaded.


Thank you for reading, please let me know your questions, thoughts, or feedback in the comments section. I appreciate your feedback and encouragement.

keep learning....!

Comments

Post a Comment