【问题标题】:AWS dynamodb endpoints is not correctAWS dynamodb 端点不正确
【发布时间】:2019-03-10 14:24:18
【问题描述】:

我正在使用 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”,“原始错误”:{ "message": "无法从任何提供者加载凭据", "errno": "ETIMEDOUT", "code": "CredentialsError", “系统调用”:“连接”, “地址”:“169.254.169.254”, “端口”:80, "时间": "2018-10-05T05:05:26.001Z", “原始错误”:{ "errno": "ETIMEDOUT", “代码”:“ETIMEDOUT”, “系统调用”:“连接”, “地址”:“169.254.169.254”, “端口”:80, “消息”:“连接 ETIMEDOUT 169.254.169.254:80” } } }

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

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' region.", "code": "UnknownEndpoint", "region": "ap-southeast-2",
“主机名”:“arn”,“可重试”:true,“originalError”:{ “消息”:“getaddrinfo ENOTFOUND arn arn:443”, "errno": "ENOTFOUND", "code": "NetworkingError", “系统调用”:“getaddrinfo”, “主机名”:“arn”, “主机”:“arn”, “端口”:443, “地区”:“ap-southeast-2”, “可重试”:是的, “时间”:“2018-10-05T06:17:21.352Z”},“时间”:“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.js amazon-dynamodb sails.js


    【解决方案1】:

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

       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));
           }
       });
    

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

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

    【讨论】:

    • 谢谢你的回答,AWS的文档太混乱了!!!
    猜你喜欢
    • 1970-01-01
    • 2019-05-29
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多