0.0.18 • Published 1 year ago

@muhammadzadeh/nestorage v0.0.18

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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.

0.0.17

1 year ago

0.0.18

1 year ago

0.0.15

1 year ago

0.0.16

1 year ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago