json - 在使用 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
请指教!
解决方案
想出了解决办法:
$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。
推荐阅读
- sparql - 如何使用实体名称使用 SPARQL 查询 Wikidata 并检查替代标签?
- php - Wordpress:nav_menu_link_attributes 不接受变量作为属性
- python - 获取绝对文件路径列表并忽略点目录/文件python
- flutter - GestureDetector onHorizontalDragUpdate 重复调用问题
- eclipse - 在 Eclipse 中设置服务器时无法添加项目
- postgresql - 如何在 Postgress 中获得给定月份的一周范围
- javascript - 如何将变量声明为函数并为其赋值
- javascript - (REACTJS)如何只在异步函数完成后渲染?
- javascript - 对于动态生成的字段,获取的数据不会改变
- typescript - 为什么在泛型类型上使用索引签名时,打字稿不能推断出正确的类型?