Monday, 26 June 2023

How to Upload Files to Azure Blob Storage with PowerShell

 How to Upload Files to Azure Blob Storage with PowerShell

 

Objective:- To upload files/folders to the blob storage via script/PowerShell command

1.      Install the latest version of Azure PowerShell for all users on the system in a PowerShell session opened with administrator rights using the following command:

Install-Module -Name Az -AllowClobber -Scope AllUsers
 

2.       Use the following command and sign in to your Azure subscription when prompted:

Connect-AzAccount

3.       Get the storage account context to be used for the data transfer using the following commands:

 

$uploadstorage=Get-AzStorageAccount -ResourceGroupName <resource group name> -Name <storage account name>

 

Update the place holders <resource group name> and <storage account name> with values specific to your environment


4.       Run the following command to upload a file from your local directory to a container in Azure storage:

Set-AzStorageBlobContent -Container "<storage container name>" -File "<Location of file in local disk>" -Context $storcontext

Replace the placeholders <storage container name> and <Location of file in local disk> with values specific to your environment

Once the file is uploaded successfully, you will get a message similar to what you can see in the screenshot below:








5.       To upload all files in the current folder, run the following command

Get-ChildItem -File -Recurse | Set-AzStorageBlobContent -Container "<storage container name>" -Context $storcontext


6.      







If you browse to the Azure storage container, you will see all the files uploaded


 


 

 

 


 

Here is the script code


Connect-AzAccount

$uploadstorage=Get-AzStorageAccount -ResourceGroupName Storage-Group -Name StorageName

$storcontext=$uploadstorage.Context

 

 

Set-AzStorageBlobContent -Container "Name of Container" -File " Location of file in local disk " -Context $storcontext


Copy and past in notepad save as script.ps1

No comments:

Post a Comment

How to create Webinar on teams app

  How to create Webinar on teams app   ·          In the Teams calendar, select the arrow next to  New meeting  and select  Webinar . ...