Connect to Minio from NodeJS

Minio is designed for storing unstructured data such as photos, videos, log files, backups, and more. This guide will walk you through the process of accessing Minio from a Node.js application, including setting up the environment, connecting to Minio, and performing basic operations like uploading and downloading files.
This article is part of Mino tutorial series.
Prerequisites
Before you begin, ensure you have the following:
- A running instance of Minio server.
- Node.js installed on your machine.
- Basic knowledge of JavaScript and Node.js.
Setting Up Minio Server
Ensure your Minio server is running. If it’s not already started, you can initiate it with the following command:
minio server /path/to/data
Minio will be accessible at http://localhost:9000 by default. Note down the access key, secret key, and the URL.
Installing Minio Client for Node.js
To interact with Minio using Node.js, you need to install the Minio client library. This can be done using npm:
npm install minio
Connecting to Minio from Node.js
First, create a Node.js script and import the Minio client library. Then, create a Minio client instance in your script.
const Minio = require('minio');
// Initialize Minio client
const minioClient = new Minio.Client({
endPoint: 'localhost',
port: 9000,
useSSL: false,
accessKey: 'minioadmin',
secretKey: 'minioadmin123'
});
// Check if the bucket exists
const bucketName = 'mybucket';
minioClient.bucketExists(bucketName, (err, exists) => {
if (err) {
return console.log(err);
}
if (exists) {
console.log(`Bucket ${bucketName} already exists`);
} else {
// Make a new bucket
minioClient.makeBucket(bucketName, 'us-east-1', (err) => {
if (err) {
return console.log(err);
}
console.log(`Bucket ${bucketName} created successfully`);
});
}
});
Replace 'localhost', 9000, 'minioadmin', and 'minioadmin123' with your Minio server URL, port, access key, and secret key.
Uploading a File to Minio
To upload a file to Minio, use the fPutObject method.
const filePath = '/path/to/file';
const objectName = 'my-object';
minioClient.fPutObject(bucketName, objectName, filePath, (err, etag) => {
if (err) {
return console.log(err);
}
console.log(`File uploaded successfully. ETag: ${etag}`);
});
Downloading a File from Minio
To download a file from Minio, use the fGetObject method.
const downloadPath = '/path/to/downloaded-file';
minioClient.fGetObject(bucketName, objectName, downloadPath, (err) => {
if (err) {
return console.log(err);
}
console.log(`File downloaded successfully to ${downloadPath}`);
});
Conclusion
Accessing Minio from a Node.js application is straightforward with the Minio Node.js client. Throughout this guide, we have covered everything from setting up the environment to uploading and downloading files. By following these steps, you can leverage Minio’s robust object storage capabilities in your Node.js applications, providing scalable and efficient data management solutions.

