Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add AWS S3 service
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 13, 2020
1 parent bd930be commit 7bc5e7c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -68,6 +69,7 @@ import { TasksModule } from './providers/tasks/tasks.module';
ElasticSearchModule,
SlackModule,
AirtableModule,
S3Module,
],
providers: [
{
Expand Down
7 changes: 7 additions & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ export interface Configuration {
apiKey: string;
endpointUrl?: string;
};

s3: {
accessKeyId: string;
secretAccessKey: string;
region: string;
bucket?: string;
};
}
6 changes: 6 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = () => configuration;
Expand Down
10 changes: 10 additions & 0 deletions src/providers/s3/s3.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
42 changes: 42 additions & 0 deletions src/providers/s3/s3.service.ts
Original file line number Diff line number Diff line change
@@ -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<Configuration['s3']>('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<AWS.S3.ManagedUpload.SendData> {
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);
},
);
});
}
}

0 comments on commit 7bc5e7c

Please sign in to comment.