我正在将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:{“ message”:“缺少凭据 在配置中”,“ errno”:“ ETIMEDOUT”,“ code”:“ CredentialsError”,
“ syscall”:“连接”,“地址”:“ 169.254.169.254”,“端口”:80,
“ time”:“ 2018-10-05T05:05:26.002Z”,“ originalError”:{ “ message”:“无法从任何提供程序加载凭据”, “ errno”:“ ETIMEDOUT”, “ code”:“ CredentialsError”, “ syscall”:“连接”, “ address”:“ 169.254.169.254”, “端口”:80, “ time”:“ 2018-10-05T05:05:26.001Z”, “ originalError”:{ “ errno”:“ ETIMEDOUT”, “ code”:“ ETIMEDOUT”, “ syscall”:“连接”, “ address”:“ 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:{“ message”:“未找到”,“代码”: “ UnknownError”,“ statusCode”:404,“时间”: “ 2018-10-05T06:08:28.707Z”,“可重试”:false,“ retryDelay”: 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:{“ message”:“无法访问的主机:
arn'. This service may not be available in the
ap-southeast-2' 地区。“,”代码“:” UnknownEndpoint“,”地区“:” ap-southeast-2“,
“ hostname”:“ arn”,“ retryable”:true,“ originalError”:{ “ message”:“ getaddrinfo ENOTFOUND arn arn:443”, “ errno”:“ ENOTFOUND”, “ code”:“ NetworkingError”, “ syscall”:“ getaddrinfo”, “ hostname”:“ arn”, “ host”:“ arn”, “端口”:443, “ region”:“ ap-southeast-2”, “可重试”:是的, “ time”:“ 2018-10-05T06:17:21.352Z”},“ time”:“ 2018-10-05T06:17:21.352Z”}
谁能告诉我哪个是在dynamoDB中读取,创建,更新和删除数据的正确端点。
任何帮助将不胜感激。
答案 0 :(得分: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));
}
});
不需要端点即可通过accessKeyId
,secretAccessKey
和region
。我正在以参数形式传递电子邮件,因此在这里我们需要传递表的主键值。
我已经过了,对我来说也很好。