javascript 我如何在 Node 中使用 Amazon 的 Dynamodb Local?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19868938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:05:31  来源:igfitidea点击:

How I can work with Amazon's Dynamodb Local in Node?

javascriptnode.jsamazon-web-servicesamazon-dynamodb

提问by danmcc

Amazon offers a local simulator for their Dynamodb productbut the examples are only in PHP.

亚马逊为其 Dynamodb 产品提供了本地模拟器,示例仅使用 PHP 编写

These examples mention passing the parameter "base_url" to specify that you're using a local Dynamodb, but that returns this error in Node:

这些示例提到传递参数“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 }

How do I get Dynamodb_local working in Node?

如何让 Dynamodb_local 在 Node 中工作?

回答by aaaristo

You should follow this blog postto setup your DynamoDB Local, an then you can simply use this code:

您应该按照这篇博文设置您的 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);
});

回答by Arshad

For Node please do as below:

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

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

Ensure that DynamodDB is running on port 8000.

确保 DynamodDB 在端口 8000 上运行。

回答by rynop

Here is how I do it, same code works local or inside AWS.

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

Simply leverage existence of env var DYNAMO_LOCAL_ENDPT="http://localhost:8000"

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

回答by carlsborg

This blog posthas an end to end example for using DynamoDB local with Node on Ubuntu, including setting up docker to run dynamoDB local in a container and sample code for calling various DDB operations.

这篇博文提供了在 Ubuntu 上使用 Node 本地 DynamoDB 的端到端示例,包括设置 docker 以在容器中本地运行 dynamoDB 以及调用各种 DDB 操作的示例代码。