Marius
Marius

Reputation: 1072

Download blob from Azure storage with powershell -> LoaderException

I use powershell to download a blob from blobstorage in an Azure startup task. I updated Microsoft.WindowsAzure.Storage library today from 3.0.3.0 to 4.0.1.0 via NuGet.

After the library update files are still downloaded correctly but I get same sort of warning in command window:

'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.'

function download_from_storage ($container, $blob, $connection, $destination) {
    Add-Type -Path ((Get-Location).Path + '\Microsoft.WindowsAzure.Storage.dll')
    $storageAccount = [Microsoft.WindowsAzure.Storage.CloudStorageAccount]::Parse($connection)
    $blobClient = New-Object Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient($storageAccount.BlobEndpoint, $storageAccount.Credentials)   
    $container = $blobClient.GetContainerReference($container)
    $remoteBlob = $container.GetBlockBlobReference($blob)
    $remoteBlob.DownloadToFile($destination + "\" + $blob, [System.IO.FileMode]::OpenOrCreate)
}

$connection_string = 'DefaultEndpointsProtocol=https;AccountName=<AcountName>;AccountKey=<Accountkey>'

# JRE
$jre = 'jre-7u60-windows-x64.exe'
$node = 'node-v0.10.29-x64.msi'
download_from_storage 'java-runtime' $jre $connection_string (Get-Location).Path
download_from_storage 'nodejs' $node $connection_string (Get-Location).Path

Since it is still working I am just clueless why the message occurs in the first place.

Upvotes: 2

Views: 7004

Answers (2)

Ogail
Ogail

Reputation: 177

You can do this by installing Azure PowerShell itself in the startup task and then execute the download Azure blob cmdlet. Here are rouphly the steps

Installing Azure PowerShell automatically

  • Create new service project (New-AzureServiceProject)
  • Execute Add-AzureWebRole
  • Change the cscfg to use osFamily=3 (to use new PS version which is compatible with Azure PS)
  • Copy Azure PowerShell MSI under WebRole1\bin directory
  • Edit WebRole1\startup.cmd to include this line msiexec /i AzurePowerShell.msi /quiet

Authenticating Azure PowerShell so it can execute cmdlets (if you want to use storage cmdlets only you can ignore this step and pass your storage key/name when executing the Get-AzureStorageBlobContent cmdlet)

  • Copy a latest publish settings file (myPublishSettings.publishsettings) inside WebRole1\bin folder
  • Edit WebRole1\startup.cmd to include this line after the one added before: PowerShell.exe –Command “Import-AzurePublishSettingsFile .\myPublishSettings.publishsettings)

Upvotes: 0

Raf
Raf

Reputation: 10117

This is not exactly an answer to your question but here is a much simpler way of downloading files from blob storage:

$dlPath = "C:\temp\"
$container = "BlobContainer"
Set-AzureSubscription "NameOfYourSubscription" -CurrentStorageAccount "storageAccountName"
Get-AzureStorageContainer $container | Get-AzureStorageBlob | 
    Get-AzureStorageBlobContent -Destination $container 

Upvotes: 1

Related Questions