How To Upload File To Azure Blob Storage

In this comprehensive article, I will walk you through the major methods for uploading files to Azure Blob Storage, from simple drag-and-drop to high-performance command-line tools.

How To Upload File To Azure Blob Storage

Method 1: Using Azure Portal

Follow the steps below.

1. The first step is to create an Azure storage account and a container in the Azure Portal. Follow the article on How to Create Azure Blob storage to create the Azure storage account and the Blob container.

2. Now, navigate to the newly created container you created above, or use your existing container in the Azure portal.

how to upload file in azure blob storage

3. Then, click on the name of the container, and the container page will open. Click on the Upload button there.

how to upload file to azure blob storage

4. On the Upload blob window, click on the Browse button, select the file to be uploaded from the local path of your system, and then click on the Upload button.

how to upload a file to azure blob storage

Now, it will show you a message that the upload of upload.txt is complete.

how to upload file to blob storage azure

You can see below that the file has been uploaded successfully to my Azure container.

how to upload file in blob storage azure

Method 2: Using C#

Before we start development activities, we need a storage account and must create a container in the Azure Portal.

If you do not have the storage account and container created, refer to my article How to Create Azure Blob Storage to create both.

Note: An Azure storage account and the container to be created in the Azure portal is Must before working on the upload and download files from the Blob storage.

You now have the Azure storage account and container created in the Azure Portal. Now we will create a console application in C# using Visual Studio 2019 for the following functionality.

  • Upload a file to Azure blob storage using C#
  • Download a file from the Azure blob storage using C#

1. The first step is to create a console application using Visual Studio 2019. To do that, click on File –> New –> Choose Console App (.NET Framework) from the Create a new Project window and click the Next button.

upload file azure blob storage c#

2. Enter the Project name, select the location, and click the Create button to create the console application.

how to upload file to blob storage azure c#

3. The next step is installing the NuGet package to access Azure Storage.

Go to Tools –> NuGet Package Manager –> Manage NuGet Packages for Solution.

how to upload a file to azure blob storage C#

4. Search for Azure storage and select the Azure.Storage.Blobs. Select the Project name option, then click the Install button.

how to upload file to azure blob storage C#

Click on the I Accept button on the License Acceptance window.

how to upload a file to blob storage azure C#

It will install the NuGet package for you now. Use the below namespaces in your Program.cs file

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

You can see below.

upload file to azure blob storage from browser

There might be a chance you will get the error. The type or namespace name ‘Azure’ does not exist in the namespace ‘Microsoft’ (are you missing an assembly reference?). To fix this issue, add the two libraries listed below as references to the project.

how to upload file in azure blob storage using c#

Let’s create two methods in the Main() method of the Program.cs file i.e AzureFileUpload() and AzureFileDownload()

  • AzureFileUpload() – Use to upload files to the Azure blob storage.
  • AzureFileDownload() – Use to download the file from the Azure blob storage.
 static void Main(string[] args)
        {
            AzureDemo azureDemo = new AzureDemo();

            azureDemo.AzureFileUpload("D:\\Upload.txt", "democontainer");
            azureDemo.AzureFileDownload("Upload.txt", "democontainer");

            Console.ReadKey();
        }
upload file to blob storage azure c#

I have created a class named AzureDemo with method definitions.

There are two parameters for the AzureFileUpload() method: The file name to be uploaded and the Container name.

In the AzureFileUpload() and AzureFileDownload () methods, we need to use the Azure storage connection string. So, we need to copy that from the Azure Portal.

To copy that, navigate to the storage account you will use, click Access keys, then copy the connection string as highlighted below, and keep it in a notepad. Later on in the code, you can use it.

upload file to azure blob storage c#

Below is the code for the AzureFileUpload().

 public void AzureFileUpload(string fileName, string containerName)
        {
            string myFile,
            myFileName,
            myConnectionString;
            Stream file;

            //Copy and paste the connection string from Azure portal     
            myConnectionString = "DefaultEndpointsProtocol=https;AccountName=sqlvaqrc7kssdx4eus;AccountKey=Coro2SvGOTD+nT3YdaKCrnauz########";

             
            file = new FileStream(fileName, FileMode.Open);

            CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(myConnectionString);
            CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);

            // Check for the container if it exists or not  
            if (container.CreateIfNotExists())
            {

                container.SetPermissionsAsync(new BlobContainerPermissions
                {
                    PublicAccess =
                  BlobContainerPublicAccessType.Blob
                });

            }


            myFile = Path.GetExtension(fileName);
            myFileName = Path.GetFileName(fileName);

            CloudBlockBlob myBlockBlob = container.GetBlockBlobReference(myFileName);
            myBlockBlob.Properties.ContentType = myFile;

            // Upload the file to the blob storage 
            myBlockBlob.UploadFromStreamAsync(file);  

            Console.WriteLine("Upload Completed Successfully!!!!");
        }
how to upload a file to azure blob storage using C#

You have copied the Azure storage connection string following the above steps to a notepad. Now is the time to copy and paste it into the line of code below in the AzureFileUpload() method.

//Copy and paste the connection string from the Azure portal
myConnectionString = “Provide the connection string of your storage account”;

FileStream() is used to read the file from the local path of your computer.

file = new FileStream(fileName, FileMode.Open);

The code below creates an instance of the Storage account.

CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(myConnectionString);

Similarly, the code below creates an instance of the Blob Client.

CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();

This line of code creates an instance of the Blob Container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);

The code below checks whether an existing container exists; if not, it creates a new container in the storage account with the specified name.

 // Check for the container if it exists or not  
            if (container.CreateIfNotExists())
            {

                container.SetPermissionsAsync(new BlobContainerPermissions
                {
                    PublicAccess =
                  BlobContainerPublicAccessType.Blob
                });

            }

If you run the code, you can see it is uploaded successfully to the demo container.

how to upload file in azure blob storage using c#

Method 3: Using PowerShell

Sign in to Azure

The first thing is we need to log in to Azure with the below cmdlet

PS C:\WINDOWS\system32> Login-AzureRmAccount

Or, you can also use the below PowerShell cmdlet to connect to Azure

PS C:\WINDOWS\system32> Connect-AzAccount

The next step is to get the storage account keys from the Azure portal and assign them to a variable with the below cmdlet

$Keys = "Coro2SvGOTD+nT3YdaKCrnauzWvHMF6BXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=="

To get the above key from the Azure portal,

Log in to the Azure portal, navigate to the storage account you will use, click on the Access keys, and then copy the Keys as highlighted below.

How to upload file in azure blob storage using PowerShell

Now, use the below PowerShell cmdlet, i.e., the Context, to interact with the Azure storage account with the above AccessKey.

$StorageContext = New-AzureStorageContext -StorageAccountName sqlvaqrc7kssdx4eus -StorageAccountKey $Keys

The below cmdlet is to set the local file path, container name, and Blob along with the Storage context.

Set-AzStorageBlobContent -File "D:\Bijay\Upload9.txt" -Container $containerName -Blob "Upload9.txt"  -Context $StorageContext

Now, the Complete PowerShell script is as follows.

Login-AzureRmAccount
$Keys = "Coro2SvGOTD+nT3YdaKCrnauzWvHMF6BmuD9PfSKtNJWt6etOMjkwg6KNSQ39zOOQcMpDSjyBKHdALGIg6gZ4Q=="
$StorageContext = New-AzureStorageContext -StorageAccountName sqlvaqrc7kssdx4eus -StorageAccountKey $Keys
$containerName = "democontainer";
Set-AzStorageBlobContent -File "D:\Bijay\Upload9.txt" `
  -Container $containerName `
  -Blob "Upload9.txt" `
  -Context $StorageContext 
how to upload file in azure blob storage powershell

It will ask you to enter the Azure credentials. Enter your credentials, and click the sign-in button as shown in the screenshot below.

how to upload file to azure blob storage using C#

Then, it will execute successfully and upload the file to the Azure container. Check out the screenshot below for your reference.

how to upload file in azure blob storage using powershell

To check that, navigate to the container in the Azure portal and see the file uploaded successfully.

azure blob storage download file

Method 4: Using AzCopy

AzCopy is an excellent command-line tool that helps us transfer data to and from Azure storage. That also helps to schedule different categories of Backups in Azure storage. It can also help you copy data to or from Azure Stack. You can find more information about AzCopy now.

  • Open the command prompt, navigate to the folder where you have downloaded the AzCopy, and then run the command below to log in.
azcopy login
Upload File to Azure Blob Storage AzCopy
  • Now, as mentioned above, navigate to https://login.microsoftonline.com/common/oauth2/deviceauth, enter the code mentioned on the above screen in the pop-up below, and then click on the Next button.
upload file to azure file share c#
  • Once you log in successfully, you will see “Login succeeded.” Now run the command below to create a container in the respective storage account where we will upload the files.
azcopy make “https://<your azure storage accountname>.blob.core.windows.net/<name of your container>”

Example

azcopy make "https://Demoaccount.blob.core.windows.net/test1"

Once you execute the above command, it will display a message that says “Successfully created the resource.”

  • The next step is to execute the command below to copy a file from your local machine to your Storage account.
azcopy copy <Your file location in your local path> “https://<your storage account name>.core.windows.net/< your container name>/”

Example

azcopy copy 'C:\azcopy\Testing\testing.txt' 'https://Demoaccount.blob.core.windows.net/test1'

Once you run the command above, it will display the message “Final Job status: Completed” in the command prompt.

  • You can execute the command below to copy all the files to your storage account.
azcopy copy "<Your folder location in your local path>" 'https://<your storage account name>.blob.core.windows.net/<container>' --recursive

Example

azcopy copy "C:\azcopy\Testing2\test" "https://Demoaccount.blob.core.windows.net/test1" --recursive

Once you run the command above, it will display the message “Final Job status: Completed” in the command prompt.

Now, if you log in to the Azure Portal and navigate to the Azure Storage account, you can see that the folder has been created successfully inside the container, and all the files are there.

Method 5: Azure Storage Explorer (Best for Power Users)

For IT professionals who handle cloud data daily, I always recommend downloading Azure Storage Explorer. It is a free, standalone app from Microsoft that runs on Windows, macOS, and Linux. It feels exactly like using “File Explorer” on your PC, but for the cloud.

Why use it?

  • Bulk Uploads: It handles hundreds of files much better than a browser.
  • Resume Capability: If your internet blinks, it can often resume where it left off.
  • Security: You can sign in using your Microsoft Entra ID (Azure AD) credentials or a SAS (Shared Access Signature) token.
how to upload a file to azure blob storage using Azure storage explorer

Choosing the Right Upload Method

With so many options, how do you choose? I’ve put together this comparison table to help you decide based on your specific business needs.

RequirementRecommended ToolSkill Level
One-off small fileAzure PortalBeginner
Managing many foldersAzure Storage ExplorerIntermediate
Massive data migration (TB+)AzCopyProfessional
Scheduled nightly scriptsAzure CLI / PowerShellProfessional

Check out: How to download file from Azure Blob Storage

Final Thoughts:

Uploading files to Azure Blob Storage is a fundamental skill for any modern IT professional or developer. Whether you prefer the visual simplicity of the Azure Portal or the raw power of AzCopy, the key is to understand which tool fits the job.

You may like the following Azure tutorials:

Azure Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!