Weird Characters when reading Blob from Container

末鹿安然 提交于 2021-02-11 18:15:31

问题


I have some text files that contain either OK or FAIL written to a storage account. If I download the files to disk, the contents display as expected in notepad,

However, if I get the file contents to a variable and Write-Host that, I get �O K

I am doing this:

$storageContext = New-AzureStorageContext $storageAccountName $storageAccountKey
$storageContainer = "monitor"
$storageBlobs = Get-AzureStorageBlob -Container $storageContainer -Context    $storageContext

foreach($storageBlob in $storageBlobs) {
        $blobContents = $storageBlob.ICloudBlob.DownloadText()
        $blobName = $storageBlob.Name
        Write-Host $blobName
        Write-Host $blobContents
        Write-Host
  }

I thought that maybe I could tell it to use UTF8 like this:

$encoding = [System.text.Encoding]::UTF8

foreach($storageBlob in $storageBlobs) {
        $blobContents = $storageBlob.ICloudBlob.DownloadText($encoding)
        $blobName = $storageBlob.Name
        Write-Host $blobName
        Write-Host $blobContents
        Write-Host
  }

However, that doesn't work.

I'm now trying getting it as a ByteArray with .DownloadToByteArray but my first attempt is throwing errors.

Why are there extra characters and spaces when retrieved via Powershell but not when viewed in notepad. Am I correct in thinking this is a simple encoding issue?


回答1:


Yes, it is an encoding issue. If the file is encoding with UTF8, you should use UTF8 to decode it. If it's encoding with unicode, you should use unicode to decode it.

Then in your case, I think it's using unicode. You should use Unicode to decode it, like $encoding = [System.text.Encoding]::Unicode.

I can reproduce your issue, and test it with encoding UTF8(not work), but works with Unicode.

UTF8:

Unicode:



来源:https://stackoverflow.com/questions/54010577/weird-characters-when-reading-blob-from-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!