Microsoft Azure Blob Storage – Upload File using C#

Blob storage is one of the storage types in Microsoft Azure. In Azure, blobs are files of different types like text files, images,…etc. In this section, we discuss how to upload a file into Microsoft Storage.

Create the Azure account at https://portal.azure.com/, and it is free for the first 12 months. Login to the Azure portal and select Storage accounts, as shown below. If you don’t find Storage accounts by default on the home screen, search through Search textbox.

 

Click on the Add button to create an Azure storage account as below.

 

Enter all details, including storage account name, to create a new storage account as below. Here we have given storage account name as “azureblobuploadfile”.

 

Once the storage account created, it displays under Storage accounts as below.

Let’s create a C# console application where we upload the file to Azure storage. Open Microsoft Visual Studio 2019 => Create Console Application (.Net Framework).

Install WindowsAzure.Storage package through Nuget, as shown below.

To upload the file to Azure storage, we need to have that specific storage account connection string. To get the storage connection string, go to Azure portal => select Storage Accounts and select the required storage account (here out storage account name is azureblobuploadfile). Then got to storage account settings => Access Keys => copy Connection string as shown below. 

 

Now we write C# code to upload file to Microsoft Azure.

using Microsoft.WindowsAzure.Storage;
using System;
using System.IO;

namespace AzureBlobUploadFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string storageAccntConnection = "your azure storage account connection string";
            string blobFilePath = @"C:\temp\blobTestFile.txt";

            var storageAccnt = CloudStorageAccount.Parse(storageAccntConnection);

            var blobClient = storageAccnt.CreateCloudBlobClient();

            var container = blobClient.GetContainerReference("techblobcontainer");

            container.CreateIfNotExists(); //create techblobcontainer if it's not exists

            var blob = container.GetBlockBlobReference(Path.GetFileName(blobFilePath));

            using (MemoryStream stream=new MemoryStream())
            {
                File.Open(blobFilePath, FileMode.Open, FileAccess.ReadWrite).CopyTo(stream);
                stream.Position = 0;

                blob.UploadFromStream(stream);
            }

            Console.Write("File uploaded to techblobcontainer container");
            Console.ReadLine();
        }
    }
}

As shown above, code access the storage account through connection string. To upload any files, first, we need to create a container where file stores. Here we are creating a container named “techblobcontainer”. Then we are reading blobTestFile.txt file from a local drive through file streams and uploading to Azure.

Run the application, then go to the Azure portal, select azureblobuploadfile storage account. You can find techblobcontainer container as below.

Inside techblobcontainer container,  we can find blobTestFile.txt file, as shown below.

Add comment