-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// }); | ||
// }); |