Class: NodeMinio

NodeMinio(config, bucket)

new NodeMinio(config, bucket)

Minio Helper Initialize Node Minio Object
Parameters:
Name Type Description
config object
bucket string
Source:
Example
const NodeMinio = require('node-minio');

const config = {
    minio: {
      endPoint: "192.168.23.100",
      accessKey: "SN8JBGY43WPMFT0R56fsdfdsf",
      secretKey: "fdfdfdfd+3R9e/x4V0F6Xpjtfdsfd",
      secure: false,
      port: 8000,
    },
    errorFileUrl: "/sample/errorfile.pdf",
    bucket: "sample"
}

 const Minio = new NodeMinio(config);

Methods

base64Upload(minioObject) → {object}

Takes and object with 2 attributes {object, base64String}. Asynchronously uploads file on specified path and returns promise
Parameters:
Name Type Description
minioObject object Contains an object with filepath, bucket and base64string
Source:
Returns:
returns promise object
Type
object
Example
const NodeMinio = require('node-minio');

const config = {
    minio: {
        endPoint: process.env.MINIO_ENDPOINT,
        accessKey: process.env.MINIO_ACCESS_KEY,
        secretKey: process.env.MINIO_SECRET_KEY,
        secure: false,
        port: 8000,
      },
      errorFileUrl: "/sample/errorfile.pdf"
  }

const Minio = new NodeMinio(config);

async function uploadFile(file) {
    const { base64: base64String, filename } = file;
    const extension = filename.split('.').pop();
    const fileName = moment().format('YYYY_MM_DD_hh_mm_ss_a');
    const object = `sampleFile/${fileName}.${extension}`;

    await Minio.base64Upload({ object, base64String });
    return object;
  }

base64UploadMulti()

Takes and object with 2 attributes {object, bucket, expires}. Uploads multiple base64 files. Used in sync with Multer
Source:

bufferUpload(minioObject) → {object}

Takes and object with 3 or 4 attributes {object, base64String, bucket, buffer}. Uploads file in specified minio bucket. This function is internally used by base64Upload function
Parameters:
Name Type Description
minioObject object Contains an object with filepath, bucket and base64string
Source:
Returns:
returns promise object
Type
object
Example
const NodeMinio = require('node-minio');

const config = {
    minio: {
        endPoint: process.env.MINIO_ENDPOINT,
        accessKey: process.env.MINIO_ACCESS_KEY,
        secretKey: process.env.MINIO_SECRET_KEY,
        secure: false,
        port: 8000,
      },
      errorFileUrl: "/sample/errorfile.pdf"
  }

const Minio = new NodeMinio(config);

async function uploadFile(file) {
    const { base64: base64String, filename } = file;
    const extension = filename.split('.').pop();
    const fileName = moment().format('YYYY_MM_DD_hh_mm_ss_a');
    const object = `sampleFile/${fileName}.${extension}`;
    const buffer = Buffer.from(base64String, 'base64');

    await Minio.bufferUpload({ object, base64String, buffer });
    return object;
  }

customCopyObject()

Takes and object with 2 attributes {object, bucket, expires}. Copies file from one bucket to another
Source:
Takes an object with 2 attributes {object, name}. Create a downloadable link for file
Parameters:
Name Type Description
minioObject object Contains an object with bucket, filepath
Source:
Returns:
returns url
Type
url
Example
const Minio = require('node-minio');

async function downloadFile(name) {
      const file = {
          object: 'sampleFiles/',
          name: `${name}_${
            moment().format('YYYY-DD-MM')}.${ext}`,
        };

      return Minio.downloadLink(file)
            .then(downloadLink => res.redirect(downloadLink));
            }

downloadLinkBase(minioObject) → {url}

Takes an object with 2 attributes {object, name}. Create a downloadable link for file
Parameters:
Name Type Description
minioObject object Contains an object with bucket, filepath
Source:
Returns:
returns url
Type
url
Example
const Minio = require('node-minio');

async function downloadFile(name) {
      const file = {
          object: 'sampleFiles/',
          name: `${name}_${
            moment().format('YYYY-DD-MM')}.${ext}`,
        };

      return Minio.downloadLinkBase(file)
            .then(downloadLink => res.redirect(downloadLink));
            }

getFileStream()

Takes and object with 2 attributes {object, bucket, expires}. Gets object from minio as a stream
Source:

listDirectoryObjects(minioObject, FSCompat) → {object}

Takes an object with 1 attributes {object}. Lists all the directories in current folder
Parameters:
Name Type Description
minioObject object Contains an object with filepath, bucket, expiry time
FSCompat boolean Removes forward slash (/) from file path
Source:
Returns:
returns promise object
Type
object
Example
const Minio = require('node-minio');

async function listFilesInDirectory() {

 const baseUrl = "/sampleFile";
  let fileList = await Minio.listDirectoryObjects({ object: baseUrl });
  return fileList;
  }
Takes and object with 2 attributes {object}. Used to retry alternative file to be downloaded. Here in tha example below we dont get original file than we search for -rst file and try download it
Parameters:
Name Type Description
minioObject object Contains an object with filepath *
Source:
Returns:
returns download link
Type
object
Example
const Minio = require('node-minio');

async function retryFileFromMinio(fileName) {
     const retryObject = fileName.path.toLowerCase();
     const name = `${fileName}.pdf`.replace(/ /g, '_');

   const url = await Minio
    .retryDownloadLink({
    name,
    retryObject,
    object: retryObject.replace(/\.pdf$/g, '-rst.pdf'),
  });
  }
Takes and object with 2 attributes {object, bucket, expires}. Generates and returns presigned URL for HTTP PUT operations. Clients may point to this URL to upload objects directly to a bucket even if it is private. This presigned URL can have an associated expiration time in seconds after which the URL is no longer valid. The default value is 7 days.
Parameters:
Name Type Description
minioObject object Contains an object with option bucket name and expiry
Source:
Returns:
returns url which can be used to upload file
Type
object
Example
https://docs.min.io/docs/upload-files-from-browser-using-pre-signed-urls.html
https://docs.min.io/docs/javascript-client-api-reference.html#presignedPutObject
const Minio = require('node-minio');

async function getUploadLink(req,res) {
   const url = await Minio
    .uploadLink({});
  return res.json(url);
  }

uploadTemp(minioObject) → {object}

Uploads base64 file from temp path(specified path) to bucket
Parameters:
Name Type Description
minioObject object Contains an object with option bucket
Source:
Returns:
returns promise
Type
object
Example
const Minio = require('node-minio');

async function uploadFromTemp(req, res, file) {
const extension = (file.name || file.filename).split('.').pop().toLowerCase();
        minioObject = {
          base64String: file.base64,
          temp: file.path,
          object: `sample/${Id}/${Id
          }_${moment().format('DD-MM-YYYY_hh-mm-ss-a')}.${extension}`,
        };
   const file = await Minio
    .uploadTemp(minioObject);
  return res.json(file);
  }
Takes and object with 2 attributes {object, bucket, expires}. Generates dynamic Minio URL for accessing file with expiry
Parameters:
Name Type Description
minioObject object Contains an object with filepath, bucket, expiry time
FSCompat boolean Removes forward slash (/) from file path
Source:
Returns:
returns promise object
Type
object
Example
const NodeMinio = require('node-minio');

const config = {
    minio: {
        endPoint: process.env.MINIO_ENDPOINT,
        accessKey: process.env.MINIO_ACCESS_KEY,
        secretKey: process.env.MINIO_SECRET_KEY,
        secure: false,
        port: 8000,
      },
      errorFileUrl: "/sample/errorfile.pdf"
  }

const Minio = new NodeMinio(config);

async function viewFile(fileName) {
    const filePath= 'sampleFile/${fileName}.pdf'

   const url = await minio
    .viewLink({
      object: filePath,
    }, false);
  return res.json(url);
  }