|
| 1 | +====== |
| 2 | +GridFS |
| 3 | +====== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +:manual:`GridFS </core/gridfs/>` is a specification for storing and retrieving |
| 14 | +files in MongoDB. GridFS uses two collections to store files. One collection |
| 15 | +stores the file chunks (e.g. ``fs.chunks``), and the other stores file metadata |
| 16 | +(e.g. ``fs.files``). The :phpclass:`MongoDB\\GridFS\\Bucket` class provides an |
| 17 | +interface around these collections for working with the files as PHP |
| 18 | +:php:`Streams <stream>`. |
| 19 | + |
| 20 | +Creating a GridFS Bucket |
| 21 | +------------------------ |
| 22 | + |
| 23 | +You can construct a GridFS bucket using the PHP extension's |
| 24 | +:php:`MongoDB\\Driver\\Manager <class.mongodb-driver-manager>` class, or select |
| 25 | +a bucket from the |php-library|'s :phpclass:`MongoDB\\Database` class via the |
| 26 | +:phpmethod:`selectGridFSBucket() <MongoDB\\Database::selectGridFSBucket>` |
| 27 | +method. |
| 28 | + |
| 29 | +The bucket can be constructed with various options: |
| 30 | + |
| 31 | + - ``bucketName`` determines the prefix for the buckey's metadata and chunk |
| 32 | + collections. The default value is ``"fs"``. |
| 33 | + - ``chunkSizeBytes`` determines the size of each chunk. GridFS divides the |
| 34 | + file into chunks of this length, except for the last, which is only as large |
| 35 | + as needed. The default size is ``261120`` (i.e. 255 KiB). |
| 36 | + - ``readConcern``, ``readPreference`` and ``writeConcern`` options can be used |
| 37 | + to specify defaults for read and write operations, much like the |
| 38 | + :phpclass:`MongoDB\\GridFS\\Collection` options. |
| 39 | + |
| 40 | +Uploading Files with Writable Streams |
| 41 | +------------------------------------- |
| 42 | + |
| 43 | +To upload a file to GridFS using a writable stream, you can either open a stream |
| 44 | +and write to it directly or write the entire contents of another readable stream |
| 45 | +to GridFS all at once. |
| 46 | + |
| 47 | +To open an upload stream and write to it: |
| 48 | + |
| 49 | +.. code-block:: php |
| 50 | + |
| 51 | + <?php |
| 52 | + |
| 53 | + $bucket = (new MongoDB\Client)->example->selectGridFSBucket(); |
| 54 | + |
| 55 | + $stream = $bucket->openUploadStream('my-file.txt'); |
| 56 | + |
| 57 | + $contents = file_get_contents('/path/to/my-file.txt'); |
| 58 | + fwrite($stream, $contents); |
| 59 | + fclose($stream); |
| 60 | + |
| 61 | +To upload the entire contents of a readable stream in one call: |
| 62 | + |
| 63 | +.. code-block:: php |
| 64 | + |
| 65 | + <?php |
| 66 | + |
| 67 | + $bucket = (new MongoDB\Client)->example->selectGridFSBucket(); |
| 68 | + |
| 69 | + $file = fopen('/path/to/my-file.txt', 'rb'); |
| 70 | + $bucket->uploadFromStream('my-file.txt', $file); |
| 71 | + |
| 72 | +Downloading Files with Readable Streams |
| 73 | +--------------------------------------- |
| 74 | + |
| 75 | +To download a file from GridFS using a readable stream, you can either open a |
| 76 | +stream and read from it directly or download the entire file all at once. |
| 77 | + |
| 78 | +To open a download stream and read from it: |
| 79 | + |
| 80 | +.. code-block:: php |
| 81 | + |
| 82 | + <?php |
| 83 | + |
| 84 | + // In practice, $fileId denotes the _id of an existing file in GridFS |
| 85 | + $fileId = new MongoDB\BSON\ObjectId; |
| 86 | + |
| 87 | + $bucket = (new MongoDB\Client)->example->selectGridFSBucket(); |
| 88 | + |
| 89 | + $stream = $bucket->openDownloadStream($fileId); |
| 90 | + $contents = file_get_contents($stream); |
| 91 | + |
| 92 | +To download the file all at once and write it to an writable stream: |
| 93 | + |
| 94 | +.. code-block:: php |
| 95 | + |
| 96 | + <?php |
| 97 | + |
| 98 | + // In practice, $fileId denotes the _id of an existing file in GridFS |
| 99 | + $fileId = new MongoDB\BSON\ObjectId; |
| 100 | + |
| 101 | + $bucket = (new MongoDB\Client)->example->selectGridFSBucket(); |
| 102 | + |
| 103 | + $file = fopen('/path/to/my-output-file.txt', 'wb'); |
| 104 | + |
| 105 | + $bucket->downloadToStream($fileId, $file); |
| 106 | + |
| 107 | +Selecting Files by Filename and Revision |
| 108 | +---------------------------------------- |
| 109 | + |
| 110 | +You can also download a file specified by filename and (optionally) revision |
| 111 | +number. Revision numbers are used to distinguish between files sharing the same |
| 112 | +``filename`` metadata field, ordered by date of upload (i.e. the ``uploadDate`` |
| 113 | +metadata field). The ``revision`` option accepted by |
| 114 | +:phpmethod:`openDownloadStreamByName() |
| 115 | +<MongoDB\\GridFS\\Bucket::openDownloadStreamByName>` and |
| 116 | +:phpmethod:`downloadToStreamByName() |
| 117 | +<MongoDB\\GridFS\\Bucket::downloadToStreamByName>` can be positive or negative. |
| 118 | + |
| 119 | +A positive ``revision`` number may be used to select files in order of the |
| 120 | +oldest upload date. A revision of ``0`` would denote the file with the oldest |
| 121 | +upload date, a revision of ``1`` would denote the second oldest upload, and so |
| 122 | +on. |
| 123 | + |
| 124 | +A negative revision may be used to select files in order of the most recent |
| 125 | +upload date. A revision of ``-1`` would denote a file with the most recent |
| 126 | +upload date, a revision of ``-2`` would denote the second most recent upload, |
| 127 | +and so on. The ``revision`` option defaults to ``-1`` if not specified. |
| 128 | + |
| 129 | +The following example downloads the contents of the oldest version of a |
| 130 | +particular file: |
| 131 | + |
| 132 | +.. code-block:: php |
| 133 | + |
| 134 | + <?php |
| 135 | + |
| 136 | + $bucket = (new MongoDB\Client)->example->selectGridFSBucket(); |
| 137 | + |
| 138 | + $stream = $bucket->openDownloadStreamByName('my-file.txt', ['revision' => 0]); |
| 139 | + $contents = file_get_contents($stream); |
| 140 | + |
| 141 | +Deleting Files |
| 142 | +-------------- |
| 143 | + |
| 144 | +You can delete a GridFS file by its ``_id``. |
| 145 | + |
| 146 | +.. code-block:: php |
| 147 | + |
| 148 | + <?php |
| 149 | + |
| 150 | + // In practice, $fileId denotes the _id of an existing file in GridFS |
| 151 | + $fileId = new MongoDB\BSON\ObjectId; |
| 152 | + |
| 153 | + $bucket = (new MongoDB\Client)->example->selectGridFSBucket(); |
| 154 | + |
| 155 | + $bucket->delete($fileId); |
| 156 | + |
| 157 | +Finding File Metadata |
| 158 | +--------------------- |
| 159 | + |
| 160 | +The :phpmethod:`find() <MongoDB\\GridFS\\Bucket::find>` method allows you to |
| 161 | +retrieve documents from the GridFS files collection, which contain metadata |
| 162 | +about the GridFS files. |
| 163 | + |
| 164 | +.. code-block:: php |
| 165 | + |
| 166 | + <?php |
| 167 | + |
| 168 | + $bucket = (new MongoDB\Client)->example->selectGridFSBucket(); |
| 169 | + |
| 170 | + $cursor = $bucket->find(['filename' => 'my-file.txt']); |
| 171 | + |
| 172 | +Accessing File Metadata for an Existing Stream |
| 173 | +---------------------------------------------- |
| 174 | + |
| 175 | +The :phpmethod:`getFileDocumentForStream() |
| 176 | +<MongoDB\\GridFS\\Bucket::getFileDocumentForStream>` method may be used to get |
| 177 | +the file document for an existing readable or writable GridFS stream. |
| 178 | + |
| 179 | +.. code-block:: php |
| 180 | + |
| 181 | + <?php |
| 182 | + |
| 183 | + // In practice, $fileId denotes the _id of an existing file in GridFS |
| 184 | + $fileId = new MongoDB\BSON\ObjectId; |
| 185 | + |
| 186 | + $bucket = (new MongoDB\Client)->example->selectGridFSBucket(); |
| 187 | + |
| 188 | + $stream = $bucket->openDownloadStream($fileId); |
| 189 | + $metadata = $bucket->getFileDocumentForStream($stream); |
| 190 | + |
| 191 | +.. note:: |
| 192 | + |
| 193 | + Since the file document for a writable stream is not committed to MongoDB |
| 194 | + until the stream is closed, |
| 195 | + :phpmethod:`getFileDocumentForStream() |
| 196 | + <MongoDB\\GridFS\\Bucket::getFileDocumentForStream>` can only return an |
| 197 | + in-memory document, which will be missing some fields (e.g. ``length``, |
| 198 | + ``md5``). |
| 199 | + |
| 200 | +The :phpmethod:`getFileIdForStream() |
| 201 | +<MongoDB\\GridFS\\Bucket::getFileIdForStream>` method may be used to get the |
| 202 | +``_id`` for an existing readable or writable GridFS stream. This is a |
| 203 | +convenience for accessing the ``_id`` property of the object returned by |
| 204 | +:phpmethod:`getFileDocumentForStream() |
| 205 | +<MongoDB\\GridFS\\Bucket::getFileDocumentForStream>`. |
| 206 | + |
| 207 | +.. code-block:: php |
| 208 | + |
| 209 | + <?php |
| 210 | + |
| 211 | + $bucket = (new MongoDB\Client)->example->selectGridFSBucket(); |
| 212 | + |
| 213 | + $stream = $bucket->openDownloadStreamByName('my-file.txt'); |
| 214 | + $fileId = $bucket->getFileIdForStream($stream); |
0 commit comments