我正在尝试使用node.js从EC2实例在AWS上创建SQS队列. 在同一个实例中,我可以使用node.js访问S3并列出存储桶等.该实例具有IAM角色,可授予对S3和SQS的完全访问权限. 然而,执行以下代码失败了
在同一个实例中,我可以使用node.js访问S3并列出存储桶等.该实例具有IAM角色,可授予对S3和SQS的完全访问权限.
然而,执行以下代码失败了,我无法弄清楚原因.更有趣的是,从我的电脑中,代码实际上是有效的.
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()); });
怎么解释这个?
看起来这与您在Ubuntu服务器上安装的不同版本的节点与本地计算机有关.你在本地和服务器上运行什么节点–version?