If you want to upload files to MinIO using Node.js, you can use the official MinIO SDK.
1. Install the MinIO SDK
npm install minio
2. Create a MinIO client
const Minio = require("minio");
const minioClient = new Minio.Client({
endPoint: "localhost",
port: 9000,
useSSL: false,
accessKey: "minioadmin",
secretKey: "minioadmin",
});
Replace the values with your MinIO server configuration.
3. Upload a file
const fs = require("fs");
const bucketName = "uploads";
const objectName = "example.txt";
const filePath = "./example.txt";
async function uploadFile() {
try {
// Create bucket if it doesn't exist
const exists = await minioClient.bucketExists(bucketName);
if (!exists) {
await minioClient.makeBucket(bucketName, "us-east-1");
console.log("Bucket created.");
}
// Upload file
await minioClient.fPutObject(
bucketName,
objectName,
filePath,
{
"Content-Type": "text/plain",
}
);
console.log("File uploaded successfully!");
} catch (err) {
console.error(err);
}
}
uploadFile();
Upload using Express + Multer
Install dependencies:
npm install express multer minio
const express = require("express");
const multer = require("multer");
const Minio = require("minio");
const app = express();
const upload = multer({ storage: multer.memoryStorage() });
const minioClient = new Minio.Client({
endPoint: "localhost",
port: 9000,
useSSL: false,
accessKey: "minioadmin",
secretKey: "minioadmin",
});
app.post("/upload", upload.single("file"), async (req, res) => {
try {
const bucketName = "uploads";
const exists = await minioClient.bucketExists(bucketName);
if (!exists) {
await minioClient.makeBucket(bucketName, "us-east-1");
}
const objectName = `${Date.now()}-${req.file.originalname}`;
await minioClient.putObject(
bucketName,
objectName,
req.file.buffer,
req.file.size,
{
"Content-Type": req.file.mimetype,
}
);
res.json({
success: true,
fileName: objectName,
});
} catch (err) {
console.error(err);
res.status(500).json({
success: false,
error: err.message,
});
}
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Test with cURL
curl -X POST http://localhost:3000/upload \
-F "file=@/path/to/image.jpg"
Response
{
"success": true,
"fileName": "1752412345678-image.jpg"
}
This approach avoids saving the file locally by uploading it directly from memory to MinIO.
If you're using NestJS, Fastify, or TypeScript, I can provide an example tailored to that framework as well.