AWS& Node.js:使用Browserify

时间:2016-05-25 14:32:37

标签: javascript node.js amazon-web-services amazon-s3 browserify

我想在Javascript中使用aws-sdk。所以我用nam install安装了browserify和aws-sdk。在我写的终端

browserify project/project.js > browser-app.js

它为我创建了一个browser-app.js。现在我想在浏览器中运行这样的代码:

var AWS = require('aws-sdk');
var sqs = new AWS.SQS()
var params = {
                MessageBody: JSON.stringify(SOMEJson),
                QueueUrl: QueueURL
            };
sqs.sendMessage(params,function(err, data){
                if (err) console.log(err, err.stack);
                else     console.log(data);
            });

我的凭证文件位于〜/ .aws / config

然后我运行代码我收到以下错误消息:

Error: Missing credentials in config
at credError (http://localhost:63343/urfado-regex/browser-app.js:1018:40)
at Config.getCredentials (http://localhost:63343/urfado-regex/browser-app.js:1057:14)
at Request.VALIDATE_CREDENTIALS (http://localhost:63343/urfado-regex/browser-app.js:3111:26)
at Request.callListeners (http://localhost:63343/urfado-regex/browser-app.js:7214:18)
at Request.emit (http://localhost:63343/urfado-regex/browser-app.js:7190:10)
at Request.emit (http://localhost:63343/urfado-regex/browser-app.js:6042:14)
at Request.transition (http://localhost:63343/urfado-regex/browser-app.js:5449:10)
at AcceptorStateMachine.runTo (http://localhost:63343/urfado-regex/browser-app.js:9740:12)
at Request.runTo (http://localhost:63343/urfado-regex/browser-app.js:5821:15)
at Request.send (http://localhost:63343/urfado-regex/browser-app.js:5785:10) CredentialsError: Missing credentials in config
at credError (http://localhost:63343/urfado-regex/browser-app.js:1018:40)
at Config.getCredentials (http://localhost:63343/urfado-regex/browser-app.js:1057:14)
at Request.VALIDATE_CREDENTIALS (http://localhost:63343/urfado-regex/browser-app.js:3111:26)
at Request.callListeners (http://localhost:63343/urfado-regex/browser-app.js:7214:18)
at Request.emit (http://localhost:63343/urfado-regex/browser-app.js:7190:10)
at Request.emit (http://localhost:63343/urfado-regex/browser-app.js:6042:14)
at Request.transition (http://localhost:63343/urfado-regex/browser-app.js:5449:10)
at AcceptorStateMachine.runTo (http://localhost:63343/urfado-regex/browser-app.js:9740:12)
at Request.runTo (http://localhost:63343/urfado-regex/browser-app.js:5821:15)
at Request.send (http://localhost:63343/urfado-regex/browser-app.js:5785:10)

我还尝试使用aws.config对我的凭据进行硬编码,如下所示:

AWS.config.update({
                accessKeyId: "MYKEY",
                secretAccessKey: "MYSECRETKEY",
                "region": "eu-central-1"
        });

我仍然得到同样的错误。谁知道我能做错什么?

1 个答案:

答案 0 :(得分:0)

从浏览器上下文执行时,您可以使用以下选项进行身份验证

  • 使用Amazon Cognito对用户进行身份验证
  • 使用网络身份联合身份验证用户
  • 在您的申请中硬编码

确保你设置 AWS.config.update({ accessKeyId: "MYKEY", secretAccessKey: "MYSECRETKEY", "region": "eu-central-1" });

在实例化sqs对象之前

参考:http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring.html