首页 > 解决方案 > AWS dynamodb 端点不正确

问题描述

我正在使用sails js(最新版本 1.0)和 DynamoDB(我已经在我的本地机器上安装了 Sails js 框架)但是当试图从那时起读取记录时aws dynamodb它给出了错误。我尝试过以下情况,每次都会遇到不同的错误。

var AWS = require("aws-sdk");
AWS.config.update({
  region: "ap-southeast-2",
  endpoint: "http://localhost:1337"
});

var docClient = new AWS.DynamoDB.DocumentClient();
var table = "SMSGateway_User";
var email = 'yoursptc@gmail.com';

var params = {
    TableName: table,
    Key:{
        "userEmail": email
    }
};

docClient.get(params, function(err, data) {
    if (err) {
        console.log("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    }
});

我得到的错误:

无法读取项目。错误 JSON:{“消息”:“配置中缺少凭据”、“errno”:“ETIMEDOUT”、“代码”:“CredentialsError”、
“系统调用”:“连接”、“地址”:“169.254.169.254”、“端口”:80,
“时间”:“2018-10-05T05:05:26.002Z”,“originalError”:{“消息”:“无法从任何提供者加载凭据”,“errno”:“ETIMEDOUT”,“代码”:“凭据错误”、“系统调用”:“连接”、“地址”:“169.254.169.254”、“端口”:80、“时间”:“2018-10-05T05:05:26.001Z”、“原始错误” “:

当我更改并添加以下配置代码时:

AWS.config.update({
  accessKeyId: 'XXXXXXXXXXXX',
  secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
  region: "ap-southeast-2",
  endpoint: "http://localhost:1337"
});

然后得到以下错误:

无法读取项目。错误 JSON:{“消息”:“未找到”,“代码”:“未知错误”,“状态代码”:404,“时间”:“2018-10-05T06:08:28.707Z”,“可重试”:假, “重试延迟”:47.4917958614573}

当我更改端点并放入 ARN

AWS.config.update({
  accessKeyId: 'XXXXXXXXXXXX',
  secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
  region: "ap-southeast-2",
  endpoint: "arn:aws:dynamodb:ap-southeast-2:420344081058:table/SMSGateway_User"
});

错误是:

无法读取项目。错误 JSON:{“消息”:“无法访问的主机: arn'. This service may not be available in theap-southeast-2' 区域。”,“代码”:“UnknownEndpoint”,“区域”:“ap-southeast-2”,
“主机名”:“arn”, "retryable": true, "originalError": { "message": "getaddrinfo ENOTFOUND arn arn:443", "errno": "ENOTFOUND", "code": "NetworkingError", "syscall": "getaddrinfo", "hostname ”:“arn”,“主机”:“arn”,“端口”:443,“区域”:“ap-southeast-2”,“可重试”:true,“时间”:“2018-10-05T06:17 :21.352Z" }, "

谁能告诉我哪个是正确的端点来读取、创建、更新和删除 dynamoDB 中的数据。

我正在关注这个参考:https ://docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html#GettingStarted.NodeJs.03.02

任何帮助将不胜感激。

标签: node.jsamazon-dynamodbsails.js

解决方案


经过大量研究,我找到了为什么它不起作用的解决方案

   var AWS = require("aws-sdk");
   //AWS.config.loadFromPath('details.json');
   AWS.config.update({
     accessKeyId: 'XXXXXXXXXXXXXXXX',
     secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
     region: "XXXXXXXXX",
   });

   var docClient = new AWS.DynamoDB.DocumentClient();

/* Get single item */

   var table = "TABLE NAME";
   var msgId = 'PRIMARY KEY VALUE';
   var params = {
       TableName: table,
       Key:{
           'MsgId': msgId
       }
   };

   docClient.get(params, function(err, data) {
       if (err) {
           console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
       } else {
           console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
       }
   });

要获取表的所有记录,只需使用如下扫描功能

   var table = "PadmanStopsTest";
   var params = {
       TableName: table,
   };
   docClient.scan(params, function(err, data) {
       if (err) {
           console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
       } else {
           console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
       }
   });

不需要终点,只需要通过accessKeyId,secretAccessKeyregion. 我在参数中传递电子邮件,所以在这里我们需要传递表的主键值。

我已经通过了,它对我来说工作正常。


推荐阅读