|
| 1 | +.. _scala-gridfs: |
| 2 | + |
| 3 | +====== |
| 4 | +GridFS |
| 5 | +====== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, large file, storage |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +GridFS is a specification for storing and retrieving files that |
| 21 | +exceed the BSON document size limit of 16 MB. Instead of storing a large |
| 22 | +file in a single document, GridFS divides a file into parts, or chunks, and |
| 23 | +stores each of those chunks as separate documents. |
| 24 | + |
| 25 | +When you query a GridFS store for a file, the driver reassembles the |
| 26 | +chunks as needed. |
| 27 | + |
| 28 | +The code examples in this guide come from the `GridFSTour.scala |
| 29 | +<{+driver-source-gh+}/blob/master/driver-scala/src/integration/scala/tour/GridFSTour.scala>`__ |
| 30 | +file in the driver source code GitHub repository. |
| 31 | + |
| 32 | +Prerequisites |
| 33 | +------------- |
| 34 | + |
| 35 | +You must include the following import statements in your program to run the |
| 36 | +code examples in this guide: |
| 37 | + |
| 38 | +.. code-block:: scala |
| 39 | + |
| 40 | + import java.nio.ByteBuffer |
| 41 | + import java.nio.charset.StandardCharsets |
| 42 | + |
| 43 | + import org.mongodb.scala._ |
| 44 | + import org.mongodb.scala.bson.BsonObjectId |
| 45 | + import org.mongodb.scala.gridfs._ |
| 46 | + import org.mongodb.scala.model.Filters |
| 47 | + |
| 48 | + import tour.Helpers._ |
| 49 | + |
| 50 | + import scala.util.Success |
| 51 | + |
| 52 | +.. include:: /includes/obs-note.rst |
| 53 | + |
| 54 | +Connect to a MongoDB Deployment |
| 55 | +------------------------------- |
| 56 | + |
| 57 | +First, connect to a MongoDB deployment and declare and define |
| 58 | +a ``MongoDatabase`` instance. |
| 59 | + |
| 60 | +The following code connects to a standalone |
| 61 | +MongoDB deployment running on ``localhost`` on port ``27017``: |
| 62 | + |
| 63 | +.. code-block:: scala |
| 64 | + |
| 65 | + val mongoClient: MongoClient = MongoClient() |
| 66 | + |
| 67 | +To learn more about connecting to MongoDB deployments, |
| 68 | +see the :ref:`scala-connect` guide. |
| 69 | + |
| 70 | +Create a GridFS Bucket |
| 71 | +---------------------- |
| 72 | + |
| 73 | +GridFS stores files in two collections: |
| 74 | + |
| 75 | +- ``chunks``: stores the file chunks |
| 76 | +- ``files``: stores file metadata |
| 77 | + |
| 78 | +The two collections are in a common bucket and the collection names |
| 79 | +are prefixed with the bucket name. |
| 80 | + |
| 81 | +The driver provides the ``GridFSBucket()`` method to |
| 82 | +create ``GridFSBucket`` instances: |
| 83 | + |
| 84 | +.. code-block:: scala |
| 85 | + |
| 86 | + val myDatabase = mongoClient.getDatabase("mydb") |
| 87 | + |
| 88 | + // Create a gridFSBucket using the default bucket name "fs" |
| 89 | + val gridFSBucket = GridFSBucket(myDatabase) |
| 90 | + |
| 91 | +You can pass a bucket name to the ``GridFSBucket()`` method: |
| 92 | + |
| 93 | +.. code-block:: scala |
| 94 | + |
| 95 | + // Create a gridFSBucket with a custom bucket name "files" |
| 96 | + val gridFSFilesBucket = GridFSBucket(myDatabase, "files") |
| 97 | + |
| 98 | +.. note:: |
| 99 | + |
| 100 | + GridFS automatically creates indexes on the ``files`` and ``chunks`` |
| 101 | + collections when you upload data to the GridFS bucket. |
| 102 | + |
| 103 | +Upload to GridFS |
| 104 | +---------------- |
| 105 | + |
| 106 | +The ``GridFSBucket.uploadFromObservable()`` method reads the contents |
| 107 | +of an ``Observable[ByteBuffer]`` and saves it to the ``GridFSBucket`` instance. |
| 108 | + |
| 109 | +You can use the ``GridFSUploadOptions`` type to configure the chunk size |
| 110 | +or include additional metadata. |
| 111 | + |
| 112 | +The following example uploads the contents of a |
| 113 | +``Observable[ByteBuffer]`` into ``GridFSBucket``: |
| 114 | + |
| 115 | +.. code-block:: scala |
| 116 | + |
| 117 | + // Get the input stream |
| 118 | + val observableToUploadFrom: Observable[ByteBuffer] = Observable( |
| 119 | + Seq(ByteBuffer.wrap("MongoDB Tutorial".getBytes(StandardCharsets.UTF_8))) |
| 120 | + ) |
| 121 | + |
| 122 | + // Create some custom options |
| 123 | + val options: GridFSUploadOptions = new GridFSUploadOptions() |
| 124 | + .chunkSizeBytes(358400) |
| 125 | + .metadata(Document("type" -> "presentation")) |
| 126 | + |
| 127 | + val fileId: BsonObjectId = gridFSBucket |
| 128 | + .uploadFromObservable("mongodb-tutorial", observableToUploadFrom, options) |
| 129 | + .headResult() |
| 130 | + |
| 131 | +Find Files Stored in GridFS |
| 132 | +--------------------------- |
| 133 | + |
| 134 | +To find the files stored in the ``GridFSBucket``, use the ``find()`` |
| 135 | +method. |
| 136 | + |
| 137 | +The following example prints out the filename of each file stored: |
| 138 | + |
| 139 | +.. code-block:: scala |
| 140 | + |
| 141 | + gridFSBucket.find().results().foreach(file => println(s" - ${file.getFilename}")) |
| 142 | + |
| 143 | +You can also provide a custom filter to limit the results returned. |
| 144 | +The following example prints out the filenames of all files in which the |
| 145 | +``contentType`` value is an ``image/png`` value in the user-defined metadata |
| 146 | +document: |
| 147 | + |
| 148 | +.. code-block:: scala |
| 149 | + |
| 150 | + gridFSBucket |
| 151 | + .find(Filters.equal("metadata.contentType", "image/png")) |
| 152 | + .results() |
| 153 | + .foreach(file => println(s" > ${file.getFilename}")) |
| 154 | + |
| 155 | +Download from GridFS |
| 156 | +-------------------- |
| 157 | + |
| 158 | +The ``downloadToObservable()`` method returns an ``Observable[ByteBuffer]`` |
| 159 | +that reads the contents from MongoDB. |
| 160 | + |
| 161 | +To download a file by its file ``_id``, pass the ``_id`` to the method. |
| 162 | +The following example downloads a file by its file ``_id``: |
| 163 | + |
| 164 | +.. code-block:: scala |
| 165 | + |
| 166 | + val downloadById = gridFSBucket.downloadToObservable(fileId).results() |
| 167 | + |
| 168 | +If you don't know the ``_id`` of the file but know the filename, then you |
| 169 | +can pass the filename to the ``downloadToObservable()`` method. By |
| 170 | +default, it will download the latest version of the file. Use the |
| 171 | +``GridFSDownloadOptions`` class to configure which version to download. |
| 172 | + |
| 173 | +The following example downloads the original version of the file named |
| 174 | +``mongodb-tutorial``: |
| 175 | + |
| 176 | +.. code-block:: scala |
| 177 | + |
| 178 | + val downloadOptions: GridFSDownloadOptions = new GridFSDownloadOptions().revision(0) |
| 179 | + val downloadByName = gridFSBucket.downloadToObservable("mongodb-tutorial", downloadOptions).results() |
| 180 | + |
| 181 | +Rename Files |
| 182 | +------------ |
| 183 | + |
| 184 | +If you need to rename a file, then use the ``rename()`` method. |
| 185 | + |
| 186 | +The following example renames a file to ``mongodbTutorial``: |
| 187 | + |
| 188 | +.. code-block:: scala |
| 189 | + |
| 190 | + val fileId: ObjectId = ... // ObjectId of a file uploaded to GridFS |
| 191 | + gridFSBucket.rename(fileId, "mongodbTutorial").printResults() |
| 192 | + |
| 193 | +.. note:: |
| 194 | + |
| 195 | + The ``rename()`` method requires an ``ObjectId`` rather than a ``filename`` to |
| 196 | + ensure the correct file is renamed. |
| 197 | + |
| 198 | + To rename multiple revisions of the same filename, first retrieve the |
| 199 | + full list of files. Then, for every file that should be renamed, |
| 200 | + run ``rename()`` with the corresponding ``_id``. |
| 201 | + |
| 202 | +Delete Files |
| 203 | +------------ |
| 204 | + |
| 205 | +To delete a file from the ``GridFSBucket``, use the ``delete()`` method. |
| 206 | + |
| 207 | +The following example deletes a file from the ``GridFSBucket``: |
| 208 | + |
| 209 | +.. code-block:: scala |
| 210 | + |
| 211 | + val fileId: ObjectId = ... //ObjectId of a file uploaded to GridFS |
| 212 | + gridFSBucket.delete(fileId).printResults() |
0 commit comments