Skip to content

Commit 84f3df5

Browse files
authored
Merge pull request #11 from arthurlenoir/master
Migrate from aws-sdk v2 to @AWS-SDK v3
2 parents 10663c7 + b34d24f commit 84f3df5

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const zlib = require('zlib');
66
const { Buffer } = require('buffer');
77
const { promisify } = require('util');
88

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

4848
// prepare AWS upload using config
49-
const s3 = new AWS.S3(opts.aws);
50-
51-
// we cannot currently use this since it does not return a promise
52-
// <https://github.com/aws/aws-sdk-js/pull/1079>
53-
// await s3obj.upload({ Body }).promise();
54-
//
55-
// so instead we use promisify to convert it to a promise
56-
const upload = promisify(s3.upload).bind(s3);
49+
const s3Client = new S3Client(opts.aws);
5750

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

126119
const obj = {
120+
...opts.aws.params,
127121
Key,
128122
ACL: 'public-read',
129123
Body,
@@ -135,7 +129,9 @@ const base64ToS3 = (options = {}) => {
135129
// use a fallback dir if the upload fails
136130
// but only if the environment is not production
137131
try {
138-
const data = cache[Key] ? cache[Key] : await upload(obj);
132+
const data = cache[Key]
133+
? cache[Key]
134+
: await s3Client.send(new PutObjectCommand(obj));
139135
if (cache[Key]) debug(`hitting cache for ${Key}`);
140136

141137
const replacement = isSANB(opts.cloudFrontDomainName)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)"
1616
],
1717
"dependencies": {
18-
"aws-sdk": "^2.1152.0",
18+
"@aws-sdk/client-s3": "^3.373.0",
1919
"debug": "^4.3.4",
2020
"is-string-and-not-blank": "^0.0.2",
2121
"lodash": "^4.17.21",
@@ -30,7 +30,7 @@
3030
"cheerio": "^1.0.0-rc.11",
3131
"cross-env": "^7.0.3",
3232
"dotenv": "^16.0.1",
33-
"eslint": "^8.17.0",
33+
"eslint": "8.39.0",
3434
"eslint-config-xo-lass": "^2.0.1",
3535
"fixpack": "^4.0.0",
3636
"husky": "^8.0.1",

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ transport.use(
3030
dir: 'nodemailer/',
3131
cloudFrontDomainName: process.env.AWS_CLOUDFRONT_DOMAIN,
3232
aws: {
33+
region: process.env.AWS_REGION,
3334
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
3435
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
3536
params: {

0 commit comments

Comments
 (0)