@@ -29,8 +29,8 @@ and organization of the file storage.
2929
3030Use GridFS if the size of your files exceeds the BSON document size limit of
313116 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 .
3434
3535How GridFS Works
3636----------------
@@ -39,8 +39,8 @@ GridFS organizes files in a **bucket**, a group of MongoDB collections
3939that contain the chunks of files and information describing them. The
4040bucket contains the following collections:
4141
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
4444
4545When you create a new GridFS bucket, the driver creates the preceding
4646collections. The default bucket name ``fs`` prefixes the collection names,
@@ -49,8 +49,9 @@ bucket during the first write operation.
4949
5050The driver also creates an index on each collection to ensure efficient
5151retrieval 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.
5455
5556When storing files with GridFS, the driver splits the files into smaller
5657chunks, each represented by a separate document in the ``chunks`` collection.
@@ -62,15 +63,14 @@ how GridFS splits the uploaded files:
6263 :alt: A diagram that shows how GridFS uploads a file to a bucket
6364
6465When 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
6667the file from documents in the ``chunks`` collection. You can read the file
6768into memory or output it to a stream.
6869
69- Use GridFS
70- ----------
70+ GridFS Operations
71+ -----------------
7172
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:
7474
7575- :ref:`<golang-create-bucket>`
7676- :ref:`<golang-upload-files>`
@@ -87,73 +87,74 @@ Create a GridFS Bucket
8787
8888To store or retrieve files from GridFS, create a bucket or get a reference to
8989an 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:
9192
9293.. code-block:: go
9394
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()
9997
10098.. note::
10199
102- If a GridFS bucket already exists, the ``NewBucket ()`` method returns a
100+ If a GridFS bucket already exists, the ``GridFSBucket ()`` method returns a
103101 reference to the bucket rather than instantiating a new one.
104102
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 :
108106
109107.. code-block:: go
110108
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)
118113
119114.. _golang-upload-files:
120115
121116Upload Files
122117~~~~~~~~~~~~
123118
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:
125121
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
128124
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>`__.
132128
133129Upload with an Input Stream
134130```````````````````````````
135131
136132To upload a file with an input stream, use the ``UploadFromStream()`` method
137- with the following parameters:
133+ and include the following parameters:
138134
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()``
142138
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:
145142
146143.. io-code-block::
147144 :copyable: true
148145
149146 .. input::
150147 :language: go
151148
152- file, err := os.Open("path/to /file.txt")
149+ file, err := os.Open("home/documents /file.txt")
153150 uploadOpts := options.GridFSUpload().SetMetadata(bson.D{{"metadata tag", "first"}})
154151
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+ )
157158 if err != nil {
158159 panic(err)
159160 }
@@ -164,20 +165,19 @@ content to a GridFS bucket. It uses an ``opts`` parameter to set file metadata:
164165 :language: none
165166 :visible: false
166167
167- New file uploaded with ID 62e00...
168-
168+ New file uploaded with ID ...
169169
170170Upload with an Output Stream
171171````````````````````````````
172172
173173To upload a file with an output stream, use the ``OpenUploadStream()`` method
174- with the following parameters:
174+ and include the following parameters:
175175
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()``
178178
179179The 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
181181the ``Write()`` method on the content of ``file.txt`` to write its content to
182182the stream:
183183
@@ -194,26 +194,26 @@ Retrieve File Information
194194
195195You can retrieve file metadata stored in the ``files`` collection of the GridFS
196196bucket. Each document in the ``files`` collection contains the following
197- information:
197+ pieces of information:
198198
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
205205
206206To retrieve file data, call the ``Find()`` method on a ``GridFSBucket``
207207instance. You can pass a query filter as an argument to ``Find()`` to match
208208only certain file documents.
209209
210210.. note::
211211
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
213213 documents in the ``files`` collection, pass an empty query filter to ``Find()``.
214214
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``:
217217
218218.. code-block:: go
219219
@@ -223,11 +223,12 @@ The following example retrieves the file name and length of documents in the
223223 panic(err)
224224 }
225225
226- type gridfsFile struct {
226+ type gridFSFile struct {
227227 Name string `bson:"filename"`
228228 Length int64 `bson:"length"`
229229 }
230- var foundFiles []gridfsFile
230+
231+ var foundFiles []gridFSFile
231232 if err = cursor.All(context.TODO(), &foundFiles); err != nil {
232233 panic(err)
233234 }
@@ -241,35 +242,38 @@ The following example retrieves the file name and length of documents in the
241242Download Files
242243~~~~~~~~~~~~~~
243244
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 :
245246
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
248249
249250Download a File to an Output Stream
250251```````````````````````````````````
251252
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.
256258
257259The following example downloads a file and writes to a file buffer:
258260
259261.. code-block:: go
260262
261263 id, err := primitive.ObjectIDFromHex("62f7bd54a6e4452da13b3e88")
262264 fileBuffer := bytes.NewBuffer(nil)
265+
263266 if _, err := bucket.DownloadToStream(id, fileBuffer); err != nil {
264267 panic(err)
265268 }
266269
267270Download a File to an Input Stream
268271``````````````````````````````````
269272
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.
273277
274278The following example downloads a file into memory and reads its contents:
275279
@@ -328,32 +332,35 @@ Delete a GridFS Bucket
328332
329333You can delete a GridFS bucket by using the ``Drop()`` method.
330334
331- The following code example deletes a GridFS bucket:
335+ The following code example removes a GridFS bucket:
332336
333337.. code-block:: go
334338
335339 if err := bucket.Drop(); err != nil {
336340 panic(err)
337341 }
338342
339-
340343Additional Resources
341344--------------------
342345
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/>`
344351
345352API Documentation
346353~~~~~~~~~~~~~~~~~
347354
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