从本地反应应用程序调用时,AWS凭证错误“配置中缺少凭证,如果使用AWS_CONFIG_FILE,则设置AWS_SDK_LOAD_CONFIG = 1”

时间:2020-08-14 21:49:29

标签: node.js reactjs amazon-web-services aws-ssm

在本地运行我的react应用程序时出现以下错误,该应用程序通过AWS-SDK向AWS发出api请求:

CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

我尝试过:

这是我发出请求的方式:

import AWS from 'aws-sdk';
import { useState, useEffect } from 'react';

const ssm = new AWS.SSM({ region: 'eu-west-1' });

export const useFetchParams = (initialValue) => {
    const [result, setResult] = useState(initialValue);

    useEffect(() => {
        const params = {
            Path: '/',
            MaxResults: '2',
            Recursive: true,
            WithDecryption: true
        };

        ssm.getParametersByPath(params, function (err, data) {
            if (err) console.log(err, err.stack);
            else setResult(data);
        });
    }, []);

    return result;
};

export default useFetchParams;

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

该错误表明凭据丢失,因此这是身份验证问题,您可以尝试直接在SSM构造函数上设置accessKeyIdsecretAccessKeycredentials字段。

因此只需按原样维护您的代码,只需进行以下更改:

// From
const ssm = new AWS.SSM({ region: 'eu-west-1' });

// To
const ssm = new AWS.SSM({
    region: 'eu-west-1',
    accessKeyId: 'your-access-key',
    secretAccessKey: 'your-secret-key'
});

// Or To
const ssm = new AWS.SSM({
    region: 'eu-west-1',
    credentials: {
        accessKeyId: 'your-access-key',
        secretAccessKey: 'your-secret-key'
    }
});