Json NodeJS无法加载凭据文件AWS

Json NodeJS无法加载凭据文件AWS,json,node.js,amazon-web-services,aws-sdk,Json,Node.js,Amazon Web Services,Aws Sdk,这就是我的代码的样子: 'use strict'; process.env.AWS_PROFILE // Load the AWS SDK for Node.js const AWS = require('aws-sdk'); // Create EC2 service object var ec2 = new AWS.EC2({apiVersion: '2016-11-15'}); // Load credentials and set region from JSON file AW

这就是我的代码的样子:

'use strict';

process.env.AWS_PROFILE

// Load the AWS SDK for Node.js
const AWS = require('aws-sdk');

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

// Load credentials and set region from JSON file
AWS.config.loadFromPath('/Users/testuser/.aws/credentials');
// Load in security group parameters
const securityParams = require('./securityParams.json');

module.exports = {
    //Exports creation of Security Groups
    CreateSecurityGroup: (req, res) => {
        ec2.createSecurityGroup(securityParams, function(err, data) {
            if (err) {
                return (console.log("Error", err));
            }
            // Pass the Json as a parameter in this function
            ec2.authorizeSecurityGroupIngress(securityParams, function(err, data) {
                if (err) {
                    res.serverError(err, err.stack);
                } else {
                    res.ok(data);
                    console.log('Ingress Security Rules Created');
                }
            })
            // Pass the Json as a parameter in this function
            ec2.authorizeSecurityGroupEgress(securityParams, function(err, data) {
                if (err) {
                    res.serverError(err, err.stack);
                } else {
                    res.ok(data);
                    console.log('Egress Security Rules Created');
                }
            })
        })
    }
  }
我试图让脚本从两个文件加载配置;一个aws凭据文件和一个json。但是,它会在凭据文件上抛出错误,如下所示:

[default]
aws_access_key_id=**************
aws_secret_access_key**************
我不确定我遗漏了什么,以便让它正确读取属性

下面是我看到的错误:

undefined:1
[default]
 ^

SyntaxError: Unexpected token d in JSON at position 1
    at JSON.parse (<anonymous>)
未定义:1
[默认值]
^
SyntaxError:JSON中位置1处出现意外标记d
在JSON.parse()处

凭证是一个普通的Ascii文件,不是json文件

// Load credentials and set region from JSON file
AWS.config.loadFromPath('/Users/testuser/.aws/credentials');
您可以使用命令
file/Users/testuser/.aws/credentials

读取props文件并设置AWS配置的示例代码段

var PropertiesReader = require('properties-reader');
var AWS = require('aws-sdk')
var properties = PropertiesReader('/Users/username/.aws/credentials');

AWS.config.update({
        accessKeyId : properties.get('aws_access_key_id'),
        secretAccessKey : properties.get('aws_secret_access_key'),
        region : 'us-west-2'
})

console.log(AWS.config)

Ref:

“但是它在凭证文件上抛出错误”有两件事:1。什么错误?2.这不是JSON,但您的标题是“NodeJS无法加载JSON”(可能是
securityParams.JSON
包含JSON——您通过
require
将其视为JavaScript代码,但不是凭据)。更新了我的问题,所以听起来我不能像我尝试的那样要求文件?看起来loadFromPath试图解析JSON文件,但是凭证文件不是JSON。以下是关于使用AWS SDK时设置凭据的两个参考资料。我个人更喜欢使用环境变量。还有一个问题,为了真正读入JSON,我是否必须在代码中添加JSON解析?如果是,在哪里?是的,我现在知道了。你知道我如何将这些参数读入我正在编写的脚本中,而不改变文件的原样吗?是的,你必须使用属性读取器包并相应地更新配置。明白了,这很有帮助。还有一件事。现在模块导出中没有一个函数正在执行,基本上没有返回任何内容。我需要做些什么才能让它们运行?一点错误都没有。尝试了两件事,
AWS\u PROFILE=AWS admin node AWS\u Security\u Group.js
Yes它导出,但是req、res对象与api调用相关,因此您必须在该api中调用此函数。因此,如果希望独立运行此文件,只需删除对所有req、res和call module.exports.CreateSecurityGroup()的引用