From 19a4c2351d0068736c7a1ceef84a444c1985e098 Mon Sep 17 00:00:00 2001 From: horstenwillem Date: Tue, 14 May 2019 14:43:54 +0200 Subject: [PATCH 1/2] Allow file content to be upload instead of only the file path --- README.md | 3 ++- src/lib/amazon.ts | 8 +++++--- tests/lib/amazon.test.ts | 27 ++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 39c5e3b..6cf3414 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,8 @@ Upload a file to S3 import { amazon } from '@icapps/tree-house-storage' const options = { - path: 'localPath/localFile.png', + filePath: 'localPath/localFile.png', + content: 'fileContent ...', name: uuid.v4(), contentType: 'image/png', bucket: 's3bucketName', diff --git a/src/lib/amazon.ts b/src/lib/amazon.ts index 7e7ae80..518d4b9 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.filePath) fileStream = await readFile(options.filePath); 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; + filePath?: string; } export interface IUploadS3Result { diff --git a/tests/lib/amazon.test.ts b/tests/lib/amazon.test.ts index 2570265..9618ada 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', @@ -56,7 +56,7 @@ describe('amazon', () => { }); await uploadFile(awsClient, { - path: './uploadtest.txt', + filePath: './uploadtest.txt', contentType: 'image/png', bucket: 'bucket', key: 'key', @@ -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', @@ -74,7 +74,24 @@ describe('amazon', () => { }); await uploadFile(awsClient, { - path: './uploadtest.txt', + filePath: './uploadtest.txt', + contentType: 'image/png', + bucket: 'bucket', + key: 'key', + }); + + 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', @@ -89,7 +106,7 @@ describe('amazon', () => { expect.assertions(NUM_ERROR_CHECKS); try { await uploadFile(awsClient, { - path: './uploadtest.txt', + filePath: './uploadtest.txt', contentType: 'image/png', bucket: 'bucket', key: 'key', From 6b76dd0f375eb54cf869a5881ca356a732c13eb2 Mon Sep 17 00:00:00 2001 From: horstenwillem Date: Tue, 14 May 2019 14:46:25 +0200 Subject: [PATCH 2/2] Update dependencies --- README.md | 2 +- package-lock.json | 12 ++++++------ package.json | 4 ++-- src/lib/amazon.ts | 4 ++-- tests/lib/amazon.test.ts | 6 +++--- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6cf3414..4c73744 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ Upload a file to S3 import { amazon } from '@icapps/tree-house-storage' const options = { - filePath: 'localPath/localFile.png', + path: 'localPath/localFile.png', content: 'fileContent ...', name: uuid.v4(), contentType: 'image/png', 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 518d4b9..7703a96 100644 --- a/src/lib/amazon.ts +++ b/src/lib/amazon.ts @@ -51,7 +51,7 @@ export async function getFile(client: S3, bucket: string, key: string): Promise< export async function uploadFile(client: S3, options: IUploadS3Options): Promise { try { let fileStream; - if (options.filePath) fileStream = await readFile(options.filePath); + if (options.path) fileStream = await readFile(options.path); const params: S3.PutObjectRequest = { Bucket: options.bucket, @@ -144,7 +144,7 @@ export interface IUploadS3Options { key: string; content?: string; encryption?: string; - filePath?: string; + path?: string; } export interface IUploadS3Result { diff --git a/tests/lib/amazon.test.ts b/tests/lib/amazon.test.ts index 9618ada..e64ea3d 100644 --- a/tests/lib/amazon.test.ts +++ b/tests/lib/amazon.test.ts @@ -56,7 +56,7 @@ describe('amazon', () => { }); await uploadFile(awsClient, { - filePath: './uploadtest.txt', + path: './uploadtest.txt', contentType: 'image/png', bucket: 'bucket', key: 'key', @@ -74,7 +74,7 @@ describe('amazon', () => { }); await uploadFile(awsClient, { - filePath: './uploadtest.txt', + path: './uploadtest.txt', contentType: 'image/png', bucket: 'bucket', key: 'key', @@ -106,7 +106,7 @@ describe('amazon', () => { expect.assertions(NUM_ERROR_CHECKS); try { await uploadFile(awsClient, { - filePath: './uploadtest.txt', + path: './uploadtest.txt', contentType: 'image/png', bucket: 'bucket', key: 'key',