Skip to content

Commit eef12cf

Browse files
committed
fixes
1 parent 1849072 commit eef12cf

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

source/includes/gridfs/gridfs.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
# end create bucket
66

77
# start create custom bucket
8-
custom_bucket = gridfs.GridFSBucket(db, bucket_name='myCustomBucket')
8+
custom_bucket = gridfs.GridFSBucket(db, bucket_name="myCustomBucket")
99
# end create custom bucket
1010

1111
# 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)
12+
with bucket.open_upload_stream(
13+
"my_file", chunk_size_bytes=4,
14+
metadata={"contentType": "text/plain"}) as grid_in:
15+
grid_in.write("data to store")
1716
# end upload files
1817

1918
# start retrieve file info
@@ -22,13 +21,13 @@
2221
# end retrieve file info
2322

2423
# start download files name
25-
destination = open('output_file','wb')
26-
bucket.download_to_stream_by_name('my_file', destination)
24+
file = bucket.open_download_stream_by_name("my_file")
25+
contents = file.read()
2726
# end download files name
2827

2928
# start download files id
30-
destination = open('output_file','wb+')
31-
bucket.download_to_stream(ObjectId('66b3c86e672a17b6c8a4a4a9'), destination)
29+
file = bucket.open_download_stream(ObjectId('66b3c86e672a17b6c8a4a4a9'))
30+
contents = file.read()
3231
# end download files id
3332

3433
# start rename files

source/write/gridfs.txt

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

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()``
107+
Use the ``open_upload_stream()`` method from the ``GridFSBucket`` class to upload
108+
a file to a GridFS bucket. Pass the following parameters to the ``open_upload_stream()``
109109
method:
110110

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).
111+
- ``filename``: The name of the file you want to upload
112+
- ``chunk_size_bytes``: The number of bytes per chunk of this file (optional)
113+
- ``metadata``: The file metadata (optional)
115114

116-
The following code example uploads a file located at ``./file_to_upload`` and stores
117-
its contents in ``my_file``:
115+
The following code example uploads a file named ``"my_file"`` to a GridFS bucket:
118116

119117
.. literalinclude:: /includes/gridfs/gridfs.py
120118
:language: python
@@ -163,11 +161,11 @@ Download Files
163161
--------------
164162

165163
You can download files from your MongoDB database by using the
166-
``download_to_stream_by_name()`` method from the ``GridFSBucket``
164+
``open_download_stream_by_name()`` method from the ``GridFSBucket``
167165
class.
168166

169167
The following example shows you how to download a file referenced
170-
by the file name, ``my_file``, into a file named ``output_file``:
168+
by the file name, ``"my_file"``, and read its contents:
171169

172170
.. literalinclude:: /includes/gridfs/gridfs.py
173171
:language: python
@@ -181,7 +179,7 @@ by the file name, ``my_file``, into a file named ``output_file``:
181179
GridFS will stream the most recent file with the given name (as
182180
determined by the ``uploadDate`` field).
183181

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

187185
.. literalinclude:: /includes/gridfs/gridfs.py

0 commit comments

Comments
 (0)