无法将json文件上传到AWS CloudWatch

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法将json文件上传到AWS CloudWatch相关的知识,希望对你有一定的参考价值。

我试图通过替换其中的内容在aws cloudwatch上上传json文件,作为回报,我将该文件上传到cloudwatch。

[步骤1:-我正在Windows机器上使用AWS cli(AWS cloudwatch get-dashboard *)保存cloudwatch仪表板(JSON格式)。

Step2:-替换文件的内容并按原样保存。

Step3:-我正在将新修改的文​​件(在json中)上传到cloudwatch,但上传失败。

[当我查看未以JSON格式显示的文件时,Windows将其视为字符串。

cli我使用为:

aws cloudwatch get-dashboard --dashboard-name SIT  --region eu-west-1 > SIT.json

输出显示如下:


    "DashboardName": "SIT",
    "DashboardArn": "arn:aws:cloudwatch::123456789:dashboard/SIT",
    "DashboardBody": "\"widgets\":[\"type\":\"metric\",\"x\":0,\"y\":0,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"sit/Default\",\"Memory Available\",\"Instance\",\"i-04996767d0822cccf\"]]
,\"view\":\"timeSeries\",\"stacked\":false,\"region\":\"eu-west-1\",\"title\":\"RT1 Available Memory\",\"period\":300,\"type\":\"metric\",\"x\":0,\"y\":6,\"width\":12,\"height\":6,\"properties\":\"met
rics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-04996767d0822cccf\"]],\"view\":\"timeSeries\",\"stacked\":false,\"region\":\"eu-west-1\",\"title\":\"RT1 CPU Utilization\",\"period\":300,\"ty
pe\":\"metric\",\"x\":0,\"y\":18,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-092fcbe0dd0eeceb5\"]],\"view\":\"timeSeries\",\"stacked\":false,\
"title\":\"SQL Primary CPU Utilization\",\"region\":\"eu-west-1\",\"period\":300,\"type\":\"metric\",\"x\":12,\"y\":12,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"AWS/EC2\",\"CPUUtilizati
on\",\"InstanceId\",\"i-045c7346c36f1cc3d\"]],\"view\":\"timeSeries\",\"stacked\":false,\"region\":\"eu-west-1\",\"title\":\"Batch CPU Utilization\",\"period\":300,\"type\":\"metric\",\"x\":0,\"y\":12,
\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"sit/Default\",\"Memory Available\",\"Instance\",\"i-045c7346c36f1cc3d\"]],\"view\":\"timeSeries\",\"stacked\":false,\"title\":\"Batch Available Me
mory\",\"region\":\"eu-west-1\",\"period\":300,\"type\":\"metric\",\"x\":12,\"y\":18,\"width\":12,\"height\":6,\"properties\":\"view\":\"timeSeries\",\"stacked\":false,\"metrics\":[[\"AWS/EC2\",\"CPUU
tilization\",\"InstanceId\",\"i-092fcbe0dd0eeceb5\"]],\"region\":\"eu-west-1\",\"title\":\"SQL Secondary CPU Utilization\",\"type\":\"metric\",\"x\":0,\"y\":24,\"width\":12,\"height\":6,\"properties\":
\"metrics\":[[\"sit/Default\",\"Memory Available\",\"Instance\",\"i-03b623e8400e4b801\"]],\"view\":\"timeSeries\",\"stacked\":false,\"title\":\"SAM Available Memory\",\"region\":\"eu-west-1\",\"period\":
300,\"type\":\"metric\",\"x\":12,\"y\":24,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-03b623e8400e4b801\"]],\"view\":\"timeSeries\",\"stack
ed\":false,\"title\":\"SAM CPU Utilization\",\"region\":\"eu-west-1\",\"period\":300,\"type\":\"metric\",\"x\":12,\"y\":0,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"sit/Default\",\"Memor
y Available\",\"Instance\",\"i-0e82d201d3fac0d2d\"]],\"view\":\"timeSeries\",\"stacked\":false,\"title\":\"RT2 Available Memory\",\"region\":\"eu-west-1\",\"period\":300,\"type\":\"metric\",\"x\":12,\"
y\":6,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-0e82d201d3fac0d2d\"]],\"view\":\"timeSeries\",\"stacked\":false,\"title\":\"RT2 CPU Utilizat
ion\",\"region\":\"eu-west-1\",\"period\":300]"

我尝试使用“ ConvertTo-Json”,但仍然无法上传。

我得到如下错误:

An error occurred (InvalidParameterInput) when calling the PutDashboard operation: The dashboard body is invalid, there are 1 validation errors: [  "dataPath": "", "message": "Should have required property 'widgets'"  ]

有人可以建议吗?

答案

似乎Dashboardody包含一个转义的JSON对象:

 "DashboardBody": "\" ...

您可以通过以下方式对特殊字符进行转义:

$invalidJsonString = @'

    "DashboardName": "SIT",
    "DashboardArn": "arn:aws:cloudwatch::123456789:dashboard/SIT",
    "DashboardBody": "\"widgets\":[\"type\":\"metric\",\"x\":0,\"y\":0,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"sit/Default\",\"Memory Available\",\"Instance\",\"i-04996767d0822cccf\"]]
,\"view\":\"timeSeries\",\"stacked\":false,\"region\":\"eu-west-1\",\"title\":\"RT1 Available Memory\",\"period\":300,\"type\":\"metric\",\"x\":0,\"y\":6,\"width\":12,\"height\":6,\"properties\":\"met
rics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-04996767d0822cccf\"]],\"view\":\"timeSeries\",\"stacked\":false,\"region\":\"eu-west-1\",\"title\":\"RT1 CPU Utilization\",\"period\":300,\"ty
pe\":\"metric\",\"x\":0,\"y\":18,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-092fcbe0dd0eeceb5\"]],\"view\":\"timeSeries\",\"stacked\":false,\
"title\":\"SQL Primary CPU Utilization\",\"region\":\"eu-west-1\",\"period\":300,\"type\":\"metric\",\"x\":12,\"y\":12,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"AWS/EC2\",\"CPUUtilizati
on\",\"InstanceId\",\"i-045c7346c36f1cc3d\"]],\"view\":\"timeSeries\",\"stacked\":false,\"region\":\"eu-west-1\",\"title\":\"Batch CPU Utilization\",\"period\":300,\"type\":\"metric\",\"x\":0,\"y\":12,
\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"sit/Default\",\"Memory Available\",\"Instance\",\"i-045c7346c36f1cc3d\"]],\"view\":\"timeSeries\",\"stacked\":false,\"title\":\"Batch Available Me
mory\",\"region\":\"eu-west-1\",\"period\":300,\"type\":\"metric\",\"x\":12,\"y\":18,\"width\":12,\"height\":6,\"properties\":\"view\":\"timeSeries\",\"stacked\":false,\"metrics\":[[\"AWS/EC2\",\"CPUU
tilization\",\"InstanceId\",\"i-092fcbe0dd0eeceb5\"]],\"region\":\"eu-west-1\",\"title\":\"SQL Secondary CPU Utilization\",\"type\":\"metric\",\"x\":0,\"y\":24,\"width\":12,\"height\":6,\"properties\":
\"metrics\":[[\"sit/Default\",\"Memory Available\",\"Instance\",\"i-03b623e8400e4b801\"]],\"view\":\"timeSeries\",\"stacked\":false,\"title\":\"SAM Available Memory\",\"region\":\"eu-west-1\",\"period\":
300,\"type\":\"metric\",\"x\":12,\"y\":24,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-03b623e8400e4b801\"]],\"view\":\"timeSeries\",\"stack
ed\":false,\"title\":\"SAM CPU Utilization\",\"region\":\"eu-west-1\",\"period\":300,\"type\":\"metric\",\"x\":12,\"y\":0,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"sit/Default\",\"Memor
y Available\",\"Instance\",\"i-0e82d201d3fac0d2d\"]],\"view\":\"timeSeries\",\"stacked\":false,\"title\":\"RT2 Available Memory\",\"region\":\"eu-west-1\",\"period\":300,\"type\":\"metric\",\"x\":12,\"
y\":6,\"width\":12,\"height\":6,\"properties\":\"metrics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-0e82d201d3fac0d2d\"]],\"view\":\"timeSeries\",\"stacked\":false,\"title\":\"RT2 CPU Utilizat
ion\",\"region\":\"eu-west-1\",\"period\":300]"

'@

$correctedJsonString = (($invalidJsonString | % [System.Text.RegularExpressions.Regex]::Unescape($_) ) -replace '"', '') -replace '"', '' 

$json = $correctedJsonString | ConvertFrom-Json

输出:

DashboardName DashboardArn                                DashboardBody
------------- ------------                                -------------
SIT           arn:aws:cloudwatch::123456789:dashboard/SIT @widgets=System.Obj…

如果您想在线玩游戏,可以使用此TIO link

您可以通过以下方式修复文件的无效JSON内容:

> ((Get-Content .\SIT.json | % [System.Text.RegularExpressions.Regex]::Unescape($_) ) -replace '"', '') -replace '"', '' | out-file .\SIT.json

>  gc .\SIT.json | ConvertFrom-Json

DashboardName DashboardArn                                DashboardBody
------------- ------------                                -------------
SIT           arn:aws:cloudwatch::123456789:dashboard/SIT @widgets=System.Object[]

另一答案

我设法使用“格式列表” powershell找到了解决方案;

Get-CWDashboard -DashboardName <SIT> | select DashboardBody | fl

上面的命令将仅选择DashboardBody并将输出格式化为json视图(仪表板主体的原始格式)。

现在,我可以将DashboardBody上传回cloudwatch仪表板。

以上是关于无法将json文件上传到AWS CloudWatch的主要内容,如果未能解决你的问题,请参考以下文章

无法将大型 graphql 模式上传到 aws appsync

尝试将元数据添加到对象时,AWS S3 管道中的“无效 JSON”

AWS Glue Crawler 将 json 文件分类为 UNKNOWN

Amazon S3 无法通过 Cloudfront 上传文件

如何将文件上传到 AWS 的云端

如何使用 AWS AppSync 将文件上传到 AWS S3