Streaming multer storage engine for minio.
This project is mostly an integration piece for existing code samples from Multer's storage engine documentation with MinIO Client SDK for Javascript as the substitution piece for file system. Existing solutions I found required buffering the multipart uploads into the actual filesystem which is difficult to scale.
yarn add multer-minio-storage-engine
const Minio = require('minio');
const express = require('express');
const multer = require('multer');
const multerMinio = require('multer-minio-storage-engine');
const app = express();
const minioClient = new Minio.Client({
/* ... */
});
const upload = multer({
storage: multerMinio({
minio: minioClient,
bucketName: 'some-bucket',
metaData: function (req, file, cb) {
cb(null, {mimetype: file.mimetype});
},
objectName: function (req, file, cb) {
cb(null, Date.now().toString());
},
}),
});
app.post('/upload', upload.array('photos', 3), function (req, res, next) {
res.send('Successfully uploaded ' + req.files.length + ' files!');
});
Each file contains the following information exposed by multer-minio-storage-engine
:
Key | Description | Note |
---|---|---|
size |
Size of the file in bytes | |
bucketName |
The bucket used to store the file | S3Storage |
objectName |
The name of the object | S3Storage |
contentType |
The mimetype used to upload the file |
S3Storage |
metaData |
The metaData object to be sent to S3 |
S3Storage |
etag |
The etag of the uploaded file in S3 |
S3Storage |
The metaData
option is a callback that accepts the request and file, and returns a metaData object to be saved to S3.
Here is an example that stores all fields in the request body as metaData, and uses an id
param as the objectName:
const opts = {
minio: minioClient,
bucketName: 'some-bucket',
metaData: function (req, file, cb) {
cb(null, Object.assign({}, req.body));
},
objectName: function (req, file, cb) {
cb(null, req.params.id + '.jpg');
},
};