1 |
|
2 | # gridfs-stream
|
3 |
|
4 | Easily stream files to and from MongoDB [GridFS](http://www.mongodb.org/display/DOCS/GridFS).
|
5 |
|
6 | ```js
|
7 | var gfs = require('gridfs-stream')(db);
|
8 |
|
9 | // streaming to gridfs
|
10 | var writestream = gfs.createWriteStream('filename');
|
11 | fs.createReadStream('/some/path').pipe(writestream);
|
12 |
|
13 | // streaming from gridfs
|
14 | var readstream = gfs.createReadStream('filename');
|
15 | readstream.pipe(response);
|
16 | ```
|
17 |
|
18 | Created streams are compatible with other Node streams so piping anywhere is easy.
|
19 |
|
20 | ## install
|
21 |
|
22 | ```
|
23 | npm install gridfs-stream
|
24 | ```
|
25 |
|
26 | ## use
|
27 |
|
28 | The `gridfs-stream` module exports a function that accepts a [mongodb-native](https://github.com/mongodb/node-mongodb-native/) db. The db must already be opened before passing it in.
|
29 |
|
30 | ```js
|
31 | var gfs = require('gridfs-stream')(db);
|
32 | ```
|
33 |
|
34 | Now we're ready to start streaming.
|
35 |
|
36 | ## createWriteStream
|
37 |
|
38 | To stream data to GridFS we call `createWriteStream` passing a filename and any options.
|
39 |
|
40 | ```js
|
41 | var writestream = gfs.createWriteStream('filename' [, options]);
|
42 | fs.createReadStream('/some/path').pipe(writestream);
|
43 | ```
|
44 |
|
45 | ## createReadStream
|
46 |
|
47 | To stream data out of GridFS we call `createReadStream` passing a filename and any options.
|
48 |
|
49 | ```js
|
50 | var readstream = gfs.createReadStream('filename' [, options]);
|
51 | readstream.pipe(response);
|
52 | ```
|
53 |
|
54 | Any options are passed to the internally created [GridStore](http://mongodb.github.com/node-mongodb-native/api-generated/gridstore.html).
|
55 |
|
56 | [LICENCE](https://github.com/aheckmann/gridfs-stream/blob/master/LICENSE)
|