General

Blob copy between Storage Accounts in Azure

Previously I copied a vhd file called Nano2016.vhd from my on-premises server in to Azure. Now I need to use this file for my other storage account. So it means I need to copy this file in to my other storage account.

StorageAccount01  —————–> StorageAccount02

vhds container        —————–> uploads container (will be created during copy)

The script I am going to run is;

$vhdName = “Nano2016.vhd”
$srcContainer = “vhds”
$destContainer = “uploads”
$srcStorageAccount = “Storageaccount01”
$destStorageAccount = “StorageAccount02”

$srcStorageKey = (Get-AzureStorageKey -StorageAccountName` $srcStorageAccount).Primary
$destStorageKey = (Get-AzureStorageKey -StorageAccountName` $destStorageAccount).Primary
$srcContext = New-AzureStorageContext –StorageAccountName` $srcStorageAccount `
-StorageAccountKey $srcStorageKey

$destContext = New-AzureStorageContext –StorageAccountName ` $destStorageAccount `
-StorageAccountKey $destStorageKey

New-AzureStorageContainer -Name $destContainer `
-Context $destContext
$copiedBlob = Start-AzureStorageBlobCopy -SrcBlob $vhdName `
-SrcContainer $srcContainer `
-Context $srcContext `
-DestContainer $destContainer `
-DestBlob $vhdName `
-DestContext $destContext

$copiedBlob | Get-AzureStorageBlobCopyState

As you see we need to gather some info to such as our file name, source container which our file hosted “vhds”, destination container which in my case it will be “uploads” and will be created during running this script and of course our storage accounts. you can use both Azure portal or powershell for these.

Add-AzureAccount
Get-AzureStorageAccount | Format-Table -Property Label

From Azure Portal

Storage> you can get all the storage accounts and click on your source storage account and then containers to choose your container and the file.

Cap9

and also get your storage account names so that we can use them for our script.

$vhdName = “Nano2016.vhd”
$srcContainer = “vhds”
$destContainer = “uploads”
$srcStorageAccount = “Storageaccount01”
$destStorageAccount = “StorageAccount02”

After that we can run our script. I will run one line at a time to see the steps clearly..

PS C:\Users\user> $vhdName = “Nano2016.vhd”
$srcContainer = “https://storageaccount01.blob.core.windows.net/vhds/”
$destContainer = “https://storageaccount02.blob.core.windows.net/vhds/”
$srcStorageAccount = “storageaccount01”
$destStorageAccount = “storageaccount02”

and

PS C:\Users\user> $srcStorageKey = (Get-AzureStorageKey -StorageAccountName $srcStorageAccount).Primary
PS C:\Users\user> $destStorageKey = (Get-AzureStorageKey -StorageAccountName $destStorageAccount).Primary
PS C:\Users\user> $srcContext = New-AzureStorageContext –StorageAccountName $srcStorageAccount `
-StorageAccountKey $srcStorageKey
PS C:\Users\user> $destContext = New-AzureStorageContext –StorageAccountName $destStorageAccount `
-StorageAccountKey $destStorageKey

Next to create a new container in destination storage account;

PS C:\Users\user> New-AzureStorageContainer -Name $destContainer `
-Context $destContext

Blob End Point: https://storageaccount02.blob.core.windows.net/

Name                     PublicAccess                          LastModified
—-                        ————                      ————
uploads                  Off                                               2/25/2016 12:06:05 PM +00:00

Finally starting copy task;

PS C:\Users\user> $copiedBlob = Start-AzureStorageBlobCopy -SrcBlob $vhdName `
-SrcContainer $srcContainer `
-Context $srcContext `
-DestContainer $destContainer `
-DestBlob $vhdName `
-DestContext $destContext

PS C:\Users\user>

As you see you don’t get any information about copying process…

So we need to run another cmdlet;

PS C:\Users\user> $copiedBlob | Get-AzureStorageBlobCopyState
CopyId : 6557ef45-b677-4bbe-92b5-b9888676acf6
CompletionTime :
Status : Pending   <————
Source : https://storageaccount01.blob.core.windows.net/vhds/Nano2016.vhd?sv=2015-04-05&sr=b&sig=a4cENt%2BLQGVJDoAUs5Pz42q2hHExN
wTW557lN%2BxfUlM%3D&se=2016-03-03T12:06:15Z&sp=r
BytesCopied : 0
TotalBytes : 107374182912
StatusDescription :

Or just go to Azure Portal > Storage > Destination Storage Account > Container

in my case I can see a new contaier “uploads” been created and my file is there.

Capture223

And again if you run;

PS C:\Users\user> $copiedBlob | Get-AzureStorageBlobCopyState

CopyId : 6557ef45-b677-4bbe-92b5-b9888676acf6
CompletionTime : 2/25/2016 12:08:46 PM +00:00
Status : Success  <—————-
Source : https://storageaccount02.blob.core.windows.net/vhds/Nano2016.vhd?sv=2015-04-05&sr=b&sig=a4cENt%2BLQGVJDoAUs5Pz42q2hHExN
wTW557lN%2BxfUlM%3D&se=2016-03-03T12:06:15Z&sp=r
BytesCopied : 107374182912
TotalBytes : 107374182912
StatusDescription :

As you see the status has changed to Success from Pending…

Let’s review the parameters in the preceding example:
■■ The SrcBlob parameter expects the file name of source file to start copying.
■■ The SrcContainer parameter is the container the source file resides in.
■■ The Context parameter accepts a context object created by the New-AzureStorageContext cmdlet. The context has the storage account name and key for the source storage account and is used for authentication.
■■ The DestContainer is the destination container to copy the blob to. The call will fail if
this container does not exist on the destination storage account.
■■ The DestBlob parameter is the filename of the blob on the destination storage account.
The destination blob name does not have to be the same as the source.
■■ The DestContext parameter also accepts a context object created with the details of
the destination storage account including the authentication key.

P:S. To copy between storage accounts in separate subscriptions, you need to call Select-AzureSubscription between the calls to Get-AzureStorageKey to switch to the alternate subscription.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s