Skip to content

Commit

Permalink
Merge pull request #11 from arthurlenoir/master
Browse files Browse the repository at this point in the history
Migrate from aws-sdk v2 to @AWS-SDK v3
  • Loading branch information
titanism authored Sep 6, 2023
2 parents 10663c7 + b34d24f commit 84f3df5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
16 changes: 6 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const zlib = require('zlib');
const { Buffer } = require('buffer');
const { promisify } = require('util');

const AWS = require('aws-sdk');
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
const sharp = require('sharp');
const _ = require('lodash');
const debug = require('debug')('nodemailer-base64-to-s3');
Expand Down Expand Up @@ -46,14 +46,7 @@ const base64ToS3 = (options = {}) => {
if (_.startsWith(opts.dir, '/')) opts.dir = opts.dir.slice(1);

// prepare AWS upload using config
const s3 = new AWS.S3(opts.aws);

// we cannot currently use this since it does not return a promise
// <https://github.com/aws/aws-sdk-js/pull/1079>
// await s3obj.upload({ Body }).promise();
//
// so instead we use promisify to convert it to a promise
const upload = promisify(s3.upload).bind(s3);
const s3Client = new S3Client(opts.aws);

async function compile(mail, fn) {
try {
Expand Down Expand Up @@ -124,6 +117,7 @@ const base64ToS3 = (options = {}) => {
const Key = `${opts.dir}${fileName}`;

const obj = {
...opts.aws.params,
Key,
ACL: 'public-read',
Body,
Expand All @@ -135,7 +129,9 @@ const base64ToS3 = (options = {}) => {
// use a fallback dir if the upload fails
// but only if the environment is not production
try {
const data = cache[Key] ? cache[Key] : await upload(obj);
const data = cache[Key]
? cache[Key]
: await s3Client.send(new PutObjectCommand(obj));
if (cache[Key]) debug(`hitting cache for ${Key}`);

const replacement = isSANB(opts.cloudFrontDomainName)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
],
"dependencies": {
"aws-sdk": "^2.1152.0",
"@aws-sdk/client-s3": "^3.373.0",
"debug": "^4.3.4",
"is-string-and-not-blank": "^0.0.2",
"lodash": "^4.17.21",
Expand All @@ -30,7 +30,7 @@
"cheerio": "^1.0.0-rc.11",
"cross-env": "^7.0.3",
"dotenv": "^16.0.1",
"eslint": "^8.17.0",
"eslint": "8.39.0",
"eslint-config-xo-lass": "^2.0.1",
"fixpack": "^4.0.0",
"husky": "^8.0.1",
Expand Down
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ transport.use(
dir: 'nodemailer/',
cloudFrontDomainName: process.env.AWS_CLOUDFRONT_DOMAIN,
aws: {
region: process.env.AWS_REGION,
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
params: {
Expand Down

0 comments on commit 84f3df5

Please sign in to comment.