From 7bc5e7c6b46790b7bde90d5bdf24ce8be88d879c Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Fri, 13 Nov 2020 18:10:53 +0530 Subject: [PATCH] :sparkles: Add AWS S3 service --- src/app.module.ts | 2 ++ src/config/configuration.interface.ts | 7 +++++ src/config/configuration.ts | 6 ++++ src/providers/s3/s3.module.ts | 10 +++++++ src/providers/s3/s3.service.ts | 42 +++++++++++++++++++++++++++ 5 files changed, 67 insertions(+) create mode 100644 src/providers/s3/s3.module.ts create mode 100644 src/providers/s3/s3.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index b1e5e4fe3..cf7d36558 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -34,6 +34,7 @@ import { ElasticSearchModule } from './providers/elasticsearch/elasticsearch.mod import { GeolocationModule } from './providers/geolocation/geolocation.module'; import { MailModule } from './providers/mail/mail.module'; import { PrismaModule } from './providers/prisma/prisma.module'; +import { S3Module } from './providers/s3/s3.module'; import { SlackModule } from './providers/slack/slack.module'; import { TasksModule } from './providers/tasks/tasks.module'; @@ -68,6 +69,7 @@ import { TasksModule } from './providers/tasks/tasks.module'; ElasticSearchModule, SlackModule, AirtableModule, + S3Module, ], providers: [ { diff --git a/src/config/configuration.interface.ts b/src/config/configuration.interface.ts index 2ea6c959a..17e12f64b 100644 --- a/src/config/configuration.interface.ts +++ b/src/config/configuration.interface.ts @@ -96,4 +96,11 @@ export interface Configuration { apiKey: string; endpointUrl?: string; }; + + s3: { + accessKeyId: string; + secretAccessKey: string; + region: string; + bucket?: string; + }; } diff --git a/src/config/configuration.ts b/src/config/configuration.ts index d15fcd0e5..03a0d7d90 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -109,6 +109,12 @@ const configuration: Configuration = { apiKey: process.env.AIRTABLE_API_KEY ?? '', endpointUrl: process.env.AIRTABLE_ENDPOINT_URL, }, + s3: { + accessKeyId: process.env.S3_ACCESS_KEY_ID ?? '', + secretAccessKey: process.env.S3_SECRET_ACCESS_KEY ?? '', + region: process.env.S3_REGION ?? '', + bucket: process.env.S3_BUCKET, + }, }; const configFunction: ConfigFactory = () => configuration; diff --git a/src/providers/s3/s3.module.ts b/src/providers/s3/s3.module.ts new file mode 100644 index 000000000..818216eec --- /dev/null +++ b/src/providers/s3/s3.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { S3Service } from './s3.service'; + +@Module({ + imports: [ConfigModule], + providers: [S3Service], + exports: [S3Service], +}) +export class S3Module {} diff --git a/src/providers/s3/s3.service.ts b/src/providers/s3/s3.service.ts new file mode 100644 index 000000000..ba7a36c66 --- /dev/null +++ b/src/providers/s3/s3.service.ts @@ -0,0 +1,42 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import AWS from 'aws-sdk'; +import { Configuration } from '../../config/configuration.interface'; + +@Injectable() +export class S3Service { + s3?: AWS.S3; + private logger = new Logger(S3Service.name); + + constructor(private configService: ConfigService) { + const config = this.configService.get('s3'); + if (config.accessKeyId) + this.s3 = new AWS.S3({ + apiVersion: '2006-03-01', + accessKeyId: config.accessKeyId, + secretAccessKey: config.secretAccessKey, + region: config.region, + }); + else this.logger.warn('No S3 API key set'); + } + + upload( + name: string, + body: Buffer, + bucket?: string, + ): Promise { + return new Promise((resolve, reject) => { + this.s3.upload( + { + Bucket: bucket, + Key: name, + Body: body, + }, + (error: any, data: any) => { + if (error) return reject(error); + resolve(data); + }, + ); + }); + } +}