Skip to content

Commit 1849072

Browse files
committed
edit wording
1 parent 8f7a2d5 commit 1849072

File tree

2 files changed

+57
-49
lines changed

2 files changed

+57
-49
lines changed

source/includes/gridfs/gridfs.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
1-
# start create bucket
2-
db = MongoClient().test
3-
bucket = gridfs.GridFSBucket(db)
4-
# end create bucket
5-
6-
# start create custom bucket
7-
bucket = gridfs.GridFSBucket(db, bucket_name='myCustomBucket')
8-
# end create custom bucket
9-
10-
# start upload files
11-
with open("my_file", "rb") as f:
12-
file_id = fs.put(f, filename="my_file")
13-
# end upload files
14-
15-
# start retrieve file info
16-
for file in bucket.find({}):
17-
data = file.read()
18-
# end retrieve file info
19-
20-
# start download files name
21-
destination = open('output_file','wb')
22-
bucket.download_to_stream_by_name("my_file", destination)
23-
# end download files name
24-
25-
# start download files id
26-
destination = open('output_file','wb+')
27-
bucket.download_to_stream(file_id, destination)
28-
# end download files id
29-
30-
# start rename files
31-
fs.rename(file_id, "new_file_name")
32-
# end rename files
33-
34-
# start delete files
35-
bucket.delete(file_id)
36-
# end delete files
1+
# start create bucket
2+
client = MongoClient("<connection string>")
3+
db = client["db"]
4+
bucket = gridfs.GridFSBucket(db)
5+
# end create bucket
6+
7+
# start create custom bucket
8+
custom_bucket = gridfs.GridFSBucket(db, bucket_name='myCustomBucket')
9+
# end create custom bucket
10+
11+
# start upload files
12+
source_path = './file_to_upload'
13+
filename = 'my_file'
14+
15+
with open(source_path, 'rb') as source:
16+
bucket.upload_from_stream(filename, source)
17+
# end upload files
18+
19+
# start retrieve file info
20+
for file_doc in bucket.find({}):
21+
print(file_doc)
22+
# end retrieve file info
23+
24+
# start download files name
25+
destination = open('output_file','wb')
26+
bucket.download_to_stream_by_name('my_file', destination)
27+
# end download files name
28+
29+
# start download files id
30+
destination = open('output_file','wb+')
31+
bucket.download_to_stream(ObjectId('66b3c86e672a17b6c8a4a4a9'), destination)
32+
# end download files id
33+
34+
# start rename files
35+
bucket.rename(ObjectId('66b3c86e672a17b6c8a4a4a9'), "new_file_name")
36+
# end rename files
37+
38+
# start delete files
39+
bucket.delete(ObjectId('66b3c86e672a17b6c8a4a4a9'))
40+
# end delete files

source/write/gridfs.txt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,17 @@ constructor, as shown below:
104104
Upload Files
105105
------------
106106

107-
Use the ``open_upload_stream()`` method from the ``GridFSBucket`` class to create an upload
108-
stream for a given file name. The
109-
``open_upload_stream()`` method allows you to specify configuration information
110-
such as file chunk size and other field/value pairs to store as metadata. Set
111-
these options as parameters of ``open_upload_stream()``, as shown in the
112-
following code example:
107+
Use the ``upload_from_stream()`` method from the ``GridFSBucket`` class to upload
108+
a file to a GridFS bucket. Pass the following parameters to the ``upload_from_stream()``
109+
method:
110+
111+
- ``filename``: The name of the file to store the uploaded content
112+
- ``source``: The file you want to upload
113+
- ``chunk_size_bytes``: The number of bytes per chunk of this file (optional).
114+
- ``metadata``: The file metadata (optional).
115+
116+
The following code example uploads a file located at ``./file_to_upload`` and stores
117+
its contents in ``my_file``:
113118

114119
.. literalinclude:: /includes/gridfs/gridfs.py
115120
:language: python
@@ -138,7 +143,7 @@ from which you can access the results. To learn more about ``Cursor`` objects in
138143
{+driver-short+}, see :ref:`<pymongo-cursors>`.
139144

140145
The following code example shows you how to retrieve and print file metadata
141-
from all your files in a GridFS bucket. It uses the ``for...of`` syntax to traverse the
146+
from all your files in a GridFS bucket. It uses the ``for...in`` syntax to traverse the
142147
``Cursor`` iterable and display the results:
143148

144149
.. literalinclude:: /includes/gridfs/gridfs.py
@@ -158,12 +163,11 @@ Download Files
158163
--------------
159164

160165
You can download files from your MongoDB database by using the
161-
``open_download_stream_by_name()`` method from ``GridFSBucket`` to create a
162-
download stream.
166+
``download_to_stream_by_name()`` method from the ``GridFSBucket``
167+
class.
163168

164169
The following example shows you how to download a file referenced
165-
by the file name, stored in the ``filename`` field, into your working
166-
directory:
170+
by the file name, ``my_file``, into a file named ``output_file``:
167171

168172
.. literalinclude:: /includes/gridfs/gridfs.py
169173
:language: python
@@ -177,7 +181,7 @@ directory:
177181
GridFS will stream the most recent file with the given name (as
178182
determined by the ``uploadDate`` field).
179183

180-
Alternatively, you can use the ``open_download_stream()``
184+
Alternatively, you can use the ``download_to_stream()``
181185
method, which takes the ``_id`` field of a file as a parameter:
182186

183187
.. literalinclude:: /includes/gridfs/gridfs.py
@@ -204,7 +208,7 @@ bucket. You must specify the file to rename by its ``_id`` field
204208
rather than its file name.
205209

206210
The following example shows how to update the ``filename`` field to
207-
``"newFileName"`` by referencing a document's ``_id`` field:
211+
``"new_file_name"`` by referencing a document's ``_id`` field:
208212

209213
.. literalinclude:: /includes/gridfs/gridfs.py
210214
:language: python

0 commit comments

Comments
 (0)