Skip to content

Instantly share code, notes, and snippets.

@Sebmaster
Last active December 22, 2015 04:28
Show Gist options
  • Save Sebmaster/6416768 to your computer and use it in GitHub Desktop.
Save Sebmaster/6416768 to your computer and use it in GitHub Desktop.
A script to allow copying blobs which are currently in use
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$SrcStorageAccount,
[Parameter(Mandatory=$True)]
[string]$SrcStorageKey,
[Parameter(Mandatory=$True)]
[string]$SrcContainer,
[Parameter(Mandatory=$True)]
[string]$SrcBlob,
[string]$DstStorageAccount = $SrcStorageAccount,
[string]$DstStorageKey = $SrcStorageKey,
[string]$DstContainer = $SrcContainer,
[string]$DstBlob = $SrcBlob
)
$SrcStorageUri = "http://" + $SrcStorageAccount + ".blob.core.windows.net"
Add-Type -Path "C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.0\bin\Microsoft.WindowsAzure.StorageClient.dll"
#generate credentials based on the key
$creds = New-Object Microsoft.WindowsAzure.StorageCredentialsAccountAndKey($SrcStorageAccount, $SrcStorageKey)
#create an instance of the CloudBlobClient class
$blobClient = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient($SrcStorageUri, $creds)
#and grab a reference to our target blob
$blob = $blobClient.GetBlobReference($SrcContainer + "/" + $SrcBlob)
$snapshot = $blob.CreateSnapshot()
$srcContext = New-AzureStorageContext -StorageAccountName $SrcStorageAccount -StorageAccountKey $SrcStorageKey
$destContext = New-AzureStorageContext -StorageAccountName $DstStorageAccount -StorageAccountKey $DstStorageKey
New-AzureStorageContainer -Name $DstContainer -Context $destContext -ErrorAction SilentlyContinue
$snapshotUri = [Microsoft.WindowsAzure.StorageClient.Protocol.BlobRequest]::Get($snapshot.Uri, 0, $snapshot.SnapshotTime, $null).Address.AbsoluteUri;
$copyBlob = Start-AzureStorageBlobCopy -SrcUri $snapshotUri -SrcContext $srcContext -DestContainer $DstContainer -DestBlob $DstBlob -DestContext $destContext
$copyBlob | Get-AzureStorageBlobCopyState -WaitForComplete
$snapshot.Delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment