gpt4 book ai didi

javascript - 是否可以通过 javascript 通过读取 json 文件而不是使用参数来更新安全组?

转载 作者:太空宇宙 更新时间:2023-11-04 01:36:38 28 4
gpt4 key购买 nike

我想通过让我的 javascript 文件从单独的 json 文件读取信息来更新安全组规则,而不必使用 json 参数。

现在我已经使用 json 参数来更新安全组,我开始学习如何通过 javascript/node.js 读取文件来更新 json 文件。

这是我使用过的建议代码:

var AWS = require('aws-sdk');

AWS.config.update({region: 'us-east-1'});

var fs = require('fs');

var filename = 'sg-0136a8e42bc076309Ingress.json';

var ec2 = new AWS.EC2({apiVersion: '2016-11-15'});

ec2.updateSecurityGroupRuleDescriptionsIngress(filename, function(err, data) {
if (err) {
console.log("Failed to retrieve information", err);
return;
}
console.log("Information updated!");

fs.readFile(filename, (err, data) => {
if(err) console.log("Failed to output into file", err);
});

});

以下是 JSON 参数:

GroupId: "sg-0136a8e42bc076309",
IpPermissions: [
{
FromPort: 139,
IpProtocol: "tcp",
IpRanges: [
{
CidrIp: "0.0.0.0/0",
Description: "NetBIOS Session Service"
}
],
ToPort: 139
}
]

但是,结果我只收到一条错误消息:

Failed to retrieve information { MultipleValidationErrors: There were 34 validation errors:
* InvalidParameterType: Expected params to be a structure
* MissingRequiredParameter: Missing required key 'IpPermissions' in params

这是一个错误示例。如何通过让 javascript 读取 json 文件来更新安全组而不出现错误?

最佳答案

我认为您不应该将文件名写入变量 var filename = 'sg-0136a8e42bc076309Ingress.json' 并将其设置为 ec2.updateSecurityGroupRuleDescriptionsIngress 函数的参数。您可以只需要 JSON 文件并将其作为参数提供给函数

var file = require("./sg-0136a8e42bc076309Ingress.json");
ec2.updateSecurityGroupRuleDescriptionsIngress(file, function(err, data) {
if (err) {
console.log("Failed to retrieve information", err);
return;
}
console.log("Information updated!");
});

此外,还有一件事,在本例中,您的函数 fs.readFile 是无用的。

关于javascript - 是否可以通过 javascript 通过读取 json 文件而不是使用参数来更新安全组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54373114/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com