這是我的代碼如下所示:失敗的NodeJS的憑據加載文件AWS
'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. ***什麼***錯誤? 2.這不是JSON,但是您的標題是*「NodeJS無法以JSON加載」*(大概是'securityParams.json'包含JSON--您認爲它是通過'require'處理的JavaScript代碼 - 但不是憑據)。 –
更新了我的問題,這聽起來像我不能以我想要的方式要求文件? – user2019182
看起來像loadFromPath試圖解析JSON文件,但憑據文件不是JSON。以下是使用AWS SDK時設置憑據的兩個資源。我個人更喜歡使用環境變量。 http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-environment http://docs.aws.amazon.com/sdk-for -java/v1/developer-guide/credentials.html –