Copying a Blob Using Azure PowerShell
On regular occasions, I need to move blobs from one storage account to another. And there are a couple of ways that we can do this. The first is to download the blob and upload it to a new location. In most scenarios, this is not what we want. This post demonstrates how to create an Azure Storage Blob Copy job and how to monitor its progress.
Setup The Destination Storage Account
$destinationStorageAccountName = 'westusbrisebois' New-AzureStorageAccount -StorageAccountName $destinationStorageAccountName ` -Location 'West US' $container = 'vhds' # Destination Storage Account Context $destinationKey = (Get-AzureStorageKey -StorageAccountName $destinationStorageAccountName).Primary $destinationContext = New-AzureStorageContext –StorageAccountName $destinationStorageAccountName ` -StorageAccountKey $destinationKey # Create the destination container New-AzureStorageContainer -Name $container ` -Context $destinationContext
Copy Blob to The Target Storage Account
# Source Storage Account Context $sourceStorageAccountName = "eastusbrisebois" $sourceKey = (Get-AzureStorageKey -StorageAccountName $sourceStorageAccountName).Primary $sourceContext = New-AzureStorageContext –StorageAccountName $sourceStorageAccountName ` -StorageAccountKey $sourceKey # Find the VHDs to copy Get-AzureStorageBlob -Container 'vhds' ` -Context $sourceContext Container Uri: https://eastusbrisebois.blob.core.windows.net/vhds Name vm-os-disk.vhd PageBlob 10737418752 # Create the Blob Copy Job $blobName ='vm-os-disk.vhd' $blobCopy = Start-AzureStorageBlobCopy -DestContainer $container ` -DestContext $destinationContext ` -SrcBlob $blobName ` -Context $sourceContext ` -SrcContainer $container # Blob Copy Progress while(($blobCopy | Get-AzureStorageBlobCopyState).Status -eq "Pending") { Start-Sleep -s 30 $blobCopy | Get-AzureStorageBlobCopyState }