【问题标题】:Api stop working using nodejs and dynamodbApi 停止使用 nodejs 和 dynamodb 工作
【发布时间】:2020-08-18 21:56:40
【问题描述】:

我正在使用 aws dynamodb 开发基于 nodejs 的 api。 API 工作正常,但今天我收到此错误:

{
    "success": false,
    "message": {
        "message": "Missing region in config",
        "code": "ConfigError",
        "time": "2020-08-17T19:17:17.462Z"
    }
}

下面是我的用户 api 路由:

const AWS = require('aws-sdk');
const config = require('config');
const { v4: uuidv4 } = require('uuid');
// Set the region
AWS.config.update({ region: 'us-east-2' });

router.post(`/api/user`, (req, res) => {
    console.log('User Add Services Called');

    try {
        //Constants
        AWS.config.update(config.aws_remote_config);

        var params = {
            TableName: config.aws_table_name,
            Item: {
                PK: `${uuidv4()}`,
                SK: `User`,
                name: 'Test'
            }
        };

        //Calling DynamoDB to add the item to the table
        docClient.put(params, function (err) {
            if (err) {
                res.send({
                    success: false,
                    message: err
                });
            } else {
                res.send({
                    success: true,
                    message: 'User Added'
                });
            }
        });
    } catch (error) {
        return res.status(500).json({
            status: 'error',
            message: 'An error occurred trying to process your request'
        });
    }
});

有时也会显示这些错误:

  • 连接 ENETUNREACH 169.254.169.254:80
  • 配置中缺少凭据,如果使用 AWS_CONFIG_FILE,请设置 AWS_SDK_LOAD_CONFIG=1

谁能告诉它为什么会发生,API 工作正常,我也尝试过重新生成访问密钥,但同样的问题来了

【问题讨论】:

  • 还有什么解决方案吗?
  • 使用 api 几天后遇到同样的问题。

标签: node.js api express amazon-dynamodb aws-sdk


【解决方案1】:

我认为您正在重新配置 AWS.config,它会覆盖包含区域初始化的初始配置更新。试试这个:

const AWS = require('aws-sdk');
const config = require('config');
const { v4: uuidv4 } = require('uuid');

// Set the region
AWS.config.update({ region: 'us-east-2', ...config.aws_remote_config });

router.post(`/api/user`, (req, res) => {
    console.log('User Add Services Called');

    try {
        var params = {
            TableName: config.aws_table_name,
            Item: {
                PK: `${uuidv4()}`,
                SK: `User`,
                name: 'Test'
            }
        };

        //Calling DynamoDB to add the item to the table
        docClient.put(params, function (err) {
            if (err) {
                res.send({
                    success: false,
                    message: err
                });
            } else {
                res.send({
                    success: true,
                    message: 'User Added'
                });
            }
        });
    } catch (error) {
        return res.status(500).json({
            status: 'error',
            message: 'An error occurred trying to process your request'
        });
    }
});

编辑

那么这意味着您的凭据丢失,只需执行以下操作:


// ... Other code

// Set the region
AWS.config.update({ 
    ...config.aws_remote_config,
    region: 'us-east-2',
    credentials: {
        accessKeyId: 'your-access-key',
        secretAccessKey: 'your-secret-key'
    } });

// ... Other code

【讨论】:

  • 如果我这样做了,那么会出现以下响应:缺少配置中的凭据,如果使用 AWS_CONFIG_FILE,请设置 AWS_SDK_LOAD_CONFIG=1
  • 我已经在数组中传递了所有这些。如果顺序是问题,那么它不应该从一开始就工作。我的问题仍然是一样的,为什么它停止工作?之前工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 2011-08-22
  • 2012-11-19
  • 2015-06-28
相关资源
最近更新 更多