首页 > 解决方案 > 在使用 Set-AzureStorageBlobContent 或 ConvertTo-Json cmdlet 将文件上传到 Azure 存储 blob 时,是否可以指定编码类型?

问题描述

我有一个 PowerShell 脚本,它下载存储在 Azure 存储帐户 blob 中的 JSON 文件。此文件采用 UTF-8 编码。然后,该脚本从 JSON 中读取数据,进行更改,创建一个具有相同名称的新 JSON 文件,然后使用 Set-AzureStorageBlobContent cmdlet 将其上传回存储帐户。但是,所有使用该 JSON 文件的应用程序都停止了工作。经过数小时的故障排除后,我注意到当它将新的 JSON 上传回存储容器(替换现有的)时,它会以 UTF-16 编码上传 JSON。

Set-AzureStorageBlobContent cmdlet 中是否有可以指定编码的参数?我查看了官方文档,但找不到答案。

在我上传新的 JSON 之前,所有的值都存储在一个变量中,我实际上使用 cmdlet ConvertTo-Json 来生成新的 JSON 文件。ConvertTo-Json 中是否有参数来指定编码类型?

现在,我用来上传文件的只是:

$jsonContent | ConvertTo-Json -Depth 4 | Out-File C:\P3\myFile.json

Set-AzureStorageBlobContent -Context $storageContext -Container "myContainer" -File "myFile.JSON"  -Force

请指教!

标签: jsonazureblobazure-powershellazure-storage-account

解决方案


想出了解决办法:

$JSONConvert = $jsonContent | ConvertTo-Json -Depth 4
$JSONEncode = [System.Text.UTF8Encoding]::new($false) 
[System.IO.File]::WriteAllLines('C:\P3\myFile.JSON',$JSONConvert ,$JSONEncode)

Set-AzureStorageBlobContent -Context $storageContext -Container "myContainer" -File "myFile.JSON" -Properties @{"ContentEncoding" = "UTF-8"} -Force

这会将 UTF-8 编码的 JSON 文件上传到 blob。


推荐阅读