@@ -29,8 +29,8 @@ and organization of the file storage.
29
29
30
30
Use GridFS if the size of your files exceeds the BSON document size limit of
31
31
16 MB. GridFS also helps you access files without loading the entire file
32
- into memory. For more detailed information on whether GridFS is suitable for
33
- your use case, see the :manual:`GridFS server manual page </core/gridfs>`.
32
+ into memory. To learn more about whether GridFS is suitable for
33
+ your use case, see :manual:`GridFS </core/gridfs>` in the Server manual .
34
34
35
35
How GridFS Works
36
36
----------------
@@ -39,8 +39,8 @@ GridFS organizes files in a **bucket**, a group of MongoDB collections
39
39
that contain the chunks of files and information describing them. The
40
40
bucket contains the following collections:
41
41
42
- - The ``chunks`` collection , which stores the binary file chunks.
43
- - The ``files`` collection , which stores the file metadata.
42
+ - ``chunks``, which stores the binary file chunks
43
+ - ``files``, which stores the file metadata
44
44
45
45
When you create a new GridFS bucket, the driver creates the preceding
46
46
collections. The default bucket name ``fs`` prefixes the collection names,
@@ -49,8 +49,9 @@ bucket during the first write operation.
49
49
50
50
The driver also creates an index on each collection to ensure efficient
51
51
retrieval of the files and related metadata. The driver creates indexes
52
- if they do not already exist and when the bucket is empty. For more information
53
- on GridFS indexes, see the server manual page on :manual:`GridFS Indexes </core/gridfs/#gridfs-indexes>`.
52
+ if they do not already exist and when the bucket is empty. To learn more about
53
+ GridFS indexes, see :manual:`GridFS Indexes
54
+ </core/gridfs/#gridfs-indexes>` in the Server manual.
54
55
55
56
When storing files with GridFS, the driver splits the files into smaller
56
57
chunks, each represented by a separate document in the ``chunks`` collection.
@@ -62,15 +63,14 @@ how GridFS splits the uploaded files:
62
63
:alt: A diagram that shows how GridFS uploads a file to a bucket
63
64
64
65
When retrieving files, GridFS fetches the metadata from the ``files``
65
- collection in the specified bucket, then uses that information to reconstruct
66
+ collection in the specified bucket then uses that information to reconstruct
66
67
the file from documents in the ``chunks`` collection. You can read the file
67
68
into memory or output it to a stream.
68
69
69
- Use GridFS
70
- ----------
70
+ GridFS Operations
71
+ -----------------
71
72
72
- To learn about GridFS operations and how to perform them, navigate to the
73
- following sections:
73
+ The following sections describe how to perform GridFS operations:
74
74
75
75
- :ref:`<golang-create-bucket>`
76
76
- :ref:`<golang-upload-files>`
@@ -87,73 +87,74 @@ Create a GridFS Bucket
87
87
88
88
To store or retrieve files from GridFS, create a bucket or get a reference to
89
89
an existing bucket on a MongoDB database. To create a ``GridFSBucket`` instance,
90
- call the ``NewBucket()`` method with a database parameter:
90
+ call the ``GridFSBucket()`` method on a ``Database`` instance, as shown
91
+ in the following code:
91
92
92
93
.. code-block:: go
93
94
94
- db := client.Database("db")
95
- bucket, err := gridfs.NewBucket(db)
96
- if err != nil {
97
- panic(err)
98
- }
95
+ db := client.Database("myDB")
96
+ bucket := db.GridFSBucket()
99
97
100
98
.. note::
101
99
102
- If a GridFS bucket already exists, the ``NewBucket ()`` method returns a
100
+ If a GridFS bucket already exists, the ``GridFSBucket ()`` method returns a
103
101
reference to the bucket rather than instantiating a new one.
104
102
105
- By default, the new bucket is named ``fs``. To instantiate a bucket with a
106
- custom name, call the ``SetName()`` method on a ``BucketOptions`` instance as
107
- follows :
103
+ By default, the driver sets the name of the bucket to ``fs``. To
104
+ create a bucket with a custom name, call the ``SetName()`` method
105
+ on a ``BucketOptions`` instance, as shown in the following code :
108
106
109
107
.. code-block:: go
110
108
111
- db := client.Database("db")
112
- opts := options.GridFSBucket().SetName("custom name")
113
- bucket, err := gridfs.NewBucket(db, opts)
114
-
115
- if err != nil {
116
- panic(err)
117
- }
109
+ db := client.Database("myDB")
110
+ bucketOpts := options.GridFSBucket().SetName("myCustomBucket")
111
+
112
+ bucket := db.GridFSBucket(bucketOpts)
118
113
119
114
.. _golang-upload-files:
120
115
121
116
Upload Files
122
117
~~~~~~~~~~~~
123
118
124
- You can upload a file into a GridFS bucket in one of the following ways:
119
+ You can upload a file into a GridFS bucket by using one of the following
120
+ methods:
125
121
126
- - Use the ``UploadFromStream()`` method , which reads from an input stream.
127
- - Use the ``OpenUploadStream()`` method , which writes to an output stream.
122
+ - ``UploadFromStream()``, which reads from an input stream
123
+ - ``OpenUploadStream()``, which writes to an output stream
128
124
129
- For either upload process, you can specify configuration information on an instance
130
- of ``UploadOptions``. For a full list of ``UploadOptions`` fields, visit the
131
- `API documentation <{+api+}/mongo/options#UploadOptions>`__.
125
+ For either upload process, you can specify configuration information by creating
126
+ an ``UploadOptions`` instance. To view a full list of options, see the
127
+ `UploadOptions API documentation <{+api+}/mongo/options#UploadOptions>`__.
132
128
133
129
Upload with an Input Stream
134
130
```````````````````````````
135
131
136
132
To upload a file with an input stream, use the ``UploadFromStream()`` method
137
- with the following parameters:
133
+ and include the following parameters:
138
134
139
- - Your file name
140
- - An ``io.Reader``, with your opened file as a parameter
141
- - An optional ``opts`` parameter to modify the behavior of ``UploadFromStream()``
135
+ - File name
136
+ - ``io.Reader`` instance, including your opened file as a parameter
137
+ - ``opts`` parameter to modify the behavior of ``UploadFromStream()``
142
138
143
- The following code example reads from a file called ``file.txt`` and uploads the
144
- content to a GridFS bucket. It uses an ``opts`` parameter to set file metadata:
139
+ The following code example reads from a file called ``file.txt``,
140
+ creates an ``opts`` parameter to set file metadata, and uploads the
141
+ content to a GridFS bucket:
145
142
146
143
.. io-code-block::
147
144
:copyable: true
148
145
149
146
.. input::
150
147
:language: go
151
148
152
- file, err := os.Open("path/to /file.txt")
149
+ file, err := os.Open("home/documents /file.txt")
153
150
uploadOpts := options.GridFSUpload().SetMetadata(bson.D{{"metadata tag", "first"}})
154
151
155
- objectID, err := bucket.UploadFromStream("file.txt", io.Reader(file),
156
- uploadOpts)
152
+ objectID, err := bucket
153
+ .UploadFromStream(
154
+ "file.txt",
155
+ io.Reader(file),
156
+ uploadOpts
157
+ )
157
158
if err != nil {
158
159
panic(err)
159
160
}
@@ -164,20 +165,19 @@ content to a GridFS bucket. It uses an ``opts`` parameter to set file metadata:
164
165
:language: none
165
166
:visible: false
166
167
167
- New file uploaded with ID 62e00...
168
-
168
+ New file uploaded with ID ...
169
169
170
170
Upload with an Output Stream
171
171
````````````````````````````
172
172
173
173
To upload a file with an output stream, use the ``OpenUploadStream()`` method
174
- with the following parameters:
174
+ and include the following parameters:
175
175
176
- - Your file name
177
- - An optional ``opts`` parameter to modify the behavior of ``OpenUploadStream()``
176
+ - File name
177
+ - ``opts`` parameter to modify the behavior of ``OpenUploadStream()``
178
178
179
179
The following code example opens an upload stream on a GridFS bucket and sets
180
- the number of bytes in each chunk with an ``opts`` parameter. Then, it calls
180
+ the number of bytes in each chunk in the options parameter. Then, it calls
181
181
the ``Write()`` method on the content of ``file.txt`` to write its content to
182
182
the stream:
183
183
@@ -194,26 +194,26 @@ Retrieve File Information
194
194
195
195
You can retrieve file metadata stored in the ``files`` collection of the GridFS
196
196
bucket. Each document in the ``files`` collection contains the following
197
- information:
197
+ pieces of information:
198
198
199
- - The file ID
200
- - The file length
201
- - The maximum chunk size
202
- - The upload date and time
203
- - The file name
204
- - A ``metadata`` document in which you can store any other information
199
+ - File ID
200
+ - File length
201
+ - Maximum chunk size
202
+ - Upload date and time
203
+ - File name
204
+ - ``metadata`` document that stores any other information
205
205
206
206
To retrieve file data, call the ``Find()`` method on a ``GridFSBucket``
207
207
instance. You can pass a query filter as an argument to ``Find()`` to match
208
208
only certain file documents.
209
209
210
210
.. note::
211
211
212
- The ``Find()`` method requires a query filter as a parameter . To match all
212
+ You must pass a query filter to the ``Find()`` method . To retrieve all
213
213
documents in the ``files`` collection, pass an empty query filter to ``Find()``.
214
214
215
- The following example retrieves the file name and length of documents in the
216
- ``files`` collection with ``length`` values greater than ``1500``:
215
+ The following example retrieves the file name and length of documents in
216
+ which the ``length`` value is greater than ``1500``:
217
217
218
218
.. code-block:: go
219
219
@@ -223,11 +223,12 @@ The following example retrieves the file name and length of documents in the
223
223
panic(err)
224
224
}
225
225
226
- type gridfsFile struct {
226
+ type gridFSFile struct {
227
227
Name string `bson:"filename"`
228
228
Length int64 `bson:"length"`
229
229
}
230
- var foundFiles []gridfsFile
230
+
231
+ var foundFiles []gridFSFile
231
232
if err = cursor.All(context.TODO(), &foundFiles); err != nil {
232
233
panic(err)
233
234
}
@@ -241,35 +242,38 @@ The following example retrieves the file name and length of documents in the
241
242
Download Files
242
243
~~~~~~~~~~~~~~
243
244
244
- You can download a GridFS file in one of the following ways :
245
+ You can download a GridFS file by using one of the following methods :
245
246
246
- - Use the ``DownloadToStream()`` method to download a file to an output stream.
247
- - Use the ``OpenDownloadStream()`` method to open an input stream.
247
+ - ``DownloadToStream()``, which downloads a file to an output stream
248
+ - ``OpenDownloadStream()``, which opens an input stream
248
249
249
250
Download a File to an Output Stream
250
251
```````````````````````````````````
251
252
252
- You can download a file in a GridFS bucket directly to an output stream using the
253
- ``DownloadToStream()`` method. ``DownloadToStream()`` takes a file ID and an
254
- ``io.Writer`` as parameters. The method downloads the file with the specified
255
- file ID and writes to the ``io.Writer``.
253
+ You can download a file in a GridFS bucket directly to an output stream
254
+ by using the ``DownloadToStream()`` method. The ``DownloadToStream()``
255
+ method takes a file ID and an ``io.Writer`` instance as parameters. The
256
+ method downloads the file with the specified file ID and writes it to the
257
+ ``io.Writer`` instance.
256
258
257
259
The following example downloads a file and writes to a file buffer:
258
260
259
261
.. code-block:: go
260
262
261
263
id, err := primitive.ObjectIDFromHex("62f7bd54a6e4452da13b3e88")
262
264
fileBuffer := bytes.NewBuffer(nil)
265
+
263
266
if _, err := bucket.DownloadToStream(id, fileBuffer); err != nil {
264
267
panic(err)
265
268
}
266
269
267
270
Download a File to an Input Stream
268
271
``````````````````````````````````
269
272
270
- You can download a file in a GridFS bucket to memory with an input stream using
271
- the ``OpenDownloadStream()`` method. ``OpenDownloadStream()`` takes a file ID as
272
- a parameter and returns an input stream from which you can read the file.
273
+ You can download a file in a GridFS bucket to memory with an input
274
+ stream by using the ``OpenDownloadStream()`` method. The
275
+ ``OpenDownloadStream()`` method takes a file ID as a parameter and
276
+ returns an input stream from which you can read the file.
273
277
274
278
The following example downloads a file into memory and reads its contents:
275
279
@@ -328,32 +332,35 @@ Delete a GridFS Bucket
328
332
329
333
You can delete a GridFS bucket by using the ``Drop()`` method.
330
334
331
- The following code example deletes a GridFS bucket:
335
+ The following code example removes a GridFS bucket:
332
336
333
337
.. code-block:: go
334
338
335
339
if err := bucket.Drop(); err != nil {
336
340
panic(err)
337
341
}
338
342
339
-
340
343
Additional Resources
341
344
--------------------
342
345
343
- To learn more about GridFS and its operations, visit the :manual:`GridFS manual page </core/gridfs>`.
346
+ To learn more about GridFS and storage, see the following pages in the Server
347
+ manual:
348
+
349
+ - :manual:`GridFS </core/gridfs>`
350
+ - :manual:`FAQ: MongoDB Storage </faq/storage/>`
344
351
345
352
API Documentation
346
353
~~~~~~~~~~~~~~~~~
347
354
348
- To learn more about the methods or types discussed in this guide, see the following
349
- API Documentation :
350
-
351
- - `NewBucket () <{+api+}/mongo/gridfs#NewBucket >`__
352
- - `OpenUploadStream() <{+api+}/mongo/gridfs#Bucket .OpenUploadStream>`__
353
- - `UploadFromStream() <{+api+}/mongo/gridfs#Bucket .UploadFromStream>`__
354
- - `Find() <{+api+}/mongo/gridfs#Bucket .Find>`__
355
- - `OpenDownloadStream() <{+api+}/mongo/gridfs#Bucket.OpenUploadStream >`__
356
- - `DownloadToStream() <{+api+}/mongo/gridfs#Bucket .DownloadToStream>`__
357
- - `Rename() <{+api+}/mongo/gridfs#Bucket .Rename>`__
358
- - `Delete() <{+api+}/mongo/gridfs#Bucket .Delete>`__
359
- - `Drop() <{+api+}/mongo/gridfs#Bucket .Drop>`__
355
+ To learn more about the methods and types mentioned in this guide, see
356
+ the following API documentation :
357
+
358
+ - `GridFSBucket () <{+api+}/mongo#Database.GridFSBucket >`__
359
+ - `OpenUploadStream() <{+api+}/mongo#GridFSBucket .OpenUploadStream>`__
360
+ - `UploadFromStream() <{+api+}/mongo#GridFSBucket .UploadFromStream>`__
361
+ - `Find() <{+api+}/mongo#GridFSBucket .Find>`__
362
+ - `OpenDownloadStream() <{+api+}/mongo#GridFSBucket.OpenDownloadStream >`__
363
+ - `DownloadToStream() <{+api+}/mongo#GridFSBucket .DownloadToStream>`__
364
+ - `Rename() <{+api+}/mongo#GridFSBucket .Rename>`__
365
+ - `Delete() <{+api+}/mongo#GridFSBucket .Delete>`__
366
+ - `Drop() <{+api+}/mongo#GridFSBucket .Drop>`__
0 commit comments