错误:请求中包含的安全令牌无效 - AWS DynamoDB

时间:2017-03-11 02:51:37

标签: javascript amazon-web-services amazon-s3 amazon-dynamodb

该文件存储在.js脚本中,位于AWS S3中。

AWS.config.update({
  region: "us-west-2",
  //endpoint: 'dynamodb.us-west-2.amazonaws.com',
  accessKeyId: "name",
  secretAccessKey: "pass"
});

var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var params = {
    TableName : "table_name",
    ProjectionExpression:"company, link, budget",
    KeyConditionExpression: "company = :Adidas"
};
dynamodb.query(params, function (err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

我正在尝试从DynamoDB获取数据,而是得到这个:

  

错误:请求中包含的安全令牌无效。       在Request.extractError(aws-sdk.js:96980)       在Request.callListeners(aws-sdk.js:98762)       在Request.emit(aws-sdk.js:98736)       在Request.emit(aws-sdk.js:97899)       在Request.transition(aws-sdk.js:97626)       at AcceptorStateMachine.runTo(aws-sdk.js:101148)       在aws-sdk.js:101160       在请求。 (AWS-sdk.js:97642)       在请求。 (AWS-sdk.js:97901)       在Request.callListeners(aws-sdk.js:98772)“UnrecognizedClientException:安全令牌包含在   请求无效。       在Request.extractError(https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:96980:27)       在Request.callListeners(https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:98762:20)       在Request.emit(https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:98736:10)       在Request.emit(https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:97899:14)       在Request.transition(https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:97626:10)       at AcceptorStateMachine.runTo(https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:101148:12)       在https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:101160:10       在请求。 (https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:97642:9)       在请求。 (https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:97901:12)       在Request.callListeners(https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.22.0/aws-sdk.js:98772:18)“

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

错误消息表明凭据有问题。将凭证嵌入脚本中的局部变量会带来安全风险。请尝试使用其中一个recommended methods配置您的客户端凭据,按优先顺序递减:

  1. AWS身份和访问管理(IAM)角色(例如,在EC2实例配置文件或Lambda执行角色中)
  2. 共享凭证文件(〜/ .aws / credentials)
  3. 环境变量
  4. 磁盘上的JSON文件