Friday, April 5, 2013

Windows Azure storage through Powershell

This is just a first stab at manipulating Windows Azure Storage via Powershell.

Get Microsoft.WindowsAzure.Storage.dll using NuGet 
  1. Navigate to this by first going to the Windows Azure Downloads site.
  2. Select .NET. 
  3. Select Client libraries under Resources.
  4. Instructions on how to download Windows Azure Storage are found here. 
Here is the code I'm using to download files from blob storage

Param (
    [Parameter(Mandatory=$true)] [String] $StorageAccountName,
    [Parameter(Mandatory=$true)] [String] $AccessKey,
    [Parameter(Mandatory=$true)] [String] $BlobFilename,
    [Parameter(Mandatory=$true)] [String] $LocalFilename,
    [String] $StorageAccountEndpoint = "http://$storageAccountName.blob.core.windows.net/"
)

Add-Type -Path '.\Microsoft.WindowsAzure.Storage.dll'

$BlobFilename = $BlobFilename.Replace('%20', ' ').Trim('/')
$storageCredentials = New-Object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials($StorageAccountName, $AccessKey)
$blobClient = New-Object Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient($StorageAccountEndpoint, $storageCredentials)
$blob = $blobClient.GetBlobReferenceFromServer($BlobFilename)
$stream = New-Object System.IO.FileStream($LocalFilename, [System.IO.FileMode]::Create)
$blob.DownloadToStream($stream)
$stream.Close()





No comments:

Post a Comment