我正在遵循 AWS Node.js 教程,但我无法让提供的代码正常工作。
具体来说,我正在尝试在 DynamoDB 的本地实例上创建一个"电影"表,并使用提供的一些示例数据加载它。但是,当我尝试创建表时,我收到了”Cannot do operations on a non-existent table”
,这似乎有点奇怪。
对于我的设置,我在一个控制台窗口中运行 DynamoDB。我正在使用和输出的命令如下:
COMPUTERNAME:node-dyna-demo myUserName$ java -Djava.library.path=./dynamodb_local_latest/DynamoDBLocal_lib -jar ./dynamodb_local_latest/DynamoDBLocal.jar -sharedDb
Initializing DynamoDB Local with the following configuration:
Port: 8000
InMemory: false
DbPath: null
SharedDb: true
shouldDelayTransientStatuses: false
CorsParams: *
在单独的控制台中,我正在执行以下代码:
AWS.config.update({
credentials: {
accessKeyId: ‘testAccessId’,
secretAccessKey: ‘testAccessKey’
},
region: 'us-west-2',
endpoint: 'http://localhost:8000'
})
const dynamodb = new AWS.DynamoDB()
const docClient = new AWS.DynamoDB.DocumentClient()
const dbParams = {
TableName : "Movies",
KeySchema: [ … ],
AttributeDefinitions: [ … ],
ProvisionedThroughput: { … }
}
dynamodb.createTable(dbParams, function(err, data) {
if (err) {
console.error(
'Unable to create table. Error JSON:',
JSON.stringify(err, null, 2)
)
} else {
console.log(
'Created table. Table description JSON:',
JSON.stringify(data, null, 2)
)
}
})
我从执行中得到的错误是:
Unable to create table. Error JSON: {
"message": "Cannot do operations on a non-existent table",
"code": "ResourceNotFoundException",
"time": "2018-01-24T15:56:13.229Z",
"requestId": "c8744331-dd19-4232-bab1-87d03027e7fc",
"statusCode": 400,
"retryable": false,
"retryDelay": 9.419252980728942
}
有谁知道此异常的可能原因?
当 Dynamodb local 在没有 -sharedDb 标志的情况下启动时,可能会出现此类问题。
在您的情况下,本地dynamodb按预期启动,
java -Djava.library.path=./dynamodb_local_latest/DynamoDBLocal_lib -jar ./dynamodb_local_latest/DynamoDBLocal.jar -sharedDb
请尝试以下解决方案,
解决方案 1:删除 AWS 配置中的凭证信息
AWS.config.update({
region: 'us-west-2',
endpoint: 'http://localhost:8000'
})
原因:如果 dynamodb 本地在没有 -sharedDb 标志的情况下运行,它将创建与该凭据相关的新数据库。
解决方案 2:使用 dynamodb 外壳 (http://localhost:8000/shell/#( 创建表,并使用以下命令验证是否创建
表AWS Dynamodb 列表表 --终端节点网址 http://localhost:8000
第一个答案给了我解决方案的提示。我正在将Java与maven结合使用。希望我的回答可以帮助其他人。AWS 区域可能冲突。Java 代码指向 eu-west-1。
amazonDynamoDB = AmazonDynamoDBClientBuilder
.standard()
.withEndpointConfiguration(
new AwsClientBuilder
.EndpointConfiguration("http://localhost:" +
LocalDbCreationRule.port, "eu-west-1"))
.build();
但是我的 AWS 配置在我的本地计算机上的主文件夹中
的位置~/.aws/config
显示以下内容:
[default]
region = eu-central-1
我在本地配置中将区域更改为 eu-west-1,与 Java 代码匹配,问题已解决。