AWS SQS 的 JavaScript 使用方式

方法1: 

import {Injectable} from '@nestjs/common';
import {SQS} from 'aws-sdk';

@Injectable()
export class SqsService {
  private client: SQS;
  private awsAccessKey = process.env.AWS_ACCESS_KEY_ID;
  private awsSecretKey = process.env.AWS_SECRET_ACCESS_KEY;
  private awsRegion = process.env.AWS_REGION;

  constructor() {
    this.client = new SQS({
      credentials: {
        accessKeyId: this.awsAccessKey,
        secretAccessKey: this.awsSecretKey,
      },
      region: this.awsRegion,
      sslEnabled: true,
    });

    /**
     * The code below is used for testing if the SQS service works.
    */
    this.client.listQueues({}, (err, data) => {
      if (err) console.log(err, err.stack); // an error occurred
      else console.log(data); // successful response
    });
  }
}

方法2:

import {Injectable} from '@nestjs/common';
import {AWSError, SQS} from 'aws-sdk';
const AWS = require('aws-sdk');

@Injectable()
export class SqsService {
  private client: SQS;
  private awsAccessKey = process.env.AWS_ACCESS_KEY_ID;
  private awsSecretKey = process.env.AWS_SECRET_ACCESS_KEY;
  private awsRegion = process.env.AWS_REGION;

  constructor() {
    this.client = new AWS.SQS({
      credentials: {
        accessKeyId: this.awsAccessKey,
        secretAccessKey: this.awsSecretKey,
      },
      region: this.awsRegion,
      sslEnabled: true,
    });

    /**
     * The code below is used for testing if the SQS service works.
    */
    this.client.listQueues({}, (err, data) => {
      if (err) console.log(err, err.stack); // an error occurred
      else console.log(data); // successful response
    });
  }
}

以上两种方法都是正确的。

以下报错产生于 this.client.sendMessage() ,但是报错信息中确有 InvalidClientTokenId: The security token included in the request is invalid. 的字样,sendMessage 代码与官方文档一致,令人困惑。

    this.client.sendMessage(sendMessageRequest, (err, data) => {
      if (err) console.log(err); // an error occurred
      else console.log(data); // successful response}
    });

原因是队列的 URL 写错了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值