Windows Azure PowerShell Copy Blob Script

Here is a PowerShell script that can be used to move data in Windows Azure from one subscription to another subscription’s blob storage. The script uses Start-AzureStorageBlobCopy command which is an asynchronous copy operation and copies all the blobs from one container to another. If selected file moving is the intention you can pass in some type of file matching pattern.

Prerequisite:
Download Windows Azure PowerShell
Install Windows Azure Certificate for your subscription

Copy and paste code from here.

#Change these to match the source account
$sourceStorageAccountName = "<AzureStorageAccountName>"
$sourceContainerName = "<AzureContainerName>"
$sourceStorageKey = "<AzureStorageSubscriptionKey>" #Need this if moving data from a different subscription 
  
#Destination Account Information  
$destStorageAccountName = Read-Host "Enter Destination Storage Account Name"
$destStorageAccountKey = Get-AzureStorageKey $destStorageAccountName | %{$_.Primary}
$destContainerName = Read-Host "Enter Destination Container Name"
  
$sourceContext = New-AzureStorageContext -StorageAccountName $sourceStorageAccountName -StorageAccountKey $sourceStorageKey -Protocol Http
$destContext = New-AzureStorageContext -StorageAccountName $destStorageAccountName -StorageAccountKey $destStorageAccountKey
  
$uri = $sourceContext.BlobEndPoint + $sourceContainerName +"/"
# Copy Operation
Get-AzureStorageBlob `
    -Context $sourceContext `
    -Container $sourceContainerName | 
    ForEach-Object `
    { Start-AzureStorageBlobCopy `
        -SrcUri "$uri$($_.Name)" `
        -DestContext $destContext `
        -DestContainer $destContainerName `
        -DestBlob "hackathon/$($_.Name)" `
    }
#Checking Status of Blob Copy -- This can be commented out if no confirmation is needed 
Get-AzureStorageBlob `
    -Context $sourceContext `
    -Container $sourceContainerName |
    ForEach-Object `
    { Get-AzureStorageBlobCopyState `
        -Blob $_.Name `
        -Context $destContext `
        -Container $destContainerName `
        -WaitForComplete `
    }

 

If the data you are copying is on a public blob you can use ‘anonymous’ access instead of providing the $sourceStorageKey. In that case, change $sourceContext to something like the following:

$sourceContext = New-AzureStorageContext -Anonymous -StorageAccountName $sourceStorageAccountName -Protocol Http

About these ads

About MurshedAzureCAT
http://about.me/murshedazurecat

3 Responses to Windows Azure PowerShell Copy Blob Script

  1. Pingback: Microsoft SQL Server Development Customer Advisory Team

  2. Sreekumar says:

    I have followed these instructions. However, during copy it just waits indefinitely. This is the code

    Start-AzureStorageBlobCopy -SrcBlob $($_.Name) -SrcContainer $i.Name -Context $azurestoragecontext -DestContext $waStorageContext -DestContainer $waContainer -DestBlob $dstblobname
    Get-AzureStorageBlobCopyState -Blob $dstblobname -Context $waStorageContext -Container $waContainer -WaitForComplete

  3. Nilesh says:

    Got a error,
    Start-AzureStorageBlobCopy : The remote server returned an error: (404) Not Found. HTTP Status Code: 404 – HTTP Error Message: The
    specified resource does not exist.
    At C:\Automation\Untitled3.ps1:39 char:7
    + { Start-AzureStorageBlobCopy `
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : CloseError: (:) [Start-AzureStorageBlobCopy], StorageException
    + FullyQualifiedErrorId : StorageException,Microsoft.WindowsAzure.Commands.Storage.Blob.Cmdlet.StartAzureStorageBlobCopy

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: