GridFS storage engine for Multer to store uploaded files directly to MongoDb.
Using npm
$ npm install multer-gridfs-storage --save
Basic usage example:
const express = require('express');
const multer = require('multer');
const {GridFsStorage} = require('multer-gridfs-storage');
const url = 'mongodb://yourhost:27017/database';
// Create a storage object with a given configuration
const storage = new GridFsStorage({ url });
// Set multer storage engine to the newly created object
const upload = multer({ storage });
const app = express();
// Upload your files as usual
app.post('/profile', upload.single('avatar'), (req, res, next) => {
/*....*/
});
app.post('/photos/upload', upload.array('photos', 12), (req, res, next) => {
/*....*/
});
app.post('/cool-profile', upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]), (req, res, next) => {
/*....*/
});
The module returns a function that can be invoked to create a Multer storage engine. It also works as a class. It is up to you to decide the best way to invoke it.
Check the wiki for an in depth guide on how to use this module.
The configuration parameter is an object with the following properties.
Type: string
Required if db
option is not present
An url pointing to the database used to store the incoming files.
With this option the module will create a mongodb connection for you. It must be a standard mongodb connection string.
If the db
option is specified this setting is ignored.
Example:
const {GridFsStorage} = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://yourhost:27017/database'
});
The connected database is available in the storage.db
property.
On mongodb v3 the client instance is also available in the storage.client
property.
Type: object
Not required
This setting allows you to customize how this module establishes the connection if you are using the url
option.
You can set this to an object like is specified in the MongoClient.connect
documentation and change the default behavior without having to create the connection yourself using the db
option.
Type: boolean
or string
Not required
Default value: false
Store this connection in the internal cache. You can also use a string to use a named cache. By default caching is disabled. See caching to learn more about reusing connections.
This option only applies when you use an url string to connect to MongoDb. Caching is not enabled when you create instances with a database object directly.
Type: DB
or Promise
Required if url
option is not present
The database connection to use, or a promise that resolves with the connection object. Mongoose Connection
objects are supported too.
This is useful to reuse an existing connection to create more storage objects.
Example:
// using a database instance
const client = await MongoClient.connect('mongodb://yourhost:27017');
const database = client.db('database');
const storage = new GridFsStorage({ db: database });
// using a promise
const promise = MongoClient
.connect('mongodb://yourhost:27017')
.then(client => client.db('database'));
const storage = new GridFsStorage({ db: promise });
// using Mongoose
const connection = mongoose.connect('mongodb://yourhost:27017/database');
const storage = new GridFsStorage({ db: connection });
// mongodb v2
const {GridFsStorage} = require('multer-gridfs-storage');
// using a database instance
const database = await MongoClient.connect('mongodb://yourhost:27017/database');
const storage = new GridFsStorage({ db: database });
// using a promise
const promise = MongoClient.connect('mongodb://yourhost:27017/database');
const storage = new GridFsStorage({ db: promise });
If you used the db
option to initialize the storage engine you can also include the client
generated by calling the MongoClient.connect
method in this option.
Using promises is also supported
// including the client in the storage
const client = await MongoClient.connect('mongodb://yourhost:27017');
const db = client.db('database');
const storage = new GridFsStorage({ db, client});
// using a promise
const client = MongoClient.connect('mongodb://yourhost:27017');
const db = client.then(cl => cl.db('database'));
const storage = new GridFsStorage({ db, client});
Using this feature is highly recommended in order to keep the storage in sync with the underlying connection status and to make your code more resilient to future changes in the mongodb library.
Type: function
or function*
Not required
A function to control the file storage in the database. Is invoked per file with the parameters req
and file
, in that order.
This module uses GridFSBucket
to store files in the database falling back to GridStore
in case the previous class is not found like, for example, in earlier versions of MongoDb.
By default, naming behaves exactly like the default Multer disk storage, a 16 bytes long name in a hexadecimal format with no extension is generated for each file to guarantee that there are very low probabilities of collisions. You can override this by passing your own function.
The return value of this function is an object, or a promise that resolves to an object (this also applies to generators) with the following properties.
Property name | Description |
---|---|
filename |
The desired filename for the file (default: 16 byte hex name without extension) |
id |
An ObjectID to use as identifier (default: auto-generated) |
metadata |
The metadata for the file (default: null ) |
chunkSize |
The size of file chunks in bytes (default: 261120) |
bucketName |
The GridFs collection to store the file (default: fs ) |
contentType |
The content type for the file (default: inferred from the request) |
aliases |
Optional array of strings to store in the file document's aliases field (default: null ) |
disableMD5 |
If true, disables adding an md5 field to file data (default: false , available only on MongoDb >= 3.1) |
Any missing properties will use the defaults. Also, note that each property must be supported by your installed version of MongoDb.
If you return null
or undefined
from the file function, the values for the current file will also be the defaults. This is useful when you want to conditionally change some files while leaving others untouched.
This example will use the collection 'photos'
only for incoming files whose reported mime-type is image/jpeg
, the others will be stored using default values.
const {GridFsStorage} = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://host:27017/database',
file: (req, file) => {
if (file.mimetype === 'image/jpeg') {
return {
bucketName: 'photos'
};
} else {
return null;
}
}
});
const upload = multer({ storage });
This other example names every file something like 'file_1504287812377'
, using the date to change the number and to generate unique values
const {GridFsStorage} = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://host:27017/database',
file: (req, file) => {
return {
filename: 'file_' + Date.now()
};
}
});
const upload = multer({ storage });
Is also possible to return values other than objects, like strings or numbers, in which case they will be used as the filename and the remaining properties will use the defaults. This is a simplified version of a previous example
const {GridFsStorage} = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://host:27017/database',
file: (req, file) => {
// instead of an object a string is returned
return 'file_' + Date.now();
}
});
const upload = multer({ storage });
Internally the function crypto.randomBytes
is used to generate names. In this example, files are named using the same format plus the extension as received from the client, also changing the collection where to store files to uploads
.
const crypto = require('crypto');
const path = require('path');
const {GridFsStorage} = require('multer-gridfs-storage');
var storage = new GridFsStorage({
url: 'mongodb://host:27017/database',
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
Each saved file located in req.file
and req.files
contain the following properties in addition to the ones that Multer create by default. Most of them can be set using the file
configuration.
Key | Description |
---|---|
filename |
The name of the file within the database |
metadata |
The stored metadata of the file |
id |
The id of the stored file |
bucketName |
The name of the GridFs collection used to store the file |
chunkSize |
The size of file chunks used to store the file |
size |
The final size of the file in bytes |
md5 |
The md5 hash of the file |
contentType |
Content type of the file in the database |
uploadDate |
The timestamp when the file was uploaded |
To see all the other properties of the file object, check the Multer's documentation.
Do not confuse
contentType
with Multer'smimetype
. The first is the value in the database while the latter is the value in the request. You could choose to override the value at the moment of storing the file. In most cases both values should be equal.
You can enable caching by either using a boolean, or a non-empty string in the cache option, then, when the module is invoked again with the same url it will use the stored db instance instead of creating a new one.
The cache is not a simple object hash. It supports handling asynchronous connections. You could, for example, synchronously create two storage instances for the same cache one after the other and only one of them will try to open a connection.
This greatly simplifies managing instances in different files of your app. All you have to do now is to store a url string in a configuration file to share the same connection. Scaling your application with a load-balancer, for example, can lead to spawn a great number of database connections for each child process. With this feature no additional code is required to keep opened connections to the exact number you want without any effort.
You can also create named caches by using a string instead of a boolean value. In those cases, the module will uniquely identify the cache allowing for an arbitrary number of cached connections per url and giving you the ability to decide which connection to use and how many of them should be created.
The following code will create a new connection and store it under a cache named 'default'
.
const {GridFsStorage} = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://yourhost:27017/database',
cache: true
});
Other, more complex example, could be creating several files and only two connections to handle them.
// file 1
const {GridFsStorage} = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://yourhost:27017/database',
cache: '1'
});
// file 2
const {GridFsStorage} = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://yourhost:27017/database',
cache: '1'
});
// file 3
const {GridFsStorage} = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://yourhost:27017/database',
cache: '2'
});
// file 4
const {GridFsStorage} = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://yourhost:27017/database',
cache: '2'
});
The files 1 and 2 will use the connection cached under the key '1'
and the files 3 and 4 will use the cache named '2'
. You don't have to worry about managing connections anymore. By setting a simple string value the module manages them for you automatically.
Connection strings are parsed and tested for similarities. In this example the urls are equivalent and only one connection will be created.
const {GridFsStorage} = require('multer-gridfs-storage');
// Both configurations are equivalent
const storage1 = new GridFsStorage({
url: 'mongodb://host1:27017,host2:27017/database',
cache: 'connections'
});
const storage2 = new GridFsStorage({
url: 'mongodb://host2:27017,host1:27017/database',
cache: 'connections'
});
Of course if you want to create more connections this is still possible. Caching is disabled by default so setting a cache: false
or not setting any cache configuration at all will cause the module to ignore caching and create a new connection each time.
Using options has a particular side effect. The cache will spawn more connections only when they differ in their values. Objects provided here are not compared by reference as long as they are just plain objects. Falsey values like null
and undefined
are considered equal. This is required because various options can lead to completely different connections, for example when using replicas or server configurations. Only connections that are semantically equivalent are considered equal.
generateBytes
A shortcut for crypto.randomBytes
which uses promises instead of callbacks to generate names and return the value in a property called filename
.
const {GridFsStorage} = require('multer-gridfs-storage');
const {generateBytes} = GridFsStorage;
const result = await generateBytes();
// result will be something like {filename: '37492f9fe13c350667350bcacf0e5b19'}
fromStream
A function that pipe a readable stream to gridfs using the current storage configuration. Useful if you want to upload the received file in multiple storage devices.
const {GridFsStorage} = require('multer-gridfs-storage');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const app = express();
const storage = new GridFsStorage({url: 'mongodb://yourhost:27017/database'});
app.post('/profile', upload.single('avatar'), function (req, res, next) {
const {file} = req;
const stream = fs.createReadStream(file.path);
storage.fromStream(stream, req, file)
.then(() => res.send('File uploaded'))
.catch(() => res.status(500).send('error'));
});
Each storage has a ready
method that returns a promise. This allows you to watch for the MongoDb connection instead of using events. These two examples are equivalent.
// Using event emitters
const storage = new GridFsStorage({
url: 'mongodb://yourhost:27017/database'
});
storage.on('connection', (db) => {
// Db is the database instance
});
storage.on('connectionFailed', (err) => {
// err is the error received from MongoDb
});
// Using the ready method
const storage = new GridFsStorage({
url: 'mongodb://yourhost:27017/database'
});
try {
const {db, client} = await storage.ready();
// db is the database instance
// client is the MongoClient instance
} catch (err) {
// err is the error received from MongoDb
}
Remember that you don't need to wait for the connection to be ready to start uploading files. The module buffers every incoming file until the connection is ready and saves all of them as soon as possible.
The ready
method is just a convenience function over code written using the connection
events also with a couple of advantages. If you set up a listener after the connection
or connectionFailed
events are dispatched your code will not execute while using the ready
method it will. The module keeps track of these events and resolves or rejects the promises accordingly. Promises in this case are more readable than events and more reliable.
Each storage object is also a standard Node.js Event Emitter. This is done to ensure that some internal events can also be handled in user code.
'connection'
This event is emitted when the MongoDb connection is ready to use.
Event arguments
result: Result is an object with the following properties:
db
: The MongoDb database pointing to the database
client
: The MongoClient instance that holds the connection
This event is triggered at most once.
'connectionFailed'
This event is emitted when the connection could not be opened.
This event only triggers at most once.
Only one of the events
connection
orconnectionFailed
will be emitted.
'file'
This event is emitted every time a new file is stored in the db.
Event arguments
'streamError'
This event is emitted when there is an error streaming the file to the database.
Event arguments
Previously this event was named
error
but in Nodeerror
events are special and crash the process if one is emitted and there is no listener attached. You could choose to handle errors in an express middleware forcing you to set an emptyerror
listener to avoid crashing. To simplify the issue this event was renamed to allow you to choose the best way to handle storage errors.
'dbError'
This event is emitted when the underlying connection emits an error.
Only available when the storage is created with the
url
option.
Event arguments
When using the url
feature with the option {useUnifiedTopology:true}
to create a MongoDb connection like this:
const storage = new GridFsStorage({
url: 'mongodb://yourhost:27017/database',
options: {useUnifiedTopology: true},
});
In this case the internal client always report that the connection is open even when is not. This is a known bug that you can track here.
Is recommended that you only use this option with a MongoDb version that has the bug resolved, otherwise the storage instance cannot track the connection status and features like buffering could not work properly in some scenarios.
To run the test suite, first install the dependencies, then run npm test
:
$ npm install
$ npm test
Tests are written with the ava testing framework.
Code coverage thanks to istanbul
$ npm run coverage
File storage is an important feature required in multiple processes across various types of applications. The existence of processes like Content Delivery Networks (CDNs), set up through third-party c
详细文档请前往githut学习 https://github.com/expressjs/multer/blob/master/doc/README-zh-cn.md 这里分享一下两种常用的使用: 1.将文件存储到本地 下载依赖 yarn add multer npm i multer //导入包 const multer = require('multer') //dest 指定存储文件的本地
Maven依赖(还有一些springboot需要的) <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </p
最近开发一个web是用nodejs开发的,有个上传文档的功能 mongo链接端 首先npm install下载需要的模块 mongo中fs.files中有字段filesname,metadata,利用metadata作为文档的分类 var mongoose = require('mongoose'); var conn = mongoose.createConnection('mongodb://
1. 配置config spring: data: mongodb: uri: mongodb://username:password@192.168.2.72:27017 database: mydb @Component public class WebConfig { @Value("${spring.data.mongodb.database}"
GridFS 是Mongo 的一种存储机制,用来存储大型二进制文件。 1. GridFS是用于存储和检索超过16MB的BSON文档大小限制的文件的解决方案。 2.GridFS是MongoDB用来存储大型二进制文件的一种存储机制。 3.GridFS 不是将文件存储在单个文档中,而是将文件分为块,并将每个块作为单独的文档存储,默认情况下,GridFS使用的块大小为256kb。 4. GridFS 使用
GridFS是MongoDB中的一个内置功能,可以用于存放大量小文件。 GridFS使用两集合来存储文件的元数据和内容。文件的元数据存储在文件集合中,文件的内容存储在块集合中。 两集合以FS。默认使用fs.chunks和fs.files来存储文件。其中fs.files集合存放文件的信息,fs.chunks存放文件数据。 有些内容参考自 http://www.server110.com/mongod
index.js 文件 const express = require('express'); const bodyParser = require('body-parser'); const path = require('path'); const { multer } = require('./model/multer'); const app = express(); app.use
此文章转载自:http://t.zoukankan.com/three-fighter-p-12641771.html#25%E3%80%81%E6%9C%8D%E5%8A%A1%E5%B1%82 https://www.cnblogs.com/three-fighter/p/12641771.html 目录 一、MongoDB存储文件 1、MongoDB存储小文件 2、MongoDB存储大文件
Express默认并不处理HTTP请求体中的数据,对于普通请求体(JSON、二进制、字符串)数据,可以使用body-parser中间件。而文件上传(multipart/form-data请求),可以基于请求流处理,也可以使用formidable模块或Multer中间件。 1. multer中间件 Multer是Express官方推出的,用于Node.jsmultipart/form-data请求数
1.存储路径---》可以理解就是存储路径,然后在通过路径来获取文件 将文件放在本地路径(网络路径)下,然后数据库中存储该文件的查找路径 db.log.insert({filename:"python.xmind",size:120,path:"/hoem/zengsf/net/mongoDB"}) 优点 : 节省数据库空间 缺点 : 当数据或者文件位置发生变化时文件即丢失 2. 将文件转换为二进制
1. Multer是什么 Multer 是一个 node.js 中间件,用于处理 multipart/form-data 类型的表单数据,它主要用于上传文件。它是写在 busboy 之上非常高效。 注意: Multer 不会处理任何非 multipart/form-data 类型的表单数据。 2. 安装 $ npm install --save multer 3. 使用 Multer 会添加一个
node中multer包的使用 //这样可以保留导入图片的文件名 const storage = multer.diskStorage({ destination:'./uploads', filename(req,file,cb){ cb(null,Date.now() + extname(file.originalname)) } }) const u
“GridFS”是用于存储和检索文件的规范。在主窗口中,点击 “GridFS”来打开 GridFS 的对象列表。 你可以在数据库中创建多个存储桶以存储文件。点击 “新建存储桶”并输入存储桶的名。 若要打开已选择的存储桶,请点击 “打开存储桶”。 存储桶查看器 “存储桶查看器”是一个用于设计 GridFS 存储桶的 Navicat 基本工具。你可以上载,下载和查看存储桶内的 GridFS 文件。 按
“GridFS”是用于存储和检索文件的规范。在主窗口中,点击 “GridFS”来打开 GridFS 的对象列表。 你可以在数据库中创建多个存储桶以存储文件。点击 ”并输入存储桶的名。 若要打开已选择的存储桶,请点击 。 存储桶查看器 “存储桶查看器”是一个用于设计 GridFS 存储桶的 Navicat 基本工具。你可以上载,下载和查看存储桶内的 GridFS 文件。 按钮 描述 打开已选择的 G
“GridFS”是用于存储和检索文件的规范。在主窗口中,点击 “GridFS”来打开 GridFS 的对象列表。 你可以在数据库中创建多个存储桶以存储文件。点击 “新建存储桶”并输入存储桶的名。 若要打开已选择的存储桶,请点击 “打开存储桶”。 存储桶查看器 “存储桶查看器”是一个用于设计 GridFS 存储桶的 Navicat 基本工具。你可以上载,下载和查看存储桶内的 GridFS 文件。 按
GridFS是一种将大型文件存储在MongoDB的文件规范。 数据库支持以BSON格式保存二进制对象。 但是MongoDB中BSON对象最大不能超过4MB。 GridFS 规范提供了一种透明的机制,可以将一个大文件分割成为多个较小的文档。这将容许我们有效的保存大的文件对象,特别对于那些巨大的文件,比如视频。 为实现这点,该规范指定了一个将文件分块的标准。每个文件都将在文件集合对象中保存一个元数
主要内容:将文件添加到 GridFSGridFS 是 MongoDB 的一种规范,用于存储和检索大型文件,如图像、音频、视频等。GridFS 也是一种存储文件的文件系统,但其数据是存储在 MongoDB 集合中的,GridFS 甚至可以存储超过 16MB 的文件。在存储文件时 GridFS 可以将一个文件分为多个数据块,并将每个数据块存储在一个单独的文档中,每个文档最大为 255KB。 默认情况下,GridFS 使用 fs.file
nginx-gridfs 是一个 Nginx 的扩展模块,用于支持直接访问 MongoDB 的 GridFS 文件系统上的文件并提供 HTTP 访问。