我在本地设置 dynamodb 以使用我的 Node 应用程序进行测试。为了设置它,我只是简单地从here复制了代码并根据我的需要进行了调整。
这是代码:
var AWS = require("aws-sdk");
var config = ({
"apiVersion": "2012-08-10",
"accessKeyId": "abcde",
"secretAccessKey": "abcde",
"region": "us-west-2",
"endpoint": "http://localhost:8001",
});
var dynamodb = new AWS.DynamoDB(config);
var params = {
TableName : "Movies",
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
dynamodb.createTable(params, 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": "Missing credentials in config",
"code": "CredentialsError",
"time": "2017-04-10T11:45:26.748Z",
"retryable": true,
"originalError": {
"message": "Could not load credentials from any providers",
"code": "CredentialsError",
"time": "2017-04-10T11:45:26.747Z",
"retryable": true,
"originalError": {
"message": "Connection timed out after 1000ms",
"code": "TimeoutError",
"time": "2017-04-10T11:45:26.747Z",
"retryable": true
}
}
}
将不胜感激任何帮助!
我也有同样的错误。就我而言,问题是它在本地从命令行运行得很好,但在 docker 中却不行。
基本上,如何在
aws-sdk
(版本 2)和 @aws-sdk/s3-client
(版本 3)之间将凭证传递到 AWS npm S3 客户端已发生变化。在 v2 的情况下,结构是这样的:
const credentials = {
region: configuration.aws.region,
accessKeyId: configuration.aws.accessKeyId,
secretAccessKey: configuration.aws.secretAccessKey
};
在 v3 中:
const credentials = {
region: configuration.aws.region,
credentials: {
accessKeyId: configuration.aws.accessKeyId,
secretAccessKey: configuration.aws.secretAccessKey
}
};
它在本地运行良好,因为我已经通过 CLI 向 AWS 进行了身份验证,并且它忽略了我传递给 v3 客户端的那些 v2 样式凭证。
结论:男孩和女孩的一切都使用 Typescript!
更改为以下代码,有虚拟
accessKeyId
和secretAccessKey
,然后运行它
var AWS = require("aws-sdk");
AWS.config.update({
region: 'us-west-1',
accessKeyId: 'accessKeyId',
secretAccessKey: 'secretAccessKey',
endpoint: new AWS.Endpoint('http://localhost:8000'),
});
var dynamodb = new AWS.DynamoDB();
var params = {
TableName : "Movies",
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
dynamodb.createTable(params, 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));
}
});
这对我有用
const sqsClient = new SQSClient({
region: REGION,
credentials: {
accessKeyId: 'XXXXXXXXXXXXXXX',
secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXX'
}
});
根据错误消息,您的凭据未在
config
中设置。
我给出了设置凭据然后使用服务的观点。
const aws = require('aws-sdk');
aws.config = new aws.Config();
aws.config.accessKeyId = "xxxxxxxxxxxxxx";
aws.config.secretAccessKey = "xxxxxxxxxx";
aws.config.region = "region";
现在使用Dynamodb
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
更新
您还可以使用文件设置凭据
aws.config.loadFromPath('./AwsConfig.json');
希望它有效!
显然我找到了问题所在。使用 json 文件设置凭据仍然会导致错误。仅使用不带 -inMemory 标志的配置对象也会导致错误。在脚本中对凭据进行硬编码并在本地启动 dynamodb 时使用 -inMemory 标志的组合似乎可以解决此问题。虽然我真的不明白为什么。
此问题的另一个选项/解决方案是在用户家中创建一个
credentials
文件。
类似:
~/.aws/credentials
内容将是:
[default]
aws_access_key_id = *************************
aws_secret_access_key = *********************************************
aws_session_token =**************************************************
DynamoDB 本地在 端口 8000 上使用虚拟凭证对我来说工作得很好。
注意:
端口号是8000。我使用以下虚拟凭据。
var creds = new AWS.Credentials('akid', 'secret', 'session');
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000",
credentials: creds
});
启动命令:-
java -Djava.library.path=DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
操作系统:-
Windows
在node.js中,您可以通过设置这些值轻松地从
.env
值加载
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN(可选)