How to Upload and Download File From Azure Blob Storage Using C# and PowerShell

In this azure tutorial, we will discuss How to Upload and Download File From Azure Blob Storage Using C# and PowerShell. Apart from this, we will also discuss on below topics

  • How to upload file in Azure blob storage (using Azure Portal)
  • Upload file in Azure blob storage using C#
  • How to upload file in azure blob storage using PowerShell
  • Download a file from Azure Blob storage (using Azure Portal)
  • How to download a file from the Azure blob storage using C#
  • Download a file from azure blob storage using PowerShell

How to upload file in Azure blob storage (using Azure Portal)

Well, we will discuss here how to upload files to azure blob storage using Azure Portal. Follow the below steps to upload the file.

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.

Now, Navigate to the newly created container you have created above or you can also use your existing container in Azure portal

How to upload file in azure blob storage (using Azure Portal)

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

Upload file in azure blob storage (using Azure Portal)

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

How to upload file in azure blob storage using Azure Portal

Now, it will show you the message that Upload completed for the Upload.txt.

Steps to upload file in azure blob storage using Azure Portal

Now, you can see below, the file has been uploaded successfully to my Azure container.

Steps to upload file in azure blob storage (using Azure Portal)

How to Upload and Download File From Azure Blob Storage Using C# and PowerShell

Now, Let’s discuss here Upload and Download File From Azure Blob Storage Using C# and PowerShell. Before we are going to start on the development activities, We need a storage account to be created and one container needs to be created in Azure Portal.

Till now, if If you do not have the storage account and container created, you can refer to my article How to Create Azure Blob storage to create both of them.

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.

By now, you have the Azure storage account and container created in Azure Portal. So now we will create a console application using C# and visual studio 2019 for the below functionality

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

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 then click on the Next button.

Upload And Download Files From Blob Storage Using C# and Powershell

Enter the Project name and select the location and then click on the Create button to create the console application.

How to upload file in Azure blob storage using C#

Now, the next step is to install the Nuget package in order to access the Azure Storage

You can go to Tools –> NuGet Package Manager –> Manage NuGet Packages for Solution

How to Upload and Download File From Azure Blob Storage Using C# and PowerShell

Search for Azure storage and select the Azure.Storage.Blobs. Check the Project name option and then click on the Install button.

Upload file in Azure blob storage using C#

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

Create file in Azure blob storage using 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

how to upload a file to azure blob in c#

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, you need to add the below two libraries as a reference to the project

how to upload And Download Files From 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 file file 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();
        }
Steps to upload file in Azure blob storage using C#

I have created one class named as AzureDemo where the method definitions are present.

How to upload file in Azure blob storage using C#

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

In the AzureFileUpload() and AzureFileDownLoad() method, 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 that you are going to use and then click on the Access keys and 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 in 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!!!!");
        }
Easy way to upload file in Azure blob storage using C#
Easy steps to upload file in Azure blob storage using C#

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

//Copy and paste the connection string from 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 below line of code is used to create the instance of the Storage account

CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(myConnectionString);

Same way, The below line of code is used to create the instance of the Blob Client

CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();

This line of the code is used to create the instance of the Blob Container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);

The below lines of code will check, if any existing container exists else it will create a new container inside storage account with the mentioned name.

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

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

            }

Now, if you will run the code you can see it is uploaded successfully in the democontainer.

Easy guide to upload file in Azure blob storage using C#

How to upload file in azure blob storage using PowerShell

We saw above, the steps to upload a file in Azure using C# via a console application. Now let’s see how do you upload a file to Azure blob storage using PowerShell. We need to follow the below steps to upload the file in Azure blob storage.

First thing is we need to login to the Azure with the below cmdlet

PS C:\WINDOWS\system32> Login-AzureRmAccount

Next step is to get the storage account keys from the Azure portal and assign it 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, and navigate to the storage account that you are going to use and then 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, Blob along the Storage context.

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

Now the Complete PowerShell script is as below

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 
Upload file in azure blob storage using PowerShell

It will ask you to enter the Azure credentials, Enter your credentials and click sign in button.

how do you upload a file to azure blob storage using powershell

Then it will execute successfully and will upload the file in the Azure container

Upload a file to azure blob storage powershell

Now to check that, Navigate to the container in Azure portal and can see the file uploaded successfully.

Upload a file to azure blob storage powershell

Download a file from Azure Blob storage (using Azure Portal)

Now, we will discuss how to download files from azure blob storage using Azure Portal. Follow the below steps to download the file from Azure blob storage container.

Select the uploaded file which you have done using the above steps. Right-click on the selected file and then click on the Download link from the pop-up.

How to download a file from Azure Blob storage (using Azure Portal)

Another way is, you can click on the three dots(…) option and select the Download option from there

How to download a file from Azure Blob storage using Azure Portal

Now, the file will download successfully.

How to download a file from the Azure blob storage using C#

There are two parameters for the AzureFileDownload() method. The name of the file to be upload and the Container name.

Below is the code for the AzureFileDownload().

public void AzureFileDownload(string fileName, string containerName)
        {
            string mystrconnectionString = "DefaultEndpointsProtocol=https;AccountName=sqlvaqrc7kssdx4eus;AccountKey=Coro2SvGOTD+nT3YdaKCrnauz########";

            CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(mystrconnectionString);
            CloudBlobClient myBlob = mycloudStorageAccount.CreateCloudBlobClient();

            CloudBlobContainer mycontainer = myBlob.GetContainerReference(containerName);
            CloudBlockBlob myBlockBlob = mycontainer.GetBlockBlobReference(fileName);

            // provide the location of the file need to be downloaded          
            Stream fileupd = File.OpenWrite (@"D:\Bijay\" + fileName);
            myBlockBlob.DownloadToStream(fileupd);

            Console.WriteLine("Download completed Successfully!!!!");
        }
How to download a file from the Azure blob storage using C#

Now after running the code, i got the below output. It downloaded the file at the specified path.

Download a file from the Azure blob storage using C#

How to download file from azure blob storage using PowerShell

We saw above, the steps to download a file from Azure using C# via a console application. Now let’s see how do you download a file from Azure blob storage using PowerShell. We need to follow the below steps to download the file from the Azure blob storage.

First thing is we need to login to the Azure with the below cmdlet to interact with Azure objects

PS C:\WINDOWS\system32> Login-AzureRmAccount

Next step is to get the storage account keys from the Azure portal and you need to assign it to a variable with the help of the below cmdlet

$Keys = "Coro2SvGOTD+nT3YdaKCrnXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=="

To get the above key from the Azure portal,

You need to log into the Azure portal and go to the storage account that from where you are getting the file and then click on the Access keys and then copy the Keys as highlighted below

How to download a file from the azure blob storage using PowerShell

As mentioned above already, use the below PowerShell cmdlet i.e the Context to access the Azure storage account with the above AccessKey.

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

The below cmdlet is to provide the local file name needs to be download , container name, Destination local path where the file need to be downloaded along with the Storage context.

Get-AzStorageBlobContent -Blob "Upload.txt" -Container $containerName -Destination "D:\Bijay\" ` -Context $StorageContext 

Below is the complete PowerShell script to download the file from the Azure blob storage

Login-AzureRmAccount
$Keys = "Coro2SvGOTD+nT3YdaKCrnauzWvHMF6BmuD9PfSKtNJWt6etOMjkwg6KNSQ39zOOQcMpDSjyBKHdALGIg6gZ4Q=="
$StorageContext = New-AzureStorageContext -StorageAccountName sqlvaqrc7kssdx4eus -StorageAccountKey $Keys
$containerName = "democontainer";
Get-AzStorageBlobContent -Blob "Upload.txt" `
  -Container $containerName `
  -Destination "D:\Bijay\" `
  -Context $StorageContext 
Download file from azure blob storage using PowerShell

It will ask to provide the Azure credential, enter you credential and click on Sign in button.

Download a file from azure blob storage in PowerShell

You can see above the script executed successfully with out any issue.

Download file from azure blob storage using PowerShell

Now to check that, Navigate to the local path and can see the file downloaded successfully.

Steps to download file from azure blob storage using PowerShell

You may like following Azure tutorials:

Conclusion

Well, here in this article, we discussed how to upload file in Azure blob storage in Azure Portal, How to download a file from Azure Blob storage in Azure Portal, steps to upload files in Azure blob storage using C# and PowerShell and finally we saw the steps to download a file from the Azure blob storage using C# and PowerShell. We have created a simple console application using C# and Visualstudion 2019 to interact with the Azure Blob storage. Hope you enjoyed this article!!!!.

Leave a Comment