Skip to content

Instantly share code, notes, and snippets.

@nesimtunc
Last active July 21, 2024 03:03
Show Gist options
  • Save nesimtunc/fe46687589a6940fbe941dee99d3ba89 to your computer and use it in GitHub Desktop.
Save nesimtunc/fe46687589a6940fbe941dee99d3ba89 to your computer and use it in GitHub Desktop.
Minio Server - NodeJS - File Upload & Photo Showing Example
const formidable = require('formidable');
const fs = require('fs');
var Minio = require('minio');
let minioClient = new Minio.Client({
endPoint: config.MINIO.ENDPOINT,
port: config.MINIO.PORT,
accessKey: config.MINIO.ACCESS_KEY,
secretKey: config.MINIO.SECRET_KEY,
secure: false
});
let minioBucketName = 'photos';
minioClient.bucketExists(minioBucketName, function(err) {
if (err) {
if (err.code == 'NoSuchBucket') {
minioClient.makeBucket(minioBucketName, 'us-east-1', function(err2) {
if (err2) {
console.log("error on creating bucket", err2);
}
});
}
}
console.log('Bucket exists:', minioBucketName);
});
module.exports = {
uploadImage(req, res, next) {
const form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
const myfile = files.myfile;
var fileStream = fs.createReadStream(myfile.path);
var fileStat = fs.stat(myfile.path, function(err2, stats) {
if (err2) {
return console.log(err)
}
minioClient.putObject(minioBucketName, myfile.name, fileStream, stats.size, function(err3, etag) {
if (err3) {
return res.status(500).send(err3);
}
return res.send(etag);
})
});
});
},
getImage(req, res, next) {
let data;
minioClient.getObject(minioBucketName, req.params.id, function(err, objStream) {
if (err) {
return console.log(err)
}
objStream.on('data', function(chunk) {
data = !data ? new Buffer(chunk) : Buffer.concat([data, chunk]);
})
objStream.on('end', function() {
res.writeHead(200, {'Content-Type': 'image/jpeg'});
res.write(data);
res.end();
})
objStream.on('error', function(err) {
res.status(500);
res.send(err);
})
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment