Hi everyone, I hope you’re staying healthy. I want to show you how to copy a blob between two Azure storage accounts in the same Azure subscription using PowerShell in this post.
- Two storage accounts will be used in the following example. You can use two existing storage accounts, or you can create new ones. If you want to know how to create a storage account using PowerShell, check out this link.
Azure PowerShell Workaround #
If you want to know how to install the PowerShell Azure module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.
This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:
Get-AzSubscription Select-AzSubscription -Subscription "My Subscription"
Once you set your default subscription, you’re ready to start.
Set the variables #
Here, we define the characteristics of our environment and the resource’s properties.
Source Storage Account #
We first define the variables for the source storage account. In the $ blobName variable, you specify the file’s name that will be copied to the other storage account.
$srcResourceGroupName = "RG-DEMO-WE" $srcStorageAccountName = "storageaccountdemowe" $srcContainer = "sourcefolder" $blobName = "dataDisk.vhd"
Destination Storage Account #
As in the case of the source account, we define the variables for the destination storage account.
$destResourceGroupName = "RG-DEMO-NE" $destStorageAccountName = "storageaccountdemone" $destContainer = "destinationfolder"
Storage Account Keys #
To obtain the key for each of the storage accounts involved. It would help if you used the Get-AzStorageAccountKey cmdlet with the following syntax.
$srcStorageKey = Get-AzStorageAccountKey ` -Name $srcStorageAccountName ` -ResourceGroupName $srcResourceGroupName $destStorageKey = Get-AzStorageAccountKey ` -Name $destStorageAccountName ` -ResourceGroupName $destResourceGroupName
Storage Account Context #
Using the New-AzStorageContext cmdlet, you set the context for the source and destination storage account.
$srcContext = New-AzStorageContext ` -StorageAccountName $srcStorageAccountName ` -StorageAccountKey $srcStorageKey.Value $destContext = New-AzStorageContext ` -StorageAccountName $destStorageAccountName ` -StorageAccountKey $destStorageKey.Value
Destination Container (optional) #
This step is optional, and you should use the following command if the container in the destination account does not exist yet. If you want to know more about how to Manage containers and blobs, check out this link.
New-AzStorageContainer ` -Name $destContainer ` -Context $destContext
Start the copy process #
Finally, to start the blob copy process between the two storage accounts, you should use the Start-AzStorageBlobCopy cmdlet with the following syntax. If you want the file in the destination directory to have a different name than the source file, you should change the value of the -DestBlob parameter.
$copyOperation = Start-AzStorageBlobCopy ` -SrcBlob $blobName ` -SrcContainer $srcContainer ` -Context $srcContext ` -DestBlob $blobName ` -DestContainer $destContainer ` -DestContext $destContext
The duration of the copy process will depend on the size of the blob.
Check the copy status #
If you want to monitor the copy process, you should use the Get-AzStorageBlobCopyState cmdlet as follows.
$copyOperation | Get-AzStorageBlobCopyState
Thanks for reading my post. I hope you find it helpful. If you want to know more about the Azure Storage Cmdlets, check out this link.