Copying VHDs (Blobs) between Storage Accounts

0

Initiating an Asynchronous Copy Request (anonymous source)

In this example I am copying the VHD from a non-authenticated source. This means I have opened up access on the source container in Windows Azure Storage to allow anonymous access. Note: the source URL does NOT have to be based in Windows Azure Storage. The source file can reside in any publicly accessible location.

Select-AzureSubscription “my subscription”

### Source VHD (West US) – anonymous access container ###
$srcUri = “http://mwwestus1.blob.core.windows.net/source/testcopy1.vhd”

### Target Storage Account (East US) ###
$storageAccount = “mweastus1”
$storageKey = “STORAGEACCOUNTKEY”

### Create the destination context for authenticating the copy
$destContext = New-AzureStorageContext –StorageAccountName $storageAccount `
-StorageAccountKey $storageKey

### Target Container Name
$containerName = “copiedvhds”

### Create the target container in storage
New-AzureStorageContainer -Name $containerName -Context $destContext

### Start the Asynchronous Copy ###
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `
-DestContainer $containerName `
-DestBlob “testcopy1.vhd” `
-DestContext $destContext

Initiating an Asynchronous Copy Request (authenticated source)

This example is essentially the same code except I am specifying a context object to access the source storage account. This allows you to copy VHDs from Windows Azure storage without opening up permissions to everyone.

Select-AzureSubscription “my subscription”

### Source VHD (West US) – authenticated container ###
$srcUri = “http://mwwestus1.blob.core.windows.net/sourceauth/testcopy1.vhd”

### Source Storage Account (West US) ###
$srcStorageAccount = “mwwestus1”
$srcStorageKey = “SOURCESTORAGEKEY”

### Target Storage Account (West US) ###
$destStorageAccount = “mwwestus2”
$destStorageKey = “DESTSTORAGEKEY”

### Create the source storage account context ###
$srcContext = New-AzureStorageContext –StorageAccountName $srcStorageAccount `
-StorageAccountKey $srcStorageKey

### Create the destination storage account context ###
$destContext = New-AzureStorageContext –StorageAccountName $destStorageAccount `
-StorageAccountKey $destStorageKey

### Destination Container Name ###
$containerName = “copiedvhds”

### Create the container on the destination ###
New-AzureStorageContainer -Name $containerName -Context $destContext

### Start the asynchronous copy – specify the source authentication with -SrcContext ###
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `
-SrcContext $srcContext `
-DestContainer $containerName `
-DestBlob “testcopy1.vhd” `
-DestContext $destContext

Checking Copy Status

Once the copy begins it is completely asynchronous meaning you could immediately start other copy operations without waiting for the previous to complete. It is useful of course to determine the progress of your blob copy. This is made possible through the Get-AzureStorageBlobCopyState cmdlet. Pipe the blob object returned from Start-AzureStorageBlobCopy to Get-AzureStorageBlobCopyState and it will return another object with progress indicators as to how many bytes have been copied and the current status of the operation.

### Retrieve the current status of the copy operation ###
$status = $blob1 | Get-AzureStorageBlobCopyState

### Print out status ###
$status

### Loop until complete ###
While($status.Status -eq “Pending”){
$status = $blob1 | Get-AzureStorageBlobCopyState
Start-Sleep 10
### Print out status ###
$status
}

You might also like More from author

Leave A Reply

Your email address will not be published.