Skip to content

Commit

Permalink
add config/s3.ts and its unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Byunk committed Jan 11, 2024
1 parent 290d6e6 commit 59afb8c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/config/s3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
S3Client,
PutObjectCommand,
DeleteObjectCommand,
} from '@aws-sdk/client-s3';
import { config } from 'dotenv';
import logger from '../config/winston';

config();

const client = new S3Client({
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID as string,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY as string,
},
region: process.env.AWS_REGION as string,
});

export async function uploadFileToS3(file: Express.Multer.File, key: string) {
logger.info(`Uploading file ${file.originalname} to S3`);

const command = new PutObjectCommand({
Bucket: process.env.AWS_STORAGE_BUCKET_NAME as string,
Key: key,
Body: file.buffer,
});

try {
const res = await client.send(command);
return {
statusCode: res.$metadata.httpStatusCode,
};
} catch (error) {
logger.error(error);
throw error;
}
}

export async function deleteFileFromS3(key: string) {
logger.info(`Deleting file (${key}) from S3`);

const command = new DeleteObjectCommand({
Bucket: process.env.AWS_STORAGE_BUCKET_NAME as string,
Key: key,
});

try {
const res = await client.send(command);
return {
statusCode: res.$metadata.httpStatusCode,
};
} catch (error) {
logger.error(error);
throw error;
}
}
Binary file added test/assets/pdf2.pdf
Binary file not shown.
44 changes: 44 additions & 0 deletions test/config/s3.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fs from 'fs';
import { deleteFileFromS3, uploadFileToS3 } from '../../src/config/s3';
import { expect } from 'chai';

const dummyImage1 = 'test/assets/image1.jpg';
const dummyPdf2 = 'test/assets/pdf2.pdf';

//! NOTE: this causes the other tests to fail
//! Uncomment this to test the S3 config
//! Do not forget to comment this again before pushing to GitHub
// describe('S3 config', function () {
// let file: Express.Multer.File;
// let buffer: Buffer;

// it('should upload and delete a image file to S3', async function () {
// this.skip();
// buffer = fs.readFileSync(dummyImage1);
// file = {
// originalname: 'image1.jpg',
// buffer: buffer,
// mimetype: 'image/jpeg',
// } as Express.Multer.File;

// const key = 'unittest/image1.jpg';
// const res = await uploadFileToS3(file, key);
// expect(res.statusCode).to.equal(200);
// await deleteFileFromS3(key);
// });

// it('should upload and delete a pdf file to S3', async function () {
// this.skip();
// buffer = fs.readFileSync(dummyPdf2);
// file = {
// originalname: 'pdf2.jpg',
// buffer: buffer,
// mimetype: 'application/pdf',
// } as Express.Multer.File;

// const key = 'unittest/pdf2.pdf';
// const res = await uploadFileToS3(file, key);
// expect(res.statusCode).to.equal(200);
// await deleteFileFromS3(key);
// });
// });

0 comments on commit 59afb8c

Please sign in to comment.