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 Files to Azure Blob Storage Using AzCopy

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)

Create a container

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)
Upload blobs to the container

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
how to upload file to azure storage

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)
Uploading and Downloading a Stream into an Azure Storage Blob

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
Upload to Azure Blob Storage using a PowerShell GUI 

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#
Upload file to azure blob storage 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
Upload And Download File To Azure Blob Storage Using C#

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#
Upload image to azure blob storage c#

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

Create file in Azure blob storage using C#
Upload image data in the cloud with Azure Storage

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
Upload file to azure blob storage rest api

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#
Upload, download, and list blobs with PowerShell

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.

Upload file to azure blob storage c#

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#
How to Download Blob Contents from an Azure Storage Account using PowerShell

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#
How do I save Azure blob storage in 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#
Download blobs

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.

Upload blobs from local folder

Sign in to Azure

First thing is we need to login to the 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

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

How to Upload Files to Azure File Share 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
Upload a block blob

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
How do I upload to BLOB storage?

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

Upload a file to azure blob storage powershell
How do you upload a file to Azure Blob Storage using 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
Upload 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 the 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.

Download blobs to local disk

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

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

PS C:\WINDOWS\system32> Connect-AzAccount

How to Download Blob Contents from an Azure Storage Account using PowerShell

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
How To Download Azure BLOB Storage Using Azure 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

How to Download Blob Contents from an Azure Storage Account 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
How do I download a file from Azure blob storage using PowerShell?

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

Download file from azure blob storage using PowerShell
Uploading and downloading files to Azure Blob Storage with 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
Download blobs to local disk

How to Upload Files to Azure Blob Storage Using AzCopy

Well, above we have discussed How to Upload and Download File From Azure Blob Storage Using C#, PowerShell, and Azure Portal. Now, the time to discuss How to Upload Files to Azure Blob Storage Using AzCopy.

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

  • Follow the below steps to Upload Files to Azure Blob Storage Using AzCopy.Open the command prompt and navigate to the folder where you have downloaded the AzCopy and then run the below command to login.
azcopy login
Upload Files to Azure Blob Storage Using AzCopy
Tools to Upload Data to Azure Blob Storage
  • Once you will login successfully, you will see the login succeeded. Now run the below command 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 will execute the above command, it will show you a message that “Successfully created the resource”

  • The next step is execute the below command 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 will execute the above command, it will show you the message in the command prompt that “Final Job status: Completed”.

  • You can execute the below command if you want 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 will execute the above command it will show you the message in the command prompt that “Final Job status: Completed”.

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

This is How to Upload Files to Azure Blob Storage Using AzCopy.

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, How to Upload Files to Azure Blob Storage Using AzCopy. We have created a simple console application using C# and Visualstudion 2019 to interact with the Azure Blob storage. Hope you enjoyed this article!!!!.