【问题标题】:How I can work with Amazon's Dynamodb Local in Node?如何在 Node 中使用 Amazon 的 Dynamodb Local?
【发布时间】:2013-11-21 01:06:32
【问题描述】:

亚马逊提供local simulator for their Dynamodb productexamples 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 中工作?

【问题讨论】:

  • 您是否使用 aws-sdk 和这个 dynamo local 创建了一个表?

标签: javascript node.js amazon-web-services amazon-dynamodb


【解决方案1】:

您应该按照此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);
});

【讨论】:

  • 你可能也对github.com/aaaristo/dyngodb感兴趣,在这种情况下你可以简单地发出:dyngodb --local
  • 谢谢!看起来 PHP SDK 使用“base_url”,而 Node SDK 使用“endpoint”。
  • 对于像我一样刚接触 AWS 的其他人,在第 1 行之后我需要添加 AWS.config.update({ accessKeyId: "myKeyId", secretAccessKey: "secretKey", region: "us-east-1" });
  • 我建议您使用环境变量 (AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_REGION) 而不是使用 AWS.config.update。 aws.amazon.com/developers/getting-started/nodejs
  • 我无法使用此代码创建表。 dyn.createTable({ TableName: 'myTbl', AttributeDefinitions: [ { AttributeName: 'aaa', AttributeType: 'S' }, ], KeySchema:[ { AttributeName: 'aaa', KeyType: 'HASH' } ] }, function() { dyn.listTables(function(err, data) { console.log(data) }); });
【解决方案2】:

对于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 上运行。

【讨论】:

    【解决方案3】:

    这是我的做法,相同的代码在本地或 AWS 内部工作。

    简单地利用环境变量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']);
    }
    

    【讨论】:

      【解决方案4】:

      如果您将 Nodejs 与 Typescript 一起使用,则此代码可能无法正常工作。因为没有属性调用端点。

      AWS.config.update({
        accessKeyId: AWSaccessKeyId,
        secretAccessKey: AWSsecretAccessKey,  
        region: AWSregion,
        endpoint: AWSendpoint 
      });
      

      你可以使用任何一个,

      dyn= new AWS.DynamoDB({ endpoint: new AWS.Endpoint('http://localhost:8000') });
      

      AWS.config.dynamodb = {
        endpoint: new Endpoint('http://localhost:8000'),
        region: 'us-east-1'
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-05
        • 2023-03-30
        • 2016-03-12
        • 1970-01-01
        • 2020-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多