我正在尝试使用 loadFromPath 加载 AWS 的凭证并收到意外错误。使用 AWS.config.update 对相同的凭证进行硬编码可以正常工作。为了确保凭据文件的路径和格式正确,我使用 fs.readFile 加载了相同的文件,并且加载正确,因此似乎没有任何路径/权限问题。这似乎超级基本,但我一直在努力解决问题。谢谢你的帮助。
错误/输出:
Here: /home/ec2-user/.ec2/credentials.json
Got this through readFile: { access_id: 'XXXXXXX',
private_key: 'XXXXXXX',
keypair: 'praneethkey',
'key-pair-file': '/home/ec2-user/.ec2/praneethkey.pem',
region: 'us-west-2' }
/home/ec2-user/node_modules/aws-sdk/lib/config.js:221
if (err) throw err;
^
SyntaxError: Unexpected token <
at Object.parse (native)
at /home/ec2-user/node_modules/aws-sdk/lib/metadata_service.js:100:38
at IncomingMessage.<anonymous> (/home/ec2-user/node_modules/aws-sdk/lib/metadata_service.js:75:43)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
编码:
'use strict';
var AWS = require('aws-sdk');
var fs = require('fs');
var pathv = process.env.HOME + '/.ec2/credentials.json';
AWS.config.loadFromPath(pathv);
console.log('Here: ' + pathv);
fs.readFile(pathv, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
data = JSON.parse(data);
console.log("Got this through readFile:",data);