File

src/provider/minio/minio.service.ts

Index

Properties
Methods

Constructor

constructor()

Methods

Async deleteObject
deleteObject(objectName: string)
Parameters :
Name Type Optional
objectName string No
Returns : any
Async saveFile
saveFile(file: Express.Multer.File)
Parameters :
Name Type Optional
file Express.Multer.File No
Returns : Promise<string>

Properties

Private Readonly logger
Default value : new Logger(MinioService.name)
minioClient
Type : Minio.Client
import { Injectable, Logger } from '@nestjs/common';
import * as Minio from 'minio';
import fs from 'fs';
import { promisify } from 'util';

@Injectable()
export class MinioService {
  private readonly logger = new Logger(MinioService.name);

  minioClient: Minio.Client;

  constructor() {
    const { GS_MINIO_ENDPOINT, GS_MINIO_ACCESSKEY, GS_MINIO_SECRET } =
      process.env;

    this.minioClient = new Minio.Client({
      endPoint: GS_MINIO_ENDPOINT,
      accessKey: GS_MINIO_ACCESSKEY,
      secretKey: GS_MINIO_SECRET,
    });
  }

  async saveFile(file: Express.Multer.File): Promise<string> {
    const { KVA_MINIO_BUCKET } = process.env;

    const { filename, path: filePathTmp } = file;

    await this.minioClient.fPutObject(KVA_MINIO_BUCKET, filename, filePathTmp);

    const unlinkAsync = promisify(fs.unlink);

    await unlinkAsync(filePathTmp);

    return filename;
  }

  async deleteObject(objectName: string) {
    try {
      const { KVA_MINIO_BUCKET } = process.env;

      const unlinkAsync = promisify(fs.unlink);

      await this.minioClient.statObject(KVA_MINIO_BUCKET, objectName);
      await this.minioClient.removeObject(KVA_MINIO_BUCKET, objectName);
      await unlinkAsync(`dist/public/images/${objectName}.png`);
    } catch (error) {
      this.logger.error('Object Not Found');
    }
  }
}

results matching ""

    No results matching ""