File

src/providers/minio/minio.provider.ts

Index

Methods

Methods

Async bucketExists
bucketExists(bucketName)

check if bucket with name exists

Parameters :
Name Optional
bucketName No
Returns : Promise<boolean>

Promise boolean

Async createBucket
createBucket(bucketName)

Function to create minio bucket

Parameters :
Name Optional
bucketName No
Returns : Promise<void>

Promise void

getApiClient
getApiClient()

Function to get api client

Returns : Minio.Client
Async getFile
getFile(bucketName: string, fileName: string)

Function to get file from bucket

Parameters :
Name Type Optional
bucketName string No
fileName string No
Returns : Promise<NodeJS.ReadableStream>

Promise ReadableStream

Async getTemporarySharedURL
getTemporarySharedURL(bucketName, filename)

Function to generate a Temporary download URL for filename withing bucketname

Parameters :
Name Optional Description
bucketName No

name of bucket

filename No

absolute path for file wrt bucket

Returns : Promise<string>

Promise that resolves to url string

Async listBuckets
listBuckets()

Function to list available buckets

Returns : Promise<object[]>

Promise that resolves to list of buckets

Async removeFile
removeFile(bucketName, filename)

Function to remove file froom mentioned bucket name

Parameters :
Name Optional
bucketName No
filename No
Returns : Promise<void>

Promise void

Async uploadFile
uploadFile(bucketName, filename, fileStream)

Function to upload file to mentioned bucket and filename

Parameters :
Name Optional Description
bucketName No

name of bucket

filename No

absolute path for file wrt bucket

fileStream No

buffer

Returns : Promise<object>

Promise that resolves to uploaded object info

import { Injectable } from "@nestjs/common";
import * as Minio from "minio";

@Injectable()
export class MinioProvider {
  /**
   * Function to get api client
   * @returns
   */
  getApiClient(): Minio.Client {
    return new Minio.Client({
      endPoint: process.env.STORAGE_PROVIDER_ENDPOINT,
      port: Number(process.env.STORAGE_PROVIDER_PORT),
      useSSL: false,
      accessKey: process.env.STORAGE_PROVIDER_ACCESS_KEY,
      secretKey: process.env.STORAGE_PROVIDER_SECRET,
    });
  }

  /**
   * Function to list available buckets
   * @returns Promise that resolves to list of buckets
   */
  async listBuckets(): Promise<object[]> {
    const client = await this.getApiClient();
    return client.listBuckets();
  }

  /**
   * Function to upload file to mentioned bucket and filename
   * @param bucketName name of bucket
   * @param filename absolute path for file wrt bucket
   * @param fileStream buffer
   * @returns Promise that resolves to uploaded object info
   */
  async uploadFile(bucketName, filename, fileStream): Promise<object> {
    const client = await this.getApiClient();
    return client.putObject(bucketName, filename, fileStream);
  }

  /**
   * Function to generate a Temporary download URL for filename
   * withing bucketname
   * @param bucketName name of bucket
   * @param filename absolute path for file wrt bucket
   * @returns Promise that resolves to url string
   */
  async getTemporarySharedURL(bucketName, filename): Promise<string> {
    const client = await this.getApiClient();
    return client.presignedGetObject(bucketName, filename);
  }

  /**
   * Function to remove file froom mentioned bucket name
   * @param bucketName
   * @param filename
   * @returns Promise void
   */
  async removeFile(bucketName, filename): Promise<void> {
    const client = await this.getApiClient();
    return client.removeObject(bucketName, filename);
  }

  /**
   * Function to create minio bucket
   * @param bucketName
   * @returns Promise void
   */
  async createBucket(bucketName): Promise<void> {
    const client = await this.getApiClient();
    return client.makeBucket(bucketName);
  }

  /**
   * check if bucket with name exists
   * @param bucketName
   * @returns Promise boolean
   */
  async bucketExists(bucketName): Promise<boolean> {
    const client = await this.getApiClient();
    return client.bucketExists(bucketName);
  }

  /**
   * Function to get file from bucket
   * @param bucketName
   * @param fileName
   * @returns Promise ReadableStream
   */
  async getFile(
    bucketName: string,
    fileName: string,
  ): Promise<NodeJS.ReadableStream> {
    const client = await this.getApiClient();
    try {
      const stream = await client.getObject(bucketName, fileName);
      return stream;
    } catch (error) {
      throw new Error(`Error retrieving file: ${error.message}`);
    }
  }
}

results matching ""

    No results matching ""