Skip to content

Commit

Permalink
Merge 6b76dd0 into 92ab747
Browse files Browse the repository at this point in the history
  • Loading branch information
horstenwillem committed May 14, 2019
2 parents 92ab747 + 6b76dd0 commit 312f572
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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',
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions src/lib/amazon.ts
Expand Up @@ -50,12 +50,13 @@ export async function getFile(client: S3, bucket: string, key: string): Promise<
*/
export async function uploadFile(client: S3, options: IUploadS3Options): Promise<IUploadS3Result> {
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,
};

Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 19 additions & 2 deletions tests/lib/amazon.test.ts
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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(<any>Error('random error occured'));

Expand Down

0 comments on commit 312f572

Please sign in to comment.