NestJS Storage module, work with famous object storage like S3, R2, Azure, Google Cloud
npm i --save @muhammadzadeh/nestorage
- select your preferred provider and provide its configs
- inject StorageService
// app.module.ts
import { Module } from '@nestjs/common';
import { StorageModule } from '@muhammadzadeh/nestorage';
@Module({
imports: [
StorageModule.forRoot({
provider: 'local',
options: {
root: 'ROOT_DIR',
},
}),
],
})
export class AppModule {}
// app.module.ts
import { Module } from '@nestjs/common';
import { StorageModule } from '@muhammadzadeh/nestorage';
@Module({
imports: [
StorageModule.forRoot({
provider: 's3',
options: {
accessKeyId: 'ACCESS_KEY_ID',
secretAccessKey: 'SECRET_ACCESS_KEY',
endpoint: 'S3_ENDPOINT',
region: 'auto',
},
}),
],
})
export class AppModule {}
// app.module.ts
import { Module } from '@nestjs/common';
import { StorageModule } from '@muhammadzadeh/nestorage';
@Module({
imports: [
StorageModule.forRoot({
provider: 'r2',
options: {
accessKeyId: 'ACCESS_KEY_ID',
secretAccessKey: 'SECRET_ACCESS_KEY',
endpoint: 'CLOUDFLARE_R2_EXAMPLE',
region: 'auto',
},
}),
],
})
export class AppModule {}
// app.module.ts
import { Module } from '@nestjs/common';
import { StorageModule } from '@muhammadzadeh/nestorage';
@Module({
imports: [
StorageModule.forRoot({
provider: 'azure',
options: {
connectionString: 'CONNECTION_STRING ',
},
}),
],
})
export class AppModule {}
// app.module.ts
import { Module } from '@nestjs/common';
import { StorageModule } from '@muhammadzadeh/nestorage';
@Module({
imports: [
StorageModule.forRoot({
provider: 'gcs',
options: {
keyFilename: 'GOOGLE_KEY_FILE_NAME',
},
}),
],
})
export class AppModule {}
import { Injectable, StreamableFile } from '@nestjs/common';
import { StorageService } from '@muhammadzadeh/nestorage';
@Injectable()
export class MyClass {
constructor(private readonly storage: StorageService) {}
async uploadFile(file: Express.Multer.File) {
await this.storage.putObject(
'bucket',
'path/to/sub',
'my-file1.png',
file.buffer,
);
}
async DownloadFile() {
const buffer = await this.storage.getObject(
'bucket',
'path/to/sub',
'my-file.png',
);
return new StreamableFile(buffer, {
type: 'png',
length: buffer.length,
disposition: `attachment; filename="my-file.png"`,
});
}
}
- Add tests
- Manage Buckets(create, delete, list)
- Delete Object
- Copy Object
- Share Object
nestorage is an MIT-licensed open source project. If this library is helpful, please click star to support it.
nestorage is MIT licensed.