gpt4 book ai didi

node.js - AWS dynamodb 端点不正确

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:18 26 4
gpt4 key购买 nike

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

我收到的错误:

Unable to read item. Error JSON: { "message": "Missing credentials in config", "errno": "ETIMEDOUT", "code": "CredentialsError",
"syscall": "connect", "address": "169.254.169.254", "port": 80,
"time": "2018-10-05T05:05:26.002Z", "originalError": { "message": "Could not load credentials from any providers", "errno": "ETIMEDOUT", "code": "CredentialsError", "syscall": "connect", "address": "169.254.169.254", "port": 80, "time": "2018-10-05T05:05:26.001Z", "originalError": { "errno": "ETIMEDOUT", "code": "ETIMEDOUT", "syscall": "connect", "address": "169.254.169.254", "port": 80, "message": "connect ETIMEDOUT 169.254.169.254:80" } } }

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

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

然后出现以下错误:

Unable to read item. Error JSON: { "message": "Not Found", "code": "UnknownError", "statusCode": 404, "time": "2018-10-05T06:08:28.707Z", "retryable": 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"
});

错误是:

Unable to read item. Error JSON: { "message": "Inaccessible host: arn'. This service may not be available in theap-southeast-2' region.", "code": "UnknownEndpoint", "region": "ap-southeast-2",
"hostname": "arn", "retryable": true, "originalError": { "message": "getaddrinfo ENOTFOUND arn arn:443", "errno": "ENOTFOUND", "code": "NetworkingError", "syscall": "getaddrinfo", "hostname": "arn", "host": "arn", "port": 443, "region": "ap-southeast-2", "retryable": true, "time": "2018-10-05T06:17:21.352Z" }, "time": "2018-10-05T06:17:21.352Z" }

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

我正在关注此引用:https://docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html#GettingStarted.NodeJs.03.02

任何帮助将不胜感激。

最佳答案

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

   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。我在参数中传递电子邮件,因此这里我们需要传递表的主键值。

我已经通过了,它对我来说工作得很好。

关于node.js - AWS dynamodb 端点不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52659500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com