From 8f7a2d5d28f7525f9c5466ad4c1d2831d97b9e9f Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 7 Aug 2024 14:56:10 -0400 Subject: [PATCH 1/6] DOCSP-42254: Fix GridFS code --- source/includes/gridfs/gridfs.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/source/includes/gridfs/gridfs.py b/source/includes/gridfs/gridfs.py index f9832427..adc67a89 100644 --- a/source/includes/gridfs/gridfs.py +++ b/source/includes/gridfs/gridfs.py @@ -1,41 +1,36 @@ # start create bucket -const db = client.db(dbName); -const bucket = new mongodb.GridFSBucket(db); +db = MongoClient().test +bucket = gridfs.GridFSBucket(db) # end create bucket # start create custom bucket -const bucket = new mongodb.GridFSBucket(db, { bucketName: 'myCustomBucket' }); +bucket = gridfs.GridFSBucket(db, bucket_name='myCustomBucket') # end create custom bucket # start upload files - fs.createReadStream('./myFile'). - pipe(bucket.openUploadStream('myFile', { - chunkSizeBytes: 1048576, - metadata: { field: 'myField', value: 'myValue' } - })); +with open("my_file", "rb") as f: + file_id = fs.put(f, filename="my_file") # end upload files # start retrieve file info - const cursor = bucket.find({}); - for await (const doc of cursor) { - console.log(doc); - } +for file in bucket.find({}): + data = file.read() # end retrieve file info # start download files name - bucket.openDownloadStreamByName('myFile'). - pipe(fs.createWriteStream('./outputFile')); +destination = open('output_file','wb') +bucket.download_to_stream_by_name("my_file", destination) # end download files name # start download files id - bucket.openDownloadStream(ObjectId("60edece5e06275bf0463aaf3")). - pipe(fs.createWriteStream('./outputFile')); +destination = open('output_file','wb+') +bucket.download_to_stream(file_id, destination) # end download files id # start rename files - bucket.rename(ObjectId("60edece5e06275bf0463aaf3"), "newFileName"); +fs.rename(file_id, "new_file_name") # end rename files # start delete files - bucket.delete(ObjectId("60edece5e06275bf0463aaf3")); +bucket.delete(file_id) # end delete files \ No newline at end of file From 18490723cb4af1f41c9080f529681f7d214d8635 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 7 Aug 2024 16:51:23 -0400 Subject: [PATCH 2/6] edit wording --- source/includes/gridfs/gridfs.py | 76 +++++++++++++++++--------------- source/write/gridfs.txt | 30 +++++++------ 2 files changed, 57 insertions(+), 49 deletions(-) diff --git a/source/includes/gridfs/gridfs.py b/source/includes/gridfs/gridfs.py index adc67a89..f108cbfe 100644 --- a/source/includes/gridfs/gridfs.py +++ b/source/includes/gridfs/gridfs.py @@ -1,36 +1,40 @@ -# start create bucket -db = MongoClient().test -bucket = gridfs.GridFSBucket(db) -# end create bucket - -# start create custom bucket -bucket = gridfs.GridFSBucket(db, bucket_name='myCustomBucket') -# end create custom bucket - -# start upload files -with open("my_file", "rb") as f: - file_id = fs.put(f, filename="my_file") -# end upload files - -# start retrieve file info -for file in bucket.find({}): - data = file.read() -# end retrieve file info - -# start download files name -destination = open('output_file','wb') -bucket.download_to_stream_by_name("my_file", destination) -# end download files name - -# start download files id -destination = open('output_file','wb+') -bucket.download_to_stream(file_id, destination) -# end download files id - -# start rename files -fs.rename(file_id, "new_file_name") -# end rename files - -# start delete files -bucket.delete(file_id) -# end delete files \ No newline at end of file + # start create bucket + client = MongoClient("") + db = client["db"] + bucket = gridfs.GridFSBucket(db) + # end create bucket + + # start create custom bucket + custom_bucket = gridfs.GridFSBucket(db, bucket_name='myCustomBucket') + # end create custom bucket + + # start upload files + source_path = './file_to_upload' + filename = 'my_file' + + with open(source_path, 'rb') as source: + bucket.upload_from_stream(filename, source) + # end upload files + + # start retrieve file info + for file_doc in bucket.find({}): + print(file_doc) + # end retrieve file info + + # start download files name + destination = open('output_file','wb') + bucket.download_to_stream_by_name('my_file', destination) + # end download files name + + # start download files id + destination = open('output_file','wb+') + bucket.download_to_stream(ObjectId('66b3c86e672a17b6c8a4a4a9'), destination) + # end download files id + + # start rename files + bucket.rename(ObjectId('66b3c86e672a17b6c8a4a4a9'), "new_file_name") + # end rename files + + # start delete files + bucket.delete(ObjectId('66b3c86e672a17b6c8a4a4a9')) + # end delete files \ No newline at end of file diff --git a/source/write/gridfs.txt b/source/write/gridfs.txt index eceea7d0..72e04a9c 100644 --- a/source/write/gridfs.txt +++ b/source/write/gridfs.txt @@ -104,12 +104,17 @@ constructor, as shown below: Upload Files ------------ -Use the ``open_upload_stream()`` method from the ``GridFSBucket`` class to create an upload -stream for a given file name. The -``open_upload_stream()`` method allows you to specify configuration information -such as file chunk size and other field/value pairs to store as metadata. Set -these options as parameters of ``open_upload_stream()``, as shown in the -following code example: +Use the ``upload_from_stream()`` method from the ``GridFSBucket`` class to upload +a file to a GridFS bucket. Pass the following parameters to the ``upload_from_stream()`` +method: + +- ``filename``: The name of the file to store the uploaded content +- ``source``: The file you want to upload +- ``chunk_size_bytes``: The number of bytes per chunk of this file (optional). +- ``metadata``: The file metadata (optional). + +The following code example uploads a file located at ``./file_to_upload`` and stores +its contents in ``my_file``: .. literalinclude:: /includes/gridfs/gridfs.py :language: python @@ -138,7 +143,7 @@ from which you can access the results. To learn more about ``Cursor`` objects in {+driver-short+}, see :ref:``. The following code example shows you how to retrieve and print file metadata -from all your files in a GridFS bucket. It uses the ``for...of`` syntax to traverse the +from all your files in a GridFS bucket. It uses the ``for...in`` syntax to traverse the ``Cursor`` iterable and display the results: .. literalinclude:: /includes/gridfs/gridfs.py @@ -158,12 +163,11 @@ Download Files -------------- You can download files from your MongoDB database by using the -``open_download_stream_by_name()`` method from ``GridFSBucket`` to create a -download stream. +``download_to_stream_by_name()`` method from the ``GridFSBucket`` +class. The following example shows you how to download a file referenced -by the file name, stored in the ``filename`` field, into your working -directory: +by the file name, ``my_file``, into a file named ``output_file``: .. literalinclude:: /includes/gridfs/gridfs.py :language: python @@ -177,7 +181,7 @@ directory: GridFS will stream the most recent file with the given name (as determined by the ``uploadDate`` field). -Alternatively, you can use the ``open_download_stream()`` +Alternatively, you can use the ``download_to_stream()`` method, which takes the ``_id`` field of a file as a parameter: .. literalinclude:: /includes/gridfs/gridfs.py @@ -204,7 +208,7 @@ bucket. You must specify the file to rename by its ``_id`` field rather than its file name. The following example shows how to update the ``filename`` field to -``"newFileName"`` by referencing a document's ``_id`` field: +``"new_file_name"`` by referencing a document's ``_id`` field: .. literalinclude:: /includes/gridfs/gridfs.py :language: python From eef12cf487a037a3c0cd3fc9fb269c52743848fc Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 7 Aug 2024 17:01:42 -0400 Subject: [PATCH 3/6] fixes --- source/includes/gridfs/gridfs.py | 19 +++++++++---------- source/write/gridfs.txt | 20 +++++++++----------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/source/includes/gridfs/gridfs.py b/source/includes/gridfs/gridfs.py index f108cbfe..50e3b9d9 100644 --- a/source/includes/gridfs/gridfs.py +++ b/source/includes/gridfs/gridfs.py @@ -5,15 +5,14 @@ # end create bucket # start create custom bucket - custom_bucket = gridfs.GridFSBucket(db, bucket_name='myCustomBucket') + custom_bucket = gridfs.GridFSBucket(db, bucket_name="myCustomBucket") # end create custom bucket # start upload files - source_path = './file_to_upload' - filename = 'my_file' - - with open(source_path, 'rb') as source: - bucket.upload_from_stream(filename, source) + with bucket.open_upload_stream( + "my_file", chunk_size_bytes=4, + metadata={"contentType": "text/plain"}) as grid_in: + grid_in.write("data to store") # end upload files # start retrieve file info @@ -22,13 +21,13 @@ # end retrieve file info # start download files name - destination = open('output_file','wb') - bucket.download_to_stream_by_name('my_file', destination) + file = bucket.open_download_stream_by_name("my_file") + contents = file.read() # end download files name # start download files id - destination = open('output_file','wb+') - bucket.download_to_stream(ObjectId('66b3c86e672a17b6c8a4a4a9'), destination) + file = bucket.open_download_stream(ObjectId('66b3c86e672a17b6c8a4a4a9')) + contents = file.read() # end download files id # start rename files diff --git a/source/write/gridfs.txt b/source/write/gridfs.txt index 72e04a9c..cfd1b7cd 100644 --- a/source/write/gridfs.txt +++ b/source/write/gridfs.txt @@ -104,17 +104,15 @@ constructor, as shown below: Upload Files ------------ -Use the ``upload_from_stream()`` method from the ``GridFSBucket`` class to upload -a file to a GridFS bucket. Pass the following parameters to the ``upload_from_stream()`` +Use the ``open_upload_stream()`` method from the ``GridFSBucket`` class to upload +a file to a GridFS bucket. Pass the following parameters to the ``open_upload_stream()`` method: -- ``filename``: The name of the file to store the uploaded content -- ``source``: The file you want to upload -- ``chunk_size_bytes``: The number of bytes per chunk of this file (optional). -- ``metadata``: The file metadata (optional). +- ``filename``: The name of the file you want to upload +- ``chunk_size_bytes``: The number of bytes per chunk of this file (optional) +- ``metadata``: The file metadata (optional) -The following code example uploads a file located at ``./file_to_upload`` and stores -its contents in ``my_file``: +The following code example uploads a file named ``"my_file"`` to a GridFS bucket: .. literalinclude:: /includes/gridfs/gridfs.py :language: python @@ -163,11 +161,11 @@ Download Files -------------- You can download files from your MongoDB database by using the -``download_to_stream_by_name()`` method from the ``GridFSBucket`` +``open_download_stream_by_name()`` method from the ``GridFSBucket`` class. The following example shows you how to download a file referenced -by the file name, ``my_file``, into a file named ``output_file``: +by the file name, ``"my_file"``, and read its contents: .. literalinclude:: /includes/gridfs/gridfs.py :language: python @@ -181,7 +179,7 @@ by the file name, ``my_file``, into a file named ``output_file``: GridFS will stream the most recent file with the given name (as determined by the ``uploadDate`` field). -Alternatively, you can use the ``download_to_stream()`` +Alternatively, you can use the ``open_download_stream()`` method, which takes the ``_id`` field of a file as a parameter: .. literalinclude:: /includes/gridfs/gridfs.py From 206c173ef61e855d0c4a4962ac84c0f6c8b4ee4b Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 7 Aug 2024 17:06:02 -0400 Subject: [PATCH 4/6] reword --- source/includes/gridfs/gridfs.py | 2 +- source/write/gridfs.txt | 19 ++++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/source/includes/gridfs/gridfs.py b/source/includes/gridfs/gridfs.py index 50e3b9d9..f7acc899 100644 --- a/source/includes/gridfs/gridfs.py +++ b/source/includes/gridfs/gridfs.py @@ -10,7 +10,7 @@ # start upload files with bucket.open_upload_stream( - "my_file", chunk_size_bytes=4, + "my_file", chunk_size_bytes=1048576, metadata={"contentType": "text/plain"}) as grid_in: grid_in.write("data to store") # end upload files diff --git a/source/write/gridfs.txt b/source/write/gridfs.txt index cfd1b7cd..564c59f7 100644 --- a/source/write/gridfs.txt +++ b/source/write/gridfs.txt @@ -104,15 +104,12 @@ constructor, as shown below: Upload Files ------------ -Use the ``open_upload_stream()`` method from the ``GridFSBucket`` class to upload -a file to a GridFS bucket. Pass the following parameters to the ``open_upload_stream()`` -method: - -- ``filename``: The name of the file you want to upload -- ``chunk_size_bytes``: The number of bytes per chunk of this file (optional) -- ``metadata``: The file metadata (optional) - -The following code example uploads a file named ``"my_file"`` to a GridFS bucket: +Use the ``open_upload_stream()`` method from the ``GridFSBucket`` class to +create an upload stream for a given file name. The ``open_upload_stream()`` +method allows you to specify configuration information such as file chunk +size and other field/value pairs to store as metadata. Set these options +as parameters of ``open_upload_stream()``, as shown in the following code +example: .. literalinclude:: /includes/gridfs/gridfs.py :language: python @@ -161,8 +158,8 @@ Download Files -------------- You can download files from your MongoDB database by using the -``open_download_stream_by_name()`` method from the ``GridFSBucket`` -class. +``open_download_stream_by_name()`` method from ``GridFSBucket`` to create a +download stream. The following example shows you how to download a file referenced by the file name, ``"my_file"``, and read its contents: From 508e58d2641b54df660a19edbb1eafe06fb6ccb8 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 7 Aug 2024 17:06:57 -0400 Subject: [PATCH 5/6] dedent --- source/write/gridfs.txt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/write/gridfs.txt b/source/write/gridfs.txt index 564c59f7..7fbd67ff 100644 --- a/source/write/gridfs.txt +++ b/source/write/gridfs.txt @@ -83,7 +83,7 @@ call read and write operations on the files in your bucket. .. literalinclude:: /includes/gridfs/gridfs.py :language: python - :copyable: true + :dedent: :start-after: start create bucket :end-before: end create bucket @@ -95,7 +95,7 @@ constructor, as shown below: .. literalinclude:: /includes/gridfs/gridfs.py :language: python - :copyable: true + :dedent: :start-after: start create custom bucket :end-before: end create custom bucket @@ -113,7 +113,7 @@ example: .. literalinclude:: /includes/gridfs/gridfs.py :language: python - :copyable: true + :dedent: :start-after: start upload files :end-before: end upload files @@ -143,7 +143,7 @@ from all your files in a GridFS bucket. It uses the ``for...in`` syntax to trave .. literalinclude:: /includes/gridfs/gridfs.py :language: python - :copyable: true + :dedent: :start-after: start retrieve file info :end-before: end retrieve file info @@ -166,7 +166,7 @@ by the file name, ``"my_file"``, and read its contents: .. literalinclude:: /includes/gridfs/gridfs.py :language: python - :copyable: true + :dedent: :start-after: start download files name :end-before: end download files name @@ -181,7 +181,7 @@ method, which takes the ``_id`` field of a file as a parameter: .. literalinclude:: /includes/gridfs/gridfs.py :language: python - :copyable: true + :dedent: :start-after: start download files id :end-before: end download files id @@ -207,7 +207,7 @@ The following example shows how to update the ``filename`` field to .. literalinclude:: /includes/gridfs/gridfs.py :language: python - :copyable: true + :dedent: :start-after: start rename files :end-before: end rename files @@ -232,7 +232,7 @@ The following example shows you how to delete a file by referencing its ``_id`` .. literalinclude:: /includes/gridfs/gridfs.py :language: python - :copyable: true + :dedent: :start-after: start delete files :end-before: end delete files From e6f49b41119a093828f4fa682c977a459b98ae54 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 8 Aug 2024 10:07:32 -0400 Subject: [PATCH 6/6] RR feedback --- source/includes/gridfs/gridfs.py | 64 ++++++++++++++++---------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/source/includes/gridfs/gridfs.py b/source/includes/gridfs/gridfs.py index f7acc899..c7162f73 100644 --- a/source/includes/gridfs/gridfs.py +++ b/source/includes/gridfs/gridfs.py @@ -1,39 +1,39 @@ - # start create bucket - client = MongoClient("") - db = client["db"] - bucket = gridfs.GridFSBucket(db) - # end create bucket +# start create bucket +client = MongoClient("") +db = client["db"] +bucket = gridfs.GridFSBucket(db) +# end create bucket - # start create custom bucket - custom_bucket = gridfs.GridFSBucket(db, bucket_name="myCustomBucket") - # end create custom bucket +# start create custom bucket +custom_bucket = gridfs.GridFSBucket(db, bucket_name="myCustomBucket") +# end create custom bucket - # start upload files - with bucket.open_upload_stream( - "my_file", chunk_size_bytes=1048576, - metadata={"contentType": "text/plain"}) as grid_in: - grid_in.write("data to store") - # end upload files +# start upload files +with bucket.open_upload_stream( + "my_file", chunk_size_bytes=1048576, metadata={"contentType": "text/plain"} +) as grid_in: + grid_in.write("data to store") +# end upload files - # start retrieve file info - for file_doc in bucket.find({}): - print(file_doc) - # end retrieve file info +# start retrieve file info +for file_doc in bucket.find({}): + print(file_doc) +# end retrieve file info - # start download files name - file = bucket.open_download_stream_by_name("my_file") - contents = file.read() - # end download files name +# start download files name +file = bucket.open_download_stream_by_name("my_file") +contents = file.read() +# end download files name - # start download files id - file = bucket.open_download_stream(ObjectId('66b3c86e672a17b6c8a4a4a9')) - contents = file.read() - # end download files id +# start download files id +file = bucket.open_download_stream(ObjectId("66b3c86e672a17b6c8a4a4a9")) +contents = file.read() +# end download files id - # start rename files - bucket.rename(ObjectId('66b3c86e672a17b6c8a4a4a9'), "new_file_name") - # end rename files +# start rename files +bucket.rename(ObjectId("66b3c86e672a17b6c8a4a4a9"), "new_file_name") +# end rename files - # start delete files - bucket.delete(ObjectId('66b3c86e672a17b6c8a4a4a9')) - # end delete files \ No newline at end of file +# start delete files +bucket.delete(ObjectId("66b3c86e672a17b6c8a4a4a9")) +# end delete files