azure - 无法将 AzureStorageContext 转换为 IStorageContext

标签 azure powershell azure-storage azure-blob-storage

我想要将文件从 Azure Blob 存储容器复制到 Azure 文件共享(在同一 Azure 存储帐户中)。

使用 Start-AzureStorageFileCopy 或 New-AzureStorageDirectory 时,我在上下文参数上收到错误:

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
}

我尝试在上下文参数后添加 .Context ,它显示 IStorageContext 而不是 AzureStorageContext,但得到了相同的错误 enter image description here

奇怪的是,三周前它运行没有错误,但现在我遇到了这个错误。不确定同时发生了什么变化

最佳答案

您无法混合新的 Az旧命令 AzureRm一起指挥。你应该注意,不仅仅是 Az.StorageAzure.Storage模块,还有其他模块,它们会给出不同的错误。这种情况我遇到过好几次了。

因此,在您的情况下,重新打开一个新的 powershell session ,使用 Start-AzStorageFileCopy New-AzStorageDirectory 而不是 Start-AzureStorageFileCopyNew-AzureStorageDirectory .

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

关于azure - 无法将 AzureStorageContext 转换为 IStorageContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60284093/

相关文章:

azure - 在文件共享中复制文件时出现 Azure 数据工厂配置问题

azure - 同步两个 azure 虚拟机时要排除哪些文件?

database - 谷歌应用引擎: Which is its RDBMS?

azure - 如何在 Azure 中从 GCP 设置类似的识别感知代理

Azure Service Fabric MultiMachine Windows X509 群集 - 等待计算机 vm1 的安装程序服务完成时超时

shell - 在 PowerShell 中添加数字

azure - 如何将 Azure 虚拟机移动到另一个存储帐户?

git - 远程计算机上 git 中的多个包文件

windows - Chocolatey 和 powershell : pin and unpin programs from task bar and file associations

windows - Powershell 使用指定的输入数据创建大型文件