来自 Node-aws 的 Dynamo Local:所有操作都失败 "Cannot do operations on a non-existent table"

Dynamo Local from Node-aws: all operations fail "Cannot do operations on a non-existent table"

我有一个本地 dynamo-db 运行。我已经使用 JavaScript 控制台设置了我的 tables,他们从那里列出了 OK。

我还可以从 JavaScript 控制台向我的 table 放置和获取物品:

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 {} 我假设是 "success" 因为

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"}}} 即它检索我刚放入 table 的对象。

但是,如果它启动节点 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 托管发电机的相同代码有效。

问题是 JavaScript 控制台和您的应用程序使用不同的配置文件(凭据和区域),因此本地 DynamoDB 将为它们创建单独的数据库文件。通过在启动本地 DynamoDB 时使用 -sharedDb 标志,将为所有客户端共享一个数据库文件。

来自doc

-sharedDb — DynamoDB Local will use a single database file, instead of using separate files for each credential and region. If you specify -sharedDb, all DynamoDB Local clients will interact with the same set of tables regardless of their region and credential configuration.

那些正在使用 the official DynamoDB Local Docker image 的人应该使用这一行来启动它以启用 sharedDb:

docker run -p 8000:8000 amazon/dynamodb-local -jar DynamoDBLocal.jar -inMemory -sharedDb

图像使用的原始 ENTRYPOINT and CMD 可以在 docker inspect amazon/dynamodb-local 输出中看到,并且是:

"Entrypoint": [
    "java"
]
"Cmd": [
    "-jar",
    "DynamoDBLocal.jar",
    "-inMemory"
]

所以我们基本上需要复制它们并添加-sharedDb

如果您使用的是 CircleCI,您的执行程序应该如下所示:

# using service containers on CircleCI
# https://circleci.com/docs/2.0/databases/
executors:
  with-dynamodb:
    docker:
      # image used to install source code,
      # run our server and run Cypress tests
      - image: cypress/base:14.17.3

      # image used to run Dynamodb in a separate container
      - image: amazon/dynamodb-local:1.17.0
        # HERE IS THE IMPORTANT PART
        command: ["-jar", "DynamoDBLocal.jar", "-inMemory", "-sharedDb"]

command 选项允许您覆盖 Dockerfile 中的默认值 CMD,@madhead 指出。