Copying Azure Blob Storage Containers Between Accounts With PowerShell and AzCopy
A few months ago I wrote about backing up and restoring Azure blobs from a local environment. You can read that post here. This is not an efficient approach for moving blobs from one Azure storage account to another. In this post I will show you how to copy all containers from one account to another using PowerShell and how to copy individual Azure storage containers using AzCopy.
[original publish date: 12/30/16]
Copying All Containers with PowerShell
This script will copy all containers from one Azure storage account to another. This can be useful for populating Azure development accounts.
First we will set up our Azure storage accounts and create a storage context for each:
Finally we will get all blobs from the current container and attempt to copy all of them to the destination container. Note, the -Force will cause existing blobs to be overwritten if they exist.$SourceAzureAccountName = "SourceAzureAccount"
$SourceAzureKey = "SourceAzureKey"
$DestAzureAccountName = "DestAzureAccount"
$DestAzureKey = "DestAzureKey"
$SourceCtx = New-AzureStorageContext $SourceAzureAccountName -StorageAccountKey $SourceAzureKey
$DestCtx = New-AzureStorageContext $DestAzureAccountName -StorageAccountKey $DestAzureKey
Next we loop through all Azure storage containers on the source account and attempt to create a container for each in the destination account, and logging if the container already exists:Get-AzureStorageContainer -Context $SourceCtx | % {
$ContainerName = $_.Name
try
{
New-AzureStorageContainer -Name $ContainerName -Context $DestCtx -ErrorAction Stop
"Creating container " + $ContainerName
}
catch [Microsoft.WindowsAzure.Commands.Storage.Common.ResourceAlreadyExistException]
{
"Container " + $ContainerName + " already exists"
}
$blobs = Get-AzureStorageBlob -Container $ContainerName -Context $SourceCtx
$blobs | Start-AzureStorageBlobCopy -DestContainer $ContainerName -DestContext $DestCtx -Force | Out-Null
}
Copying Individual Containers with AzCopy
AzCopy is a powerful Windows command-line tool that can be used to copy individual containers or groups of blobs from one Azure storage account to another. Copying all blobs from one container is trivial and can be done with the following command:AzCopy /Source:https://SourceAzureAccount.blob.core.windows.net/SourceContainer
/Dest:https://DestAzureAccount.blob.core.windows.net/DestContainer
/SourceKey:SourceKey /DestKey:DestKey /S /Y
The /S parameter enables recursive mode and will copy all blobs within the container. The /Y parameter suppresses confirmation dialog and will overwrite existing blobs. For a full list of available AzCopy parameters check Microsoft's documentation here.[original publish date: 12/30/16]
Comments
Post a Comment