gridfs.txt 6.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
======
GridFS
======

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

13
:manual:`GridFS </core/gridfs>` is a specification for storing and retrieving
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
files in MongoDB. GridFS uses two collections to store files. One collection
stores the file chunks (e.g. ``fs.chunks``), and the other stores file metadata
(e.g. ``fs.files``). The :phpclass:`MongoDB\\GridFS\\Bucket` class provides an
interface around these collections for working with the files as PHP
:php:`Streams <stream>`.

Creating a GridFS Bucket
------------------------

You can construct a GridFS bucket using the PHP extension's
:php:`MongoDB\\Driver\\Manager <class.mongodb-driver-manager>` class, or select
a bucket from the |php-library|'s :phpclass:`MongoDB\\Database` class via the
:phpmethod:`selectGridFSBucket() <MongoDB\\Database::selectGridFSBucket>`
method.

The bucket can be constructed with various options:

31
 - ``bucketName`` determines the prefix for the bucket's metadata and chunk
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
   collections. The default value is ``"fs"``.
 - ``chunkSizeBytes`` determines the size of each chunk. GridFS divides the
   file into chunks of this length, except for the last, which is only as large
   as needed. The default size is ``261120`` (i.e. 255 KiB).
 - ``readConcern``, ``readPreference`` and ``writeConcern`` options can be used
   to specify defaults for read and write operations, much like the
   :phpclass:`MongoDB\\GridFS\\Collection` options.

Uploading Files with Writable Streams
-------------------------------------

To upload a file to GridFS using a writable stream, you can either open a stream
and write to it directly or write the entire contents of another readable stream
to GridFS all at once.

To open an upload stream and write to it:

.. code-block:: php

   <?php

53
   $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
54 55 56 57 58 59 60 61 62 63 64 65 66

   $stream = $bucket->openUploadStream('my-file.txt');

   $contents = file_get_contents('/path/to/my-file.txt');
   fwrite($stream, $contents);
   fclose($stream);

To upload the entire contents of a readable stream in one call:

.. code-block:: php

   <?php

67
   $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

   $file = fopen('/path/to/my-file.txt', 'rb');
   $bucket->uploadFromStream('my-file.txt', $file);

Downloading Files with Readable Streams
---------------------------------------

To download a file from GridFS using a readable stream, you can either open a
stream and read from it directly or download the entire file all at once.

To open a download stream and read from it:

.. code-block:: php

   <?php

   // In practice, $fileId denotes the _id of an existing file in GridFS
   $fileId = new MongoDB\BSON\ObjectId;

87
   $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
88 89

   $stream = $bucket->openDownloadStream($fileId);
90
   $contents = stream_get_contents($stream);
91

92
To download the file all at once and write it to a writable stream:
93 94 95 96 97 98 99 100

.. code-block:: php

   <?php

   // In practice, $fileId denotes the _id of an existing file in GridFS
   $fileId = new MongoDB\BSON\ObjectId;

101
   $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

   $file = fopen('/path/to/my-output-file.txt', 'wb');

   $bucket->downloadToStream($fileId, $file);

Selecting Files by Filename and Revision
----------------------------------------

You can also download a file specified by filename and (optionally) revision
number. Revision numbers are used to distinguish between files sharing the same
``filename`` metadata field, ordered by date of upload (i.e. the ``uploadDate``
metadata field). The ``revision`` option accepted by
:phpmethod:`openDownloadStreamByName()
<MongoDB\\GridFS\\Bucket::openDownloadStreamByName>` and
:phpmethod:`downloadToStreamByName()
<MongoDB\\GridFS\\Bucket::downloadToStreamByName>` can be positive or negative.

A positive ``revision`` number may be used to select files in order of the
oldest upload date. A revision of ``0`` would denote the file with the oldest
upload date, a revision of ``1`` would denote the second oldest upload, and so
on.

A negative revision may be used to select files in order of the most recent
upload date. A revision of ``-1`` would denote a file with the most recent
upload date, a revision of ``-2`` would denote the second most recent upload,
and so on. The ``revision`` option defaults to ``-1`` if not specified.

The following example downloads the contents of the oldest version of a
particular file:

.. code-block:: php

   <?php

136
   $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
137 138

   $stream = $bucket->openDownloadStreamByName('my-file.txt', ['revision' => 0]);
139
   $contents = stream_get_contents($stream);
140 141 142 143 144 145 146 147 148 149 150 151 152

Deleting Files
--------------

You can delete a GridFS file by its ``_id``.

.. code-block:: php

   <?php

   // In practice, $fileId denotes the _id of an existing file in GridFS
   $fileId = new MongoDB\BSON\ObjectId;

153
   $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
154 155 156 157 158 159 160 161 162 163 164 165 166 167

   $bucket->delete($fileId);

Finding File Metadata
---------------------

The :phpmethod:`find() <MongoDB\\GridFS\\Bucket::find>` method allows you to
retrieve documents from the GridFS files collection, which contain metadata
about the GridFS files.

.. code-block:: php

   <?php

168
   $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

   $cursor = $bucket->find(['filename' => 'my-file.txt']);

Accessing File Metadata for an Existing Stream
----------------------------------------------

The :phpmethod:`getFileDocumentForStream()
<MongoDB\\GridFS\\Bucket::getFileDocumentForStream>` method may be used to get
the file document for an existing readable or writable GridFS stream.

.. code-block:: php

   <?php

   // In practice, $fileId denotes the _id of an existing file in GridFS
   $fileId = new MongoDB\BSON\ObjectId;

186
   $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

   $stream = $bucket->openDownloadStream($fileId);
   $metadata = $bucket->getFileDocumentForStream($stream);

.. note::

   Since the file document for a writable stream is not committed to MongoDB
   until the stream is closed,
   :phpmethod:`getFileDocumentForStream()
   <MongoDB\\GridFS\\Bucket::getFileDocumentForStream>` can only return an
   in-memory document, which will be missing some fields (e.g. ``length``,
   ``md5``).

The :phpmethod:`getFileIdForStream()
<MongoDB\\GridFS\\Bucket::getFileIdForStream>` method may be used to get the
``_id`` for an existing readable or writable GridFS stream. This is a
convenience for accessing the ``_id`` property of the object returned by
:phpmethod:`getFileDocumentForStream()
<MongoDB\\GridFS\\Bucket::getFileDocumentForStream>`.

.. code-block:: php

   <?php

211
   $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
212 213 214

   $stream = $bucket->openDownloadStreamByName('my-file.txt');
   $fileId = $bucket->getFileIdForStream($stream);