0.0.11 • Published 1 year ago
@prachyot/nestjs-s3 v0.0.11
S3 Module
A NestJS module to provide a S3 client and a S3 service for your application.
Implementation purpose
- Reduce project setup time
- Reduce project onboarding time
- Help lean your base project
Installation
yarn add @prachyot/nestjs-s3
Usage
Add these new variable to your .env
file
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
Import the module in your AppModule
import { S3Module } from '@prachyot/nestjs-s3';
...
@Module({
imports: [
S3Module
...
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
You can change the default configuration by passing an object to the forRoot method
import { S3Module } from '@prachyot/nestjs-s3';
...
@Module({
imports: [
S3Module.forRoot({
envFilePath: '.env.local' // Change the path to your env file
})
...
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Or you can use forRootAsync
method to pass a factory function
import { S3Module } from '@prachyot/nestjs-s3';
...
@Module({
imports: [
S3Module.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
return {
envFilePath: configService.get('ENV_FILE_PATH'),
}
}
})
...
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Service method specification
// Method: s3
// Description: Returns an instance of the S3 client
this.s3Service.s3();
/*
* Method: uploadFile
* Description: Uploads a file to S3
*
* @Params:
* bucketName: string
* dir: string
* fileName: string
* cacheAge: number (optional) (default: 31536000) (1 year)
*/
await this.uploadFile('my-bucket', 'src/assets', 'my-file.png');