我正在尝试使用node.js从EC2实例在AWS上创建SQS队列。从同一实例,我可以使用node.js访问S3并列出存储桶等内容。该实例具有授予对S3和SQS的完全访问权限的IAM角色。
然而,执行下面的代码失败了,我无法弄清原因。更有趣的是,从我的个人计算机上,该代码实际上起作用了。
var AWS = require('aws-sdk');
AWS.config.update({region:'eu-central-1'});
//console.log("awsv "+AWS.VERSION); --> 2.3.5
var sqs = new AWS.SQS();
var params = {
QueueName: "MyQueue1"
};
sqs.createQueue(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
if(data)
console.log(data); // successful response
else
console.log("Other unknown error");
}
});
错误消息:
/home/ubuntu/aws-nodejs-sample/node_modules/aws-sdk/lib/http/node.js:121
callback();
^
TypeError: undefined is not a function
at Writable.writer._write (/home/ubuntu/aws-nodejs-sample/node_modules/aws-sdk/lib/http/node.js:121:7)
at Writable.write (_stream_writable.js:176:8)
at write (_stream_readable.js:481:24)
at Array.forEach (native)
at flow (_stream_readable.js:493:19)
at _stream_readable.js:458:7
at process._tickCallback (node.js:427:13)
at process._makeCallback (node.js:345:15)
事实证明,使用调用CLI函数的脚本进行快速测试是可行的:
var AWS = require('aws-sdk');
AWS.config.region = 'eu-central-1';
var qUrl="https://sqs.eu-central-1.amazonaws.com/......./awsquerytest";
var queue = new AWS.SQS({params: {QueueUrl: qUrl}}); // using url to queue
var receipthandle;
var fs = require('fs');
var child = require('child_process').exec('aws sqs receive-message --queue-url https://sqs.eu-central-1.amazonaws.com/....../awsquerytest');
child.stdout.on('data', function(data) {
var x= data.toString();
console.log(data.toString());
fs.writeFile("/home/ubuntu/aws-nodejs-sample/output",x,function(err){
if(err){
return console.log(err);
}
console.log("oldu");
});
});
child.stderr.on("data", function (data) {
console.log(data.toString());
});
这怎么解释呢?
new AWS.SQS({apiVersion: '2012-11-05'});
。 - John