我如何在Node中使用亚马逊的Dynamodb Local?

dan*_*mcc 36 javascript amazon-web-services node.js amazon-dynamodb

亚马逊为其Dynamodb产品提供本地模拟器,示例仅在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 }
Run Code Online (Sandbox Code Playgroud)

如何让Dynamodb_local在Node中工作?

aaa*_*sto 50

您应该按照此博客文章来设置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);
});
Run Code Online (Sandbox Code Playgroud)

  • 对于像我一样刚接触AWS的其他人,在第1行之后我需要添加`AWS.config.update({accessKeyId:"myKeyId",secretAccessKey:"secretKey",region:"us-east-1"});` (7认同)
  • 我建议您使用环境变量(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_REGION)而不是使用AWS.config.update.http://aws.amazon.com/developers/getting-started/nodejs/ (2认同)

小智 5

对于节点,请执行以下操作:

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
});
Run Code Online (Sandbox Code Playgroud)

确保 DynamodDB 在端口 8000 上运行。