NestJS Minio Client

2.2.0 · active · verified Sun Apr 19

The `nestjs-minio-client` package provides a robust integration of the Minio S3-compatible object storage client into the NestJS framework. It offers a `MinioModule` for streamlined configuration and registration, supporting both synchronous `register()` and asynchronous `registerAsync()` methods, which is particularly useful for injecting configuration from NestJS's `@nestjs/config` package. An injectable `MinioService` then provides direct access to the underlying Minio JS SDK client instance, allowing developers to interact with Minio's API. The current stable version is 2.2.0, with a consistent release cadence focusing on updates to dependencies, NestJS compatibility, and internal refactoring. It serves as a dedicated wrapper, simplifying Minio usage within NestJS applications and abstracting away direct SDK initialization. It requires NestJS version 9 or later.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates registering the `MinioModule` asynchronously using `@nestjs/config` to load Minio credentials from environment variables, and then injecting and using `MinioService` in another service to list buckets and simulate a file upload operation.

import { Module } from '@nestjs/common';
import { MinioModule } from 'nestjs-minio-client';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MinioClientService } from './minio-client.service'; // Assuming you have a service using MinioService

@Module({
  imports: [
    ConfigModule.forRoot(), // Load environment variables
    MinioModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          endPoint: config.get<string>('MINIO_ENDPOINT') ?? '127.0.0.1',
          port: parseInt(config.get<string>('MINIO_PORT') ?? '9000', 10),
          useSSL: config.get<string>('MINIO_USE_SSL') === 'true',
          accessKey: config.get<string>('MINIO_ACCESS_KEY') ?? process.env.MINIO_ACCESS_KEY ?? '',
          secretKey: config.get<string>('MINIO_SECRET_KEY') ?? process.env.MINIO_SECRET_KEY ?? ''
        };
      },
    }),
  ],
  providers: [MinioClientService],
  exports: [MinioClientService],
})
export class AppModule {}

// Example minio-client.service.ts
import { Injectable } from '@nestjs/common';
import { MinioService } from 'nestjs-minio-client';

@Injectable()
export class MinioClientService {
  constructor(private readonly minioService: MinioService) {}

  async listAllBuckets() {
    return this.minioService.client.listBuckets();
  }

  // Add more methods for Minio operations here
  async uploadFile(bucketName: string, objectName: string, filepath: string) {
    // This is an example, actual Minio client usage would vary
    // For instance, you might use putObject, fPutObject, etc.
    console.log(`Uploading ${filepath} to ${bucketName}/${objectName}`);
    // await this.minioService.client.fPutObject(bucketName, objectName, filepath);
    return { success: true, objectName };
  }
}

view raw JSON →