【问题标题】:Download blob from Azure storage with powershell -> LoaderException使用 powershell 从 Azure 存储下载 blob -> LoaderException
【发布时间】:2014-08-09 17:12:28
【问题描述】:

我在 Azure 启动任务中使用 powershell 从 blobstorage 下载 blob。我今天通过 NuGet 将 Microsoft.WindowsAzure.Storage 库从 3.0.3.0 更新到了 4.0.1.0。

在仍然正确下载库更新文件后,我在命令窗口中收到相同类型的警告:

'无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。'

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

由于它仍在工作,我只是不知道为什么该消息首先出现。

【问题讨论】:

    标签: powershell azure azure-storage


    【解决方案1】:

    这并不完全是您问题的答案,但这是从 blob 存储下载文件的一种更简单的方法:

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

    【讨论】:

    • 新的 WorkerRole 实例中没有 azure powershell。因此我选择了 Add-Type cmdlet。但是你是对的,Azure powershell 可用,这会更简单。
    【解决方案2】:

    您可以通过在启动任务中安装 Azure PowerShell 本身来执行此操作,然后执行下载 Azure blob cmdlet。以下是大致步骤

    自动安装 Azure PowerShell

    • 新建服务项目(New-AzureServiceProject)
    • 执行 Add-AzureWebRole
    • 将 cscfg 更改为使用 osFamily=3(以使用与 Azure PS 兼容的新 PS 版本)
    • 将 Azure PowerShell MSI 复制到 WebRole1\bin 目录下
    • 编辑 WebRole1\startup.cmd 以包含此行 msiexec /i AzurePowerShell.msi /quiet

    验证 Azure PowerShell 以便它可以执行 cmdlet(如果您只想使用存储 cmdlet,则可以忽略此步骤并在执行 Get-AzureStorageBlobContent cmdlet 时传递您的存储密钥/名称)

    • 在 WebRole1\bin 文件夹中复制最新的发布设置文件 (myPublishSettings.publishsettings)
    • 编辑 WebRole1\startup.cmd 以在之前添加的行之后包含这一行:PowerShell.exe –Command “Import-AzurePublishSettingsFile .\myPublishSettings.publishsettings)

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 2013-01-21
      • 2015-06-20
      • 2022-11-02
      • 2019-12-19
      • 2019-09-05
      • 2012-12-08
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多