首页 > 解决方案 > Cannot convert AzureStorageContext to IStorageContext

问题描述

I want to copy files from a Azure Blob Storage Container to the Azure file share (in the same Azure Storage Account).

When using Start-AzureStorageFileCopy or New-AzureStorageDirectory I get an error on the context parameters:

Start-AzureStorageFileCopy : Cannot bind parameter 'Context'. Cannot convert the "Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext" value of type "Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext" to type "Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext".

$StorageAccountName = "myStorageAccount"
$StorageAccountKey = "FAKEbkasdw504jfja0jgldheeeffl03659d0ch259fv=="
$ContainerName = "myContainerName"
$ShareName = "myShareName"


Write-Output "Start moving folder content $($ContainerName) from storageaccount $($StorageAccountName) to file share $($ShareName)"

# Get 'context' of the source container
$StorageContext = New-AzStorageContext  -StorageAccountName $StorageAccountName `
                                        -StorageAccountKey $StorageAccountKey


# Loop through all blobs
$blobs = Get-AzStorageBlob -Container $ContainerName -Context $StorageContext
foreach ($blob in $blobs)
{
    # Switch slashes because fileshare expects \ instead /
    $FilePath = $blob.name -replace '/','\'

    # Get folderpath to create new folders
    $FolderPath = Split-Path -Path $FilePath

    # If folder was found (not the case for files in the rooth) create it on the share
    # Todo: check if it exists
    if ($FolderPath.Length -gt 0)
    {
        # Create folder
        Write-Output "Create folder $($FolderPath) in fileshare"
        New-AzureStorageDirectory   -ShareName $ShareName            `
                                    -Path $FolderPath                `
                                    -Context $StorageContext.Context `
                                    -ErrorAction SilentlyContinue #Todo check if folder already exits
    }

    # Copy file from blob storage container to fileshare
    Write-Output "Copy file $($blob.name) to fileshare"
    Start-AzureStorageFileCopy  -SrcContainerName $ContainerName    `
                                -SrcBlobName $blob.name             `
                                -DestShareName $ShareName           `
                                -DestFilePath $FilePath             `
                                -Context $StorageContext.Context    `
                                -DestContext $StorageContext.Context     


    # Delete file from blob storage container
    Write-Output "Remove blob $($blob.name) from blob container"
    Remove-AzStorageBlob -Context $StorageContext -Blob $blob.name -Container $ContainerName
}

I tried adding .Context after the context parameters and it shows IStorageContext instead of AzureStorageContext, but got the same error enter image description here

And the strange thing is that 3 weeks ago it worked without errors and now I got this error. Not sure what has changed in the meanwhile

标签: azurepowershellazure-storageazure-blob-storage

解决方案


您不能将新Az命令与旧AzureRm命令混合在一起。您应该注意,不仅是Az.StorageandAzure.Storage模块,还有其他模块,它们会给出不同的错误。我曾多次遇到这种情况。

因此,在您的情况下,重新打开一个新的 powershell 会话,使用Start-AzStorageFileCopyandNew-AzStorageDirectory而不是Start-AzureStorageFileCopyand New-AzureStorageDirectory

或者,如果您不想更改脚本,您可以重新打开一个新的 powershell 会话,Enable-AzureRmAlias在所有命令之前运行,它也可以工作。


推荐阅读