Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from aws-sdk v2 to @aws-sdk v3 #11

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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