Provides an injectable S3 client to provide s3 storage access from nestjs modules
@ntegral/nestjs-s3
implements a module, S3Module
, which when imported into
your nestjs project provides an S3 client to any class that injects it. This
lets AWS S3 be worked into your dependency injection workflow without having to
do any extra work outside of the initial setup.
npm install --save @ntegral/nestjs-s3 aws-sdk
The simplest way to use @ntegral/nestjs-s3
is to use S3Module.forRoot
import { Module } from '@nestjs-common';
import { S3Module } from '@ntegral/nestjs-s3';
@Module({
imports: [
S3Module.forRoot({
accessKeyId: 'aws_access_key_id',
secretAccessKey: 'aws_secret_access_key',
region?: 'aws_region_id',
sessionToken?: 'aws_session_token', | null,
apiVersion?: 's3_api_version' //based on s3 api version //
}),
],
})
export class AppModule {}
The async way @ntegral/nestjs-s3
is to use S3Module.forRootAsync
import { Module } from '@nestjs-common';
import { S3Module } from '@ntegral/nestjs-s3';
import { ConfigModule } from '@ntegral/nestjs-config';
import { ConfigService } from '@ntegral/nestjs-config';
@Module({
imports: [
S3Module.forRootAsync({
imports: [ConfigModule],
useFactory: async (cfg:ConfigService) => ({
accessKeyId: cfg.get('ACCESS_KEY_ID'),
secretAccessKey: 'AWS_SECRET_ACCESS_KEY'
}),
inject: [ConfigService],
})
]
})
export class AppModule {}
You can then inject the S3 client into any of your injectables by using a custom decorator
import { Injectable } from '@nestjs/common';
import { InjectS3 } from '@ntegral/nestjs-s3';
import * as S3 from 'aws-sdk/clients/s3';
@Injectable()
export class AppService {
public constructor(@InjectS3() private readonly client: S3) { }
upload(pdf) {
const params = {
ACL: 'public-read',
Body: pdf,
Bucket: process.env.BUCKET,
Key: uuidv4(),
ContentEncoding: 'base64',
ContentType: `application/pdf`
};
try {
const { Location, Key } = await this.client.upload(params).promise();
return Location;
} catch (error) {
console.log('ERROR S3 FILE UPLOAD => ', error);
}
}
}
I would greatly appreciate any contributions to make this project better. Please make sure to follow the below guidelines before getting your hands dirty.
git checkout -b my-branch
)Distributed under the ISC License. See LICENSE
for more information.
Copyright © 2020 Ntegral Inc.