亚马逊提供local simulator for their Dynamodb product,但examples are only in PHP。
这些示例提到传递参数“base_url”以指定您正在使用本地Dynamodb,但在Node中返回此错误:
{ [UnrecognizedClientException: The security token included in the request is invalid.]
message: 'The security token included in the request is invalid.',
code: 'UnrecognizedClientException',
name: 'UnrecognizedClientException',
statusCode: 400,
retryable: false }
如何让Dynamodb_local在Node中运行?
答案 0 :(得分:46)
您应该按照此blog post设置您的DynamoDB Local,然后您可以使用此代码:
var AWS= require('aws-sdk'),
dyn= new AWS.DynamoDB({ endpoint: new AWS.Endpoint('http://localhost:8000') });
dyn.listTables(function (err, data)
{
console.log('listTables',err,data);
});
答案 1 :(得分:0)
对于Node,请按以下步骤操作:
const AWS = require('aws-sdk');
const AWSaccessKeyId = 'not-important';
const AWSsecretAccessKey = 'not-important';
const AWSregion = 'local';
const AWSendpoint = 'http://localhost:8000' // This is required
AWS.config.update({
accessKeyId: AWSaccessKeyId,
secretAccessKey: AWSsecretAccessKey,
region: AWSregion,
endpoint: AWSendpoint
});
确保DynamodDB在端口8000上运行。
答案 2 :(得分:0)
这就是我的方法,相同的代码在本地或AWS内部运行。
仅利用env var DYNAMO_LOCAL_ENDPT="http://localhost:8000"
import { DynamoDB, Endpoint } from 'aws-sdk';
const ddb = new DynamoDB({ apiVersion: '2012-08-10' });
if (process.env['DYNAMO_LOCAL_ENDPT']) {
ddb.endpoint = new Endpoint(process.env['DYNAMO_LOCAL_ENDPT']);
}
答案 3 :(得分:0)
This blog post有一个端到端示例,该示例在Ubuntu上的Node上使用DynamoDB local,包括设置docker在容器中运行dynamoDB local的示例以及用于调用各种DDB操作的示例代码。