使用 Set-AzureStorageBlobContent 或 ConvertTo-Json cmdlet 将文件上传到 Azure 存储 blob 时能否指定编码类型?

Can you specify encoding type when uploading a file to Azure storage blob using Set-AzureStorageBlobContent or ConvertTo-Json cmdlet?

我有一个 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

请指教!

当然可以,试试下面的命令。

Set-AzureStorageBlobContent -Context $context -Container "111" -File "C:\Users\joyw\Desktop\testjson.json" -Properties @{"ContentEncoding" = "UTF-8"} -Force

捕获powershell的请求,你会发现x-ms-blob-content-encoding: UTF-8.

找出解决方案:

$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。