Guide to Uploading Files to Azure Blob Storage with PowerShell
Azure Blob storage is Microsoft’s object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data.
Use the Azure PowerShell module to create and manage Azure resources. You can create or manage Azure resources from the PowerShell command line or in scripts. This guide will help you to upload files/folders to the blob storage via script/PowerShell command.
- 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
- Use the following command and sign in to your Azure subscription when prompted:
- 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.
- 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:
- To upload all files in the current folder, run the following command
Get-ChildItem -File -Recurse | Set-AzStorageBlobContent -Container “<storage container name>” -Context $storcontext
- If you browse to the Azure storage container, you will see all the files uploaded.
Here is the script code
<!-- wp:paragraph --> <p>$uploadstorage=Get-AzStorageAccount -ResourceGroupName Storage-Group -Name <strong>StorageName</strong></p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>$storcontext=$uploadstorage.Context</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>Set-AzStorageBlobContent -Container "<strong>Name of Container</strong>" -File " <strong>Location of file in local disk</strong> " -Context $storcontext</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>Copy and past in notepad save as script.ps1</p> <!-- /wp:paragraph -->