我有一个本地运行的DynamoDB。我已经使用JavaScript控制台设置了我的表格,它们从那里列出得很好。
我还可以在JavaScript控制台中放置和获取我的表格中的项目:
var params = { TableName:"environmentId", Item: { environmentId: {"S":"a4fe1736-98cf-4560-bcf4-cc927730dd1b"} }};
dynamodb.putItem(params, function(err, data) {
console.log("put : err was " + JSON.stringify(err) + " and data is " + JSON.stringify(data));
});
输出 put : err was null and data is {}
,我认为这是“成功”,因为
params = { "Key":{"environmentId":{"S":"a4fe1736-98cf-45e0-bcf4-cc927730dd1b"}},"TableName":"environmentId"}
dynamodb.getItem(params, function(err, data) {
console.log("get : err was " + JSON.stringify(err) + " and data is " + JSON.stringify(data));
});
打印出get: err was null and data is {"Item":{"environmentId":{"S":"a4fe1736-98cf-45e0-bcf4-cc927730dd1b"}}}
,即它检索刚刚放入表格的对象。
但是,如果启动节点REPL并键入:
var AWS = require('aws-sdk');
AWS.config.loadFromPath("./config/credentials.js");
endpoint = new AWS.Endpoint("http://localhost:8000");
var dynamoOpts = {apiVersion: '2012-08-10', 'endpoint':endpoint};
var dynamodb = new AWS.DynamoDB(dynamoOpts);
var params = { TableName:"environmentId", Item: { environmentId: {"S":"a4fe1736-98cf-4560-bcf4-cc927730dd1b"} }};
dynamodb.putItem(params, function(err, data) {
console.log("put : err was " + JSON.stringify(err) + " and data is " + JSON.stringify(data));
}
我遇到了一个资源未找到的错误:{ "message":"Cannot do operations on a non-existent table",
"code":"ResourceNotFoundException",
"time":"2015-04 10T10:01:26.319Z",
"statusCode":400,
"retryable":false,
"retryDelay":0
}
从putCommand返回的ASW.request对象具有正确的端点:
{ protocol: 'http:',
host: 'localhost:8000',
port: 8000,
hostname: 'localhost',
pathname: '/',
// etc.
我的Node应用程序发生了同样的事情,但是连接到真实的AWS托管DynamoDB的相同代码却可以正常工作。