How to use Minio in a Node project
We will learn to use minio which is open source version of AWS S3 in a node project, we will be storing a image on a bucket inside minio.
Download Minio on Windows
To download the minio executable file click on the following link Minio.exe and remember this executable file will not open like an ordinary executable file (By double clicking it)
Launch the Minio Server on Localhost
To run the following command to the location where you downloaded the minio executable and run the following command:
minio.exe server C:\minio --console-address :9090
After running the command you will see the following on the screen
API: http://192.0.2.10:9000 http://127.0.0.1:9000 RootUser: minioadmin RootPass: minioadmin Console: http://192.0.2.10:9090 http://127.0.0.1:9090 RootUser: minioadmin RootPass: minioadmin Command-line: https://min.io/docs/minio/linux/reference/minio-mc.html $ mc alias set myminio http://192.0.2.10:9000 minioadmin minioadmin Documentation: https://min.io/docs/minio/linux/index.html WARNING: Detected default credentials 'minioadmin:minioadmin', we recommend that you change these values with 'MINIO_ROOT_USER' and 'MINIO_ROOT_PASSWORD' environment variables.
Connect your browser to Minio
Navigate to any of the APIs on your favorite browser and give the
username
andpassword
from the command lineAfter clicking on Create access key the window below should occur and click on Create
On clicking create the dialog box below should occur and note down the details
Initialize a new Node js Project
Run the below command inside an empty folder to initialize a new node project
npm init
After initialization run the below command to install the minio dependency
npm install --save minio
Copy the code below inside the index.js file
Create an
index.js
file inside the project folder and copy the following content into itvar Minio = require("minio"); var minioClient = new Minio.Client({ endPoint: "Your IP Adress", port: "Port Number", useSSL: false, accessKey: "LJjliHpAHiHZUFvd", secretKey: "7IYKuE0A0Jc4aNdC6MDKxkE67LnPiqFD", }); var file = "./file/my_image.jpeg"; minioClient.makeBucket("myimage", "us-east-1", function (err) { console.log("make bucket"); if (err) return console.log(err); console.log('Bucket created successfully in "us-east-1".'); var metaData = { "Content-Type": "image/jpeg", "X-Amz-Meta-Testing": 1234, example: 5678, }; minioClient.fPutObject( "myimage", "my_image.jpeg", file, metaData, function (err, etag) { if (err) return console.log(err); console.log("File uploaded successfully."); } ); });
Project Structure below:
Run the project
To run the project write the command
node index.js
in the terminal and voila you will see a bucket create by the name ofmyimage
You will be able to see the my_image.jpeg
inside the bucket when you click on browse