Amazon SQS 队列的 Nestjs 凭据问题

Nestjs credential issue with Amazon SQS queue

我正在尝试将消息从我的 nestjs 应用程序发布到 amazon sqs 队列。

这是一个简单的 post 请求的片段,我正在使用来自 aws-sdk 的包:

import * as aws from 'aws-sdk';

--
--
--
--
--

@Post('dummySqs')
  async sendMessage(@Body() message: string) {
    const config = {
      apiVersion: '2022-02-21',
      accessKeyId: 'myaccess',
      accessSecretKey: 'mysecret',
      region: 'us-east-1',
      output: 'json',
    };
    aws.config.update(config);
    const sqs = new aws.SQS();
    const params = {
      MessageBody: 'Something about Daniel',
      QueueUrl: 'myurl',
    };
    sqs.sendMessage(params, (err, data) => {
      if (err) {
        console.log('ERRROR', err);
      } else {
        console.log('Success', data.MessageId);
      }
    });
  }

点击此 post 请求后,出现以下错误:

ERRROR Error [CredentialsError]: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
    at Timeout.connectTimeout [as _onTimeout] (/Users/danielongzh/Documents/poc/schedule-distributed-lock-service/node_modules/aws-sdk/lib/http/node.js:69:15)
    at listOnTimeout (internal/timers.js:555:17)
    at processTimers (internal/timers.js:498:7) {
  code: 'CredentialsError',
  time: 2022-02-21T13:31:40.708Z,
  retryable: true,
  originalError: {
    message: 'Could not load credentials from any providers',
    code: 'CredentialsError',
    time: 2022-02-21T13:31:40.707Z,
    retryable: true,
    originalError: {
      message: 'EC2 Metadata roleName request returned error',
      code: 'TimeoutError',
      time: 2022-02-21T13:31:40.707Z,
      retryable: true,
      originalError: [Object]
    }
  }
}

有什么解决办法吗?

试试这个:

import * as aws from 'aws-sdk';
import config from '../aws.json' 
as.config.update(config);

this.athena = new Athena({
    region: this.configService.get<IAwsCredentials>('AWS_ATHENA').AWS_REGION,
    accessKeyId: this.configService.get<IAwsCredentials>('AWS_ATHENA').AWS_ACCESS_KEY,
    secretAccessKey: this.configService.get<IAwsCredentials>('AWS_ATHENA').AWS_SECRET_KEY,
});