diff --git a/README.md b/README.md index 39c5e3b..4c73744 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ import { amazon } from '@icapps/tree-house-storage' const options = { path: 'localPath/localFile.png', + content: 'fileContent ...', name: uuid.v4(), contentType: 'image/png', bucket: 's3bucketName', diff --git a/package-lock.json b/package-lock.json index b1504d6..b653451 100644 --- a/package-lock.json +++ b/package-lock.json @@ -654,9 +654,9 @@ } }, "@types/jest": { - "version": "24.0.12", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-24.0.12.tgz", - "integrity": "sha512-60sjqMhat7i7XntZckcSGV8iREJyXXI6yFHZkSZvCPUeOnEJ/VP1rU/WpEWQ56mvoh8NhC+sfKAuJRTyGtCOow==", + "version": "24.0.13", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-24.0.13.tgz", + "integrity": "sha512-3m6RPnO35r7Dg+uMLj1+xfZaOgIHHHut61djNjzwExXN4/Pm9has9C6I1KMYSfz7mahDhWUOVg4HW/nZdv5Pww==", "dev": true, "requires": { "@types/jest-diff": "*" @@ -1029,9 +1029,9 @@ "dev": true }, "aws-sdk": { - "version": "2.452.0", - "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.452.0.tgz", - "integrity": "sha512-l6J2NmUg12xpDKG9YdlPje5+Z+nNvqyWMA85ookzPqwx8RcD28D3vg4K1aGi27/oo/3NsngR3XfKUBPSqUpUMA==", + "version": "2.454.0", + "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.454.0.tgz", + "integrity": "sha512-1vB9DNIwh+mqKD2IZspYTQapCD6f5VnMT5V2VPlXJ1CNcUdFSU8FFyxKmYApNs+S3re1h3fhWDjpwTreS+XLRQ==", "requires": { "buffer": "4.9.1", "events": "1.1.1", diff --git a/package.json b/package.json index 6bd2a11..844cba7 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "build" ], "dependencies": { - "aws-sdk": "~2.452.0", + "aws-sdk": "~2.454.0", "multer": "~1.4.1", "tree-house": "~3.4.3", "tree-house-errors": "~1.2.3" @@ -29,7 +29,7 @@ "@types/cors": "~2.8.5", "@types/express-brute": "~0.0.37", "@types/helmet": "~0.0.43", - "@types/jest": "~24.0.12", + "@types/jest": "~24.0.13", "@types/joi": "~14.3.3", "@types/multer": "~1.3.7", "@types/redis": "~2.8.12", diff --git a/src/lib/amazon.ts b/src/lib/amazon.ts index 7e7ae80..7703a96 100644 --- a/src/lib/amazon.ts +++ b/src/lib/amazon.ts @@ -50,12 +50,13 @@ export async function getFile(client: S3, bucket: string, key: string): Promise< */ export async function uploadFile(client: S3, options: IUploadS3Options): Promise { try { - const fileStream = await readFile(options.path); + let fileStream; + if (options.path) fileStream = await readFile(options.path); const params: S3.PutObjectRequest = { Bucket: options.bucket, Key: options.key, - Body: fileStream, + Body: fileStream || options.content, ContentType: options.contentType, }; @@ -138,11 +139,12 @@ export interface IClientS3Options { } export interface IUploadS3Options { - path: string; contentType: string; bucket: string; key: string; + content?: string; encryption?: string; + path?: string; } export interface IUploadS3Result { diff --git a/tests/lib/amazon.test.ts b/tests/lib/amazon.test.ts index 2570265..e64ea3d 100644 --- a/tests/lib/amazon.test.ts +++ b/tests/lib/amazon.test.ts @@ -48,7 +48,7 @@ describe('amazon', () => { fs.unlinkSync('./uploadtest.txt'); }); - it('Should succesfully upload the file to Amazon S3 with encryption', async () => { + it('Should succesfully upload the file from path to Amazon S3 with encryption', async () => { s3PromiseMock.mockResolvedValueOnce({ Location: 'http://s3.file.com', Bucket: 'bucket', @@ -66,7 +66,7 @@ describe('amazon', () => { expect(s3PromiseMock).toBeCalledTimes(1); }); - it('Should succesfully upload the file to Amazon S3 without encryption', async () => { + it('Should succesfully upload the file from path to Amazon S3 without encryption', async () => { s3PromiseMock.mockResolvedValueOnce({ Location: 'http://s3.file.com', Bucket: 'bucket', @@ -83,6 +83,23 @@ describe('amazon', () => { expect(s3PromiseMock).toBeCalledTimes(1); }); + it('Should succesfully upload the file content to Amazon S3 without encryption', async () => { + s3PromiseMock.mockResolvedValueOnce({ + Location: 'http://s3.file.com', + Bucket: 'bucket', + Key: 'key', + }); + + await uploadFile(awsClient, { + content: 'fileContent blabla', + contentType: 'image/png', + bucket: 'bucket', + key: 'key', + }); + + expect(s3PromiseMock).toBeCalledTimes(1); + }); + it('Should throw a custom error', async () => { s3PromiseMock.mockRejectedValueOnce(Error('random error occured'));