Skip to content

Commit a3cf3cf

Browse files
committed
PYTHON-1031 - GridFsBucket.download_to_stream now uses GridOutIterator
1 parent aa12164 commit a3cf3cf

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

gridfs/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def download_to_stream(self, file_id, destination):
525525
# Get _id of file to read
526526
file_id = fs.upload_from_stream("test_file", "data I want to store!")
527527
# Get file to write to
528-
file = open('myfile','rw')
528+
file = open('myfile','rwb')
529529
fs.download_to_stream(file_id, file)
530530
contents = file.read()
531531
@@ -536,7 +536,8 @@ def download_to_stream(self, file_id, destination):
536536
-`destination`: a file-like object implementing :meth:`write`.
537537
"""
538538
gout = self.open_download_stream(file_id)
539-
destination.write(gout)
539+
for chunk in gout:
540+
destination.write(chunk)
540541

541542
def delete(self, file_id):
542543
"""Given an file_id, delete this stored file's files collection document
@@ -663,7 +664,7 @@ def download_to_stream_by_name(self, filename, destination, revision=-1):
663664
my_db = MongoClient().test
664665
fs = GridFSBucket(my_db)
665666
# Get file to write to
666-
file = open('myfile','w')
667+
file = open('myfile','wb')
667668
fs.download_to_stream_by_name("test_file", file)
668669
669670
Raises :exc:`~gridfs.errors.NoFile` if no such version of
@@ -687,8 +688,8 @@ def download_to_stream_by_name(self, filename, destination, revision=-1):
687688
-1 = the most recent revision
688689
"""
689690
gout = self.open_download_stream_by_name(filename, revision)
690-
691-
destination.write(gout)
691+
for chunk in gout:
692+
destination.write(chunk)
692693

693694
def rename(self, file_id, new_filename):
694695
"""Renames the stored file with the specified file_id.

test/test_gridfs_bucket.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,56 @@ def test_abort(self):
369369
self.assertEqual(0, self.db.fs.chunks.count(
370370
{"files_id": gin._id}))
371371

372+
def test_download_to_stream(self):
373+
file1 = StringIO(b"hello world")
374+
# Test with one chunk.
375+
oid = self.fs.upload_from_stream("one_chunk", file1)
376+
self.assertEqual(1, self.db.fs.chunks.count())
377+
file2 = StringIO()
378+
self.fs.download_to_stream(oid, file2)
379+
file1.seek(0)
380+
file2.seek(0)
381+
self.assertEqual(file1.read(), file2.read())
382+
383+
# Test with many chunks.
384+
self.db.drop_collection("fs.files")
385+
self.db.drop_collection("fs.chunks")
386+
file1.seek(0)
387+
oid = self.fs.upload_from_stream("many_chunks",
388+
file1,
389+
chunk_size_bytes=1)
390+
self.assertEqual(11, self.db.fs.chunks.count())
391+
file2 = StringIO()
392+
self.fs.download_to_stream(oid, file2)
393+
file1.seek(0)
394+
file2.seek(0)
395+
self.assertEqual(file1.read(), file2.read())
396+
397+
def test_download_to_stream_by_name(self):
398+
file1 = StringIO(b"hello world")
399+
# Test with one chunk.
400+
oid = self.fs.upload_from_stream("one_chunk", file1)
401+
self.assertEqual(1, self.db.fs.chunks.count())
402+
file2 = StringIO()
403+
self.fs.download_to_stream_by_name("one_chunk", file2)
404+
file1.seek(0)
405+
file2.seek(0)
406+
self.assertEqual(file1.read(), file2.read())
407+
408+
# Test with many chunks.
409+
self.db.drop_collection("fs.files")
410+
self.db.drop_collection("fs.chunks")
411+
file1.seek(0)
412+
self.fs.upload_from_stream("many_chunks", file1, chunk_size_bytes=1)
413+
self.assertEqual(11, self.db.fs.chunks.count())
414+
415+
file2 = StringIO()
416+
self.fs.download_to_stream_by_name("many_chunks", file2)
417+
file1.seek(0)
418+
file2.seek(0)
419+
self.assertEqual(file1.read(), file2.read())
420+
421+
372422
class TestGridfsBucketReplicaSet(TestReplicaSetClientBase):
373423

374424
def test_gridfs_replica_set(self):

0 commit comments

Comments
 (0)