2

我在本地运行我的 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;

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

4

1 回答 1

4

该错误表明缺少凭据,因此这是一个身份验证问题,您可以尝试直接在 SSM 构造函数上设置accessKeyIdandsecretAccessKey或字段。credentials

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

// 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'
    }
});
于 2020-08-14T22:12:48.043 回答