Azure Blob Storage: A PowerShell Guide to File Copy

Published:30 June 2019 - 2 min. read

Learn how to copy files to Azure blob storage with the PowerShell Set-AzureStorageBlobContent cmdlet in this handy tutorial.

I’ve been doing a lot of Azure IaaS work via ARM lately in PowerShell. As a result, I’ve unfortunately found out how bad the documentation and behavior is for the Azure PowerShell module but I’ve persisted and have overcome!

As part of this project, I’ve had to upload a bunch of files up to an Azure storage account container. Being used to PowerShell’s Copy-Item cmdlet to copy files I figured there had to be something like that for Azure in the Azure PowerShell module but I was let down. Instead, I was faced with using three separate cmdlets just to get a single file up to a common storage container.

Once I figured out how to do it, I didn’t really feel like remembering how to get a file up to an Azure storage container every time. So, just as any PowerShell developer would do, I made an easy-to-use function called Copy-AzureItem to copy files to Azure Blob Storage. This function has saved me tons of time and hopefully it can do the same for you as well.

Here’s how it works:

First, in order to get a file into an Azure ARM storage container entails three different “objects”; a storage account, a storage account container and the blob or file itself. You must specify each of these “objects” when uploading the file. To do this, you can use three different cmdlets on one line.

Get-AzStorageAccount @saParams | Get-AzStorageContainer @scParams | Set-AzureStorageBlobContent@bcParams

As you can tell, I’m using splatting to provide the various parameters to each cmdlet.

All of this just to copy a file up to Azure? No thanks! Instead, how about doing something like this?

Copy-AzureItem -FilePath C:\MyFile.exe -ContainerName azcontainer

Much easier! Granted, I’m defaulting the resource group and storage account in the function but that’s easy to update.

So, without further ado, feel free to download this function from my Github repo. If you’re too lazy to do that, copy and paste it from here.

function Copy-AzureItem
{
	<#
	.SYNOPSIS
		This function simplifies the process of uploading files to an Azure storage account. In order for this function to work you
		must have already logged into your Azure subscription with Login-AzureAccount. The file uploaded will be called the file
		name as the storage blob.
		
	.PARAMETER FilePath
		The local path of the file(s) you'd like to upload to an Azure storage account container.
	
	.PARAMETER ContainerName
		The name of the Azure storage account container the file will be placed in.
	
	.PARAMETER ResourceGroupName
		The name of the resource group the storage account is in.
	
	.PARAMETER StorageAccountName
		The name of the storage account the container that will hold the file is in.
	#>
	[CmdletBinding()]
	param
	(
		[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
		[ValidateNotNullOrEmpty()]
		[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
		[Alias('FullName')]
		[string]$FilePath,
	
		[Parameter(Mandatory)]
		[ValidateNotNullOrEmpty()]
		[string]$ContainerName,
	
		[Parameter()]
		[ValidateNotNullOrEmpty()]
		[string]$ResourceGroupName = 'ResourceGroup',
	
		[Parameter()]
		[ValidateNotNullOrEmpty()]
		[string]$StorageAccountName = 'StorageAccount'
	)
	process
	{
		try
		{
			$saParams = @{
				'ResourceGroupName' = $ResourceGroupName
				'Name' = $StorageAccountName
			}
			
			$scParams = @{
				'Container' = $ContainerName
			}
			
			$bcParams = @{
				'File' = $FilePath
				'Blob' = ($FilePath | Split-Path -Leaf)
			}
			Get-AzureRmStorageAccount @saParams | Get-AzureStorageContainer @scParams | Set-AzureStorageBlobContent @bcParams
		}
		catch
		{
			Write-Error $_.Exception.Message
		}
	}
}

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!