Skip to content

NestJS Storage module, work with famous object storage like S3, R2, Azure, Google Cloud

License

Notifications You must be signed in to change notification settings

muhammadzadeh/nestorage

Repository files navigation

Nestorage Logo

NestJS Storage module, work with famous object storage like S3, R2, Azure, Google Cloud

Installation

npm i --save @muhammadzadeh/nestorage

How To

Local Storage Example

// 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 {}

S3 Storage Example

// 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 {}

R2 Storage Example

// 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 {}

Azure Storage Example

// 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 {}

Google Cloud Storage Example

// 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 {}

StorageService

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"`,
    });
  }
}

Todo

  • Add tests
  • Manage Buckets(create, delete, list)
  • Delete Object
  • Copy Object
  • Share Object

Support

nestorage is an MIT-licensed open source project. If this library is helpful, please click star to support it.

License

nestorage is MIT licensed.

About

NestJS Storage module, work with famous object storage like S3, R2, Azure, Google Cloud

Topics

Resources

License

Security policy

Stars

Watchers

Forks