我想要将文件从 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,但得到了相同的错误
奇怪的是,三周前它运行没有错误,但现在我遇到了这个错误。不确定同时发生了什么变化
最佳答案
您无法混合新的 Az
旧命令 AzureRm
一起指挥。你应该注意,不仅仅是 Az.Storage
和Azure.Storage
模块,还有其他模块,它们会给出不同的错误。这种情况我遇到过好几次了。
因此,在您的情况下,重新打开一个新的 powershell session ,使用 Start-AzStorageFileCopy
和 New-AzStorageDirectory
而不是 Start-AzureStorageFileCopy
和New-AzureStorageDirectory
.
或者,如果您不想更改脚本,您可以重新打开一个新的 powershell session ,运行 Enable-AzureRmAlias
在您执行所有命令之前,它也会起作用。
关于azure - 无法将 AzureStorageContext 转换为 IStorageContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60284093/