迁移到AWS SDK v3 NextJs aws-ses Simple Email Services

2w3rbyxf  于 2023-08-04  发布在  其他
关注(0)|答案(1)|浏览(81)

我想将基于aws sdk v2的ses-utils typescript函数迁移到v3
下面是我的代码:

import AWS from 'aws-sdk';
import { SES, SESClient } from "@aws-sdk/client-ses";
require('dotenv').config();

AWS.config.update({
  region: `${process.env.AWS_REGION}`,
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
})

const ses = new SES({ apiVersion: 'latest' });

interface EmailParams {
  Source: string;
  Destination: {
    ToAddresses: string[];
  };
  Message: {
    Subject: { Data: string };
    Body: { Html: { Data: string } };
  };
}

const sendEmail = async (params: EmailParams): Promise<void> => {
  return new Promise((resolve, reject) => {
    ses.sendEmail(params, (err:any, data:any) => {
      if (err) {
        reject(err);
      } else {
        resolve();
      }
    });
  });
};

export { sendEmail };

字符串
我试过使用AWS建议的自动迁移工具,但在我的情况下它不能正常工作。它只是在我的代码中添加了以下行:

import { SES, SESClient } from "@aws-sdk/client-ses";


有人可以帮助我将此代码迁移到AWS SDK版本3吗?

lpwwtiir

lpwwtiir1#

问题的解决方案是linked here
特别是,我使用以下代码行编辑了ses-utils.ts:

import { SendEmailCommand } from "@aws-sdk/client-ses";
import { sesClient } from "./sesClient.js";

interface EmailParams {
  Source: string;
  Destination: {
    ToAddresses: string[];
  };
  Message: {
    Subject: { Data: string };
    Body: { Html: { Data: string } };
  };
}

const sendEmail = async (params: EmailParams): Promise<void> => { 
  return new Promise((resolve, reject) => {
    sesClient.send(new SendEmailCommand (params), (err:any, data:any) => {
      if (err) {
        reject(err);
      } else {
        resolve();
      }
    });
  });
};

export {sendEmail};

字符串
我使用了sesClient.js组件,如下所示:

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
ABOUT THIS NODE.JS EXAMPLE: This example works with the AWS SDK for JavaScript version 3 (v3),
which is available at https://github.com/aws/aws-sdk-js-v3. This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at
https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/ses-examples.html.

Purpose:
sesClient.js is a helper function that creates an Amazon Simple Email Services (Amazon SES) service client.

*/
// snippet-start:[ses.JavaScript.createclientv3]
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "region-aws-1";
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
// snippet-end:[ses.JavaScript.createclientv3]


希望你会发现这个答案有用

相关问题