o Sb>@sdZddlmZddlmZmZmZmZmZddl m Z ddl m Z ddl mZmZmZmZmZmZddlmZmZmZddlmZdd lmZdd lmZdd lmZdd l m!Z!dd l"m#Z#ddl$m%Z%gdZ&Gddde'Z(Gddde'Z)dS)aGridFS is a specification for storing large objects in Mongo. The :mod:`gridfs` package is an implementation of GridFS on top of :mod:`pymongo`, exposing a file-like interface. .. seealso:: The MongoDB documentation on `gridfs `_. )abc)AnyListMappingOptionalcast)ObjectId)NoFile)DEFAULT_CHUNK_SIZEGridInGridOut GridOutCursor_clear_entity_type_registry_disallow_transactions) ASCENDING DESCENDING_csot) ClientSession) Collection)validate_string)Database)ConfigurationError) _ServerMode) WriteConcern)GridFS GridFSBucketr r r r r c @sfeZdZdZd'dedefddZdedefd d Z d ededefd d Z d(dede e de fddZ   d)de ede ede e dede f ddZ d*de ede e dede fddZd(dede e ddfddZd(de e deefddZ  d*de ede e dedede e f d d!Zdededefd"d#Z  d*d$e ede e dedefd%d&ZdS)+r2An instance of GridFS on top of a single Database.fsdatabase collectioncCsLt|ts tdt|}|jjstd|||_|jj|_ |jj |_ dS)aCreate a new instance of :class:`GridFS`. Raises :class:`TypeError` if `database` is not an instance of :class:`~pymongo.database.Database`. :Parameters: - `database`: database to use - `collection` (optional): root collection to use .. versionchanged:: 4.0 Removed the `disable_md5` parameter. See :ref:`removed-gridfs-checksum` for details. .. versionchanged:: 3.11 Running a GridFS operation in a transaction now always raises an error. GridFS does not support multi-document transactions. .. versionchanged:: 3.7 Added the `disable_md5` parameter. .. versionchanged:: 3.1 Indexes are only ensured on the first write to the DB. .. versionchanged:: 3.0 `database` must use an acknowledged :attr:`~pymongo.database.Database.write_concern` .. seealso:: The MongoDB documentation on `gridfs `_. (database must be an instance of Databasez,database must use acknowledged write_concernN) isinstancer TypeErrorr write_concern acknowledgedr_GridFS__collectionfiles_GridFS__fileschunks_GridFS__chunks)selfrrr+6/tmp/pip-target-onvjaxws/lib/python/gridfs/__init__.py__init__;s   zGridFS.__init__kwargsreturncKst|jfi|S)aCreate a new file in GridFS. Returns a new :class:`~gridfs.grid_file.GridIn` instance to which data can be written. Any keyword arguments will be passed through to :meth:`~gridfs.grid_file.GridIn`. If the ``"_id"`` of the file is manually specified, it must not already exist in GridFS. Otherwise :class:`~gridfs.errors.FileExists` is raised. :Parameters: - `**kwargs` (optional): keyword arguments for file creation )r r%)r*r.r+r+r,new_fileeszGridFS.new_filedatacKsFt|jfi|}|||jWdS1swYdS)aPut data in GridFS as a new file. Equivalent to doing:: with fs.new_file(**kwargs) as f: f.write(data) `data` can be either an instance of :class:`bytes` or a file-like object providing a :meth:`read` method. If an `encoding` keyword argument is passed, `data` can also be a :class:`str` instance, which will be encoded as `encoding` before being written. Any keyword arguments will be passed through to the created file - see :meth:`~gridfs.grid_file.GridIn` for possible arguments. Returns the ``"_id"`` of the created file. If the ``"_id"`` of the file is manually specified, it must not already exist in GridFS. Otherwise :class:`~gridfs.errors.FileExists` is raised. :Parameters: - `data`: data to be written as a file. - `**kwargs` (optional): keyword arguments for file creation .. versionchanged:: 3.0 w=0 writes to GridFS are now prohibited. N)r r%write_id)r*r1r. grid_filer+r+r,putus $z GridFS.putNfile_idsessioncCt|j||d}||S)aGet a file from GridFS by ``"_id"``. Returns an instance of :class:`~gridfs.grid_file.GridOut`, which provides a file-like interface for reading. :Parameters: - `file_id`: ``"_id"`` of the file to get - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. r7)r r% _ensure_filer*r6r7goutr+r+r,getsz GridFS.getfilenameversionc Ks|}|dur ||d<t||jj||d}|durd}|dkr3t|d}|d|dtn |d|dtz t |}t |j ||dWSt y[t d ||fw) a(Get a file from GridFS by ``"filename"`` or metadata fields. Returns a version of the file in GridFS whose filename matches `filename` and whose metadata fields match the supplied keyword arguments, as an instance of :class:`~gridfs.grid_file.GridOut`. Version numbering is a convenience atop the GridFS API provided by MongoDB. If more than one file matches the query (either by `filename` alone, by metadata fields, or by a combination of both), then version ``-1`` will be the most recently uploaded matching file, ``-2`` the second most recently uploaded, etc. Version ``0`` will be the first version uploaded, ``1`` the second version, etc. So if three versions have been uploaded, then version ``0`` is the same as version ``-3``, version ``1`` is the same as version ``-2``, and version ``2`` is the same as version ``-1``. Raises :class:`~gridfs.errors.NoFile` if no such version of that file exists. :Parameters: - `filename`: ``"filename"`` of the file to get, or `None` - `version` (optional): version of the file to get (defaults to -1, the most recent version uploaded) - `session` (optional): a :class:`~pymongo.client_session.ClientSession` - `**kwargs` (optional): find files by custom metadata. .. versionchanged:: 3.6 Added ``session`` parameter. .. versionchanged:: 3.1 ``get_version`` no longer ensures indexes. Nr?r9r>r uploadDateZ file_documentr7no version %d for filename %r)rr'findabslimitskipsortrrnextr r% StopIterationr ) r*r?r@r7r.querycursorrHdocr+r+r, get_versions")  zGridFS.get_versioncKs|jd||d|S)a Get the most recent version of a file in GridFS by ``"filename"`` or metadata fields. Equivalent to calling :meth:`get_version` with the default `version` (``-1``). :Parameters: - `filename`: ``"filename"`` of the file to get, or `None` - `session` (optional): a :class:`~pymongo.client_session.ClientSession` - `**kwargs` (optional): find files by custom metadata. .. versionchanged:: 3.6 Added ``session`` parameter. )r?r7Nr+)rO)r*r?r7r.r+r+r,get_last_versionszGridFS.get_last_versioncCs4t||jjd|i|d|jjd|i|ddS)a`Delete a file from GridFS by ``"_id"``. Deletes all data belonging to the file with ``"_id"``: `file_id`. .. warning:: Any processes/threads reading from the file while this method is executing will likely see an invalid/corrupt file. Care should be taken to avoid concurrent reads to a file while it is being deleted. .. note:: Deletes of non-existent files are considered successful since the end result is the same: no file with that _id remains. :Parameters: - `file_id`: ``"_id"`` of the file to delete - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. .. versionchanged:: 3.1 ``delete`` no longer ensures indexes. r3r9files_idN)rr' delete_oner) delete_many)r*r6r7r+r+r,deletesz GridFS.deletecCs"t|dd|jjd|dDS)afList the names of all files stored in this instance of :class:`GridFS`. :Parameters: - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. .. versionchanged:: 3.1 ``list`` no longer ensures indexes. cSsg|]}|dur|qSNr+).0namer+r+r, (s zGridFS.list..r?r9)rr'Zdistinct)r*r7r+r+r,listsz GridFS.listfilterargscOsP|durt|tjsd|i}t||j|g|Rd|i|D]}|SdS)aGet a single file from gridfs. All arguments to :meth:`find` are also valid arguments for :meth:`find_one`, although any `limit` argument will be ignored. Returns a single :class:`~gridfs.grid_file.GridOut`, or ``None`` if no matching file is found. For example:: file = fs.find_one({"filename": "lisa.txt"}) :Parameters: - `filter` (optional): a dictionary specifying the query to be performing OR any other type to be used as the value for a query for ``"_id"`` in the file collection. - `*args` (optional): any additional positional arguments are the same as the arguments to :meth:`find`. - `session` (optional): a :class:`~pymongo.client_session.ClientSession` - `**kwargs` (optional): any additional keyword arguments are the same as the arguments to :meth:`find`. .. versionchanged:: 3.6 Added ``session`` parameter. Nr3r7)r!rrrrE)r*rZr7r[r.fr+r+r,find_one,s  zGridFS.find_onecOt|jg|Ri|S)a! Query GridFS for files. Returns a cursor that iterates across files matching arbitrary queries on the files collection. Can be combined with other modifiers for additional control. For example:: for grid_out in fs.find({"filename": "lisa.txt"}, no_cursor_timeout=True): data = grid_out.read() would iterate through all versions of "lisa.txt" stored in GridFS. Note that setting no_cursor_timeout to True may be important to prevent the cursor from timing out during long multi-file processing work. As another example, the call:: most_recent_three = fs.find().sort("uploadDate", -1).limit(3) would return a cursor to the three most recently uploaded files in GridFS. Follows a similar interface to :meth:`~pymongo.collection.Collection.find` in :class:`~pymongo.collection.Collection`. If a :class:`~pymongo.client_session.ClientSession` is passed to :meth:`find`, all returned :class:`~gridfs.grid_file.GridOut` instances are associated with that session. :Parameters: - `filter` (optional): A query document that selects which files to include in the result set. Can be an empty document to include all files. - `skip` (optional): the number of files to omit (from the start of the result set) when returning the results - `limit` (optional): the maximum number of results to return - `no_cursor_timeout` (optional): if False (the default), any returned cursor is closed by the server after 10 minutes of inactivity. If set to True, the returned cursor will never time out on the server. Care should be taken to ensure that cursors with no_cursor_timeout turned on are properly closed. - `sort` (optional): a list of (key, direction) pairs specifying the sort order for this query. See :meth:`~pymongo.cursor.Cursor.sort` for details. Raises :class:`TypeError` if any of the arguments are of improper type. Returns an instance of :class:`~gridfs.grid_file.GridOutCursor` corresponding to this query. .. versionchanged:: 3.0 Removed the read_preference, tag_sets, and secondary_acceptable_latency_ms options. .. versionadded:: 2.7 .. seealso:: The MongoDB documentation on `find `_. )r r%r*r[r.r+r+r,rESs;z GridFS.finddocument_or_idcKsDt||r|jj|dg|d}|duS|jj|dg|d}|duS)aQCheck if a file exists in this instance of :class:`GridFS`. The file to check for can be specified by the value of its ``_id`` key, or by passing in a query document. A query document can be passed in as dictionary, or by using keyword arguments. Thus, the following three calls are equivalent: >>> fs.exists(file_id) >>> fs.exists({"_id": file_id}) >>> fs.exists(_id=file_id) As are the following two calls: >>> fs.exists({"filename": "mike.txt"}) >>> fs.exists(filename="mike.txt") And the following two: >>> fs.exists({"foo": {"$gt": 12}}) >>> fs.exists(foo={"$gt": 12}) Returns ``True`` if a matching file exists, ``False`` otherwise. Calls to :meth:`exists` will not automatically create appropriate indexes; application developers should be sure to create indexes if needed and as appropriate. :Parameters: - `document_or_id` (optional): query document, or _id of the document to check for - `session` (optional): a :class:`~pymongo.client_session.ClientSession` - `**kwargs` (optional): keyword arguments are used as a query document, if they're present. .. versionchanged:: 3.6 Added ``session`` parameter. r3r9N)rr'r])r*r`r7r.r\r+r+r,existss +z GridFS.exists)rrU)Nr>N)NN)__name__ __module__ __qualname____doc__rstrr-rr r0r5rrr r=intrOrPrTrrYr]r rEboolrar+r+r+r,r8sr*  =  '?rc@s8eZdZdZdeddfdedededee dee d df d d Z   d-d edeed ee ee fdeed ef ddZ   d-de d edeed ee ee fdeed ef ddZej   d-d ede deed ee ee fdeed ef ddZej   d-de d ede deed ee ee fdeed dfddZ d.de deed efddZej d.de de deed dfddZejd.de deed dfddZd e d!e d efd"d#Z d/d ed%edeed efd&d'Zej $ d/d ede d%edeed df d(d)Z d.de d*edeed dfd+d,ZdS)0rrrNdb bucket_namechunk_size_bytesr#read_preferencer/cCst|ts tdt|}|dur|n|j}|jstd||_|||_|jj j ||d|_ |jj j ||d|_ ||_|jjj|_dS)a]Create a new instance of :class:`GridFSBucket`. Raises :exc:`TypeError` if `database` is not an instance of :class:`~pymongo.database.Database`. Raises :exc:`~pymongo.errors.ConfigurationError` if `write_concern` is not acknowledged. :Parameters: - `database`: database to use. - `bucket_name` (optional): The name of the bucket. Defaults to 'fs'. - `chunk_size_bytes` (optional): The chunk size in bytes. Defaults to 255KB. - `write_concern` (optional): The :class:`~pymongo.write_concern.WriteConcern` to use. If ``None`` (the default) db.write_concern is used. - `read_preference` (optional): The read preference to use. If ``None`` (the default) db.read_preference is used. .. versionchanged:: 4.0 Removed the `disable_md5` parameter. See :ref:`removed-gridfs-checksum` for details. .. versionchanged:: 3.11 Running a GridFSBucket operation in a transaction now always raises an error. GridFSBucket does not support multi-document transactions. .. versionchanged:: 3.7 Added the `disable_md5` parameter. .. versionadded:: 3.1 .. seealso:: The MongoDB documentation on `gridfs `_. r Nz"write concern must be acknowledged)r#rl)r!rr"rr#r$rZ _bucket_name _collectionr(Z with_options_chunksr&_files_chunk_size_bytesclientoptionstimeout_timeout)r*rirjrkr#rlZwtcr+r+r,r-s * zGridFSBucket.__init__r?metadatar7cCsHtd|||dur |n|jd}|dur||d<t|jfd|i|S)aOpens a Stream that the application can write the contents of the file to. The user must specify the filename, and can choose to add any additional information in the metadata field of the file document or modify the chunk size. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) with fs.open_upload_stream( "test_file", chunk_size_bytes=4, metadata={"contentType": "text/plain"}) as grid_in: grid_in.write("data I want to store!") # uploaded on close Returns an instance of :class:`~gridfs.grid_file.GridIn`. Raises :exc:`~gridfs.errors.NoFile` if no such version of that file exists. Raises :exc:`~ValueError` if `filename` is not a string. :Parameters: - `filename`: The name of the file to upload. - `chunk_size_bytes` (options): The number of bytes per chunk of this file. Defaults to the chunk_size_bytes in :class:`GridFSBucket`. - `metadata` (optional): User data for the 'metadata' field of the files collection document. If not provided the metadata field will be omitted from the files collection document. - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. r?N)r? chunk_sizerur7rrpr rm)r*r?rkrur7optsr+r+r,open_upload_streams *zGridFSBucket.open_upload_streamr6cCsJtd||||dur |n|jd}|dur||d<t|jfd|i|S)a_Opens a Stream that the application can write the contents of the file to. The user must specify the file id and filename, and can choose to add any additional information in the metadata field of the file document or modify the chunk size. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) with fs.open_upload_stream_with_id( ObjectId(), "test_file", chunk_size_bytes=4, metadata={"contentType": "text/plain"}) as grid_in: grid_in.write("data I want to store!") # uploaded on close Returns an instance of :class:`~gridfs.grid_file.GridIn`. Raises :exc:`~gridfs.errors.NoFile` if no such version of that file exists. Raises :exc:`~ValueError` if `filename` is not a string. :Parameters: - `file_id`: The id to use for this file. The id must not have already been used for another file. - `filename`: The name of the file to upload. - `chunk_size_bytes` (options): The number of bytes per chunk of this file. Defaults to the chunk_size_bytes in :class:`GridFSBucket`. - `metadata` (optional): User data for the 'metadata' field of the files collection document. If not provided the metadata field will be omitted from the files collection document. - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. r?N)r3r?rvrur7rw)r*r6r?rkrur7rxr+r+r,open_upload_stream_with_id>s /z'GridFSBucket.open_upload_stream_with_idsourcecCsH|j||||d }||Wdn1swYtt|jS)aUploads a user file to a GridFS bucket. Reads the contents of the user file from `source` and uploads it to the file `filename`. Source can be a string or file-like object. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) file_id = fs.upload_from_stream( "test_file", "data I want to store!", chunk_size_bytes=4, metadata={"contentType": "text/plain"}) Returns the _id of the uploaded file. Raises :exc:`~gridfs.errors.NoFile` if no such version of that file exists. Raises :exc:`~ValueError` if `filename` is not a string. :Parameters: - `filename`: The name of the file to upload. - `source`: The source stream of the content to be uploaded. Must be a file-like object that implements :meth:`read` or a string. - `chunk_size_bytes` (options): The number of bytes per chunk of this file. Defaults to the chunk_size_bytes of :class:`GridFSBucket`. - `metadata` (optional): User data for the 'metadata' field of the files collection document. If not provided the metadata field will be omitted from the files collection document. - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. r9N)ryr2rrr3)r*r?r{rkrur7ginr+r+r,upload_from_stream{s,  zGridFSBucket.upload_from_streamcCsD|j|||||d}||WddS1swYdS)a3Uploads a user file to a GridFS bucket with a custom file id. Reads the contents of the user file from `source` and uploads it to the file `filename`. Source can be a string or file-like object. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) file_id = fs.upload_from_stream( ObjectId(), "test_file", "data I want to store!", chunk_size_bytes=4, metadata={"contentType": "text/plain"}) Raises :exc:`~gridfs.errors.NoFile` if no such version of that file exists. Raises :exc:`~ValueError` if `filename` is not a string. :Parameters: - `file_id`: The id to use for this file. The id must not have already been used for another file. - `filename`: The name of the file to upload. - `source`: The source stream of the content to be uploaded. Must be a file-like object that implements :meth:`read` or a string. - `chunk_size_bytes` (options): The number of bytes per chunk of this file. Defaults to the chunk_size_bytes of :class:`GridFSBucket`. - `metadata` (optional): User data for the 'metadata' field of the files collection document. If not provided the metadata field will be omitted from the files collection document. - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. r9N)rzr2)r*r6r?r{rkrur7r|r+r+r,upload_from_stream_with_ids .  "z'GridFSBucket.upload_from_stream_with_idcCr8)a5Opens a Stream from which the application can read the contents of the stored file specified by file_id. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) # get _id of file to read. file_id = fs.upload_from_stream("test_file", "data I want to store!") grid_out = fs.open_download_stream(file_id) contents = grid_out.read() Returns an instance of :class:`~gridfs.grid_file.GridOut`. Raises :exc:`~gridfs.errors.NoFile` if no file with file_id exists. :Parameters: - `file_id`: The _id of the file to be downloaded. - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. r9)r rmr:r;r+r+r,open_download_streamsz!GridFSBucket.open_download_stream destinationcCsH|j||d}|D]}||q WddS1swYdS)aDownloads the contents of the stored file specified by file_id and writes the contents to `destination`. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) # Get _id of file to read file_id = fs.upload_from_stream("test_file", "data I want to store!") # Get file to write to file = open('myfile','wb+') fs.download_to_stream(file_id, file) file.seek(0) contents = file.read() Raises :exc:`~gridfs.errors.NoFile` if no file with file_id exists. :Parameters: - `file_id`: The _id of the file to be downloaded. - `destination`: a file-like object implementing :meth:`write`. - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. r9N)rr2)r*r6rr7r<chunkr+r+r,download_to_streams  "zGridFSBucket.download_to_streamcCsFt||jjd|i|d}|jjd|i|d|js!td|dS)aGiven an file_id, delete this stored file's files collection document and associated chunks from a GridFS bucket. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) # Get _id of file to delete file_id = fs.upload_from_stream("test_file", "data I want to store!") fs.delete(file_id) Raises :exc:`~gridfs.errors.NoFile` if no file with file_id exists. :Parameters: - `file_id`: The _id of the file to be deleted. - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. r3r9rQz0no file could be deleted because none matched %sN)rrorRrnrSZ deleted_countr )r*r6r7resr+r+r,rT"s  zGridFSBucket.deleter[r.cOr^)aEFind and return the files collection documents that match ``filter`` Returns a cursor that iterates across files matching arbitrary queries on the files collection. Can be combined with other modifiers for additional control. For example:: for grid_data in fs.find({"filename": "lisa.txt"}, no_cursor_timeout=True): data = grid_data.read() would iterate through all versions of "lisa.txt" stored in GridFS. Note that setting no_cursor_timeout to True may be important to prevent the cursor from timing out during long multi-file processing work. As another example, the call:: most_recent_three = fs.find().sort("uploadDate", -1).limit(3) would return a cursor to the three most recently uploaded files in GridFS. Follows a similar interface to :meth:`~pymongo.collection.Collection.find` in :class:`~pymongo.collection.Collection`. If a :class:`~pymongo.client_session.ClientSession` is passed to :meth:`find`, all returned :class:`~gridfs.grid_file.GridOut` instances are associated with that session. :Parameters: - `filter`: Search query. - `batch_size` (optional): The number of documents to return per batch. - `limit` (optional): The maximum number of documents to return. - `no_cursor_timeout` (optional): The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to True prevent that. - `skip` (optional): The number of documents to skip before returning. - `sort` (optional): The order by which to sort results. Defaults to None. )r rmr_r+r+r,rE?s.zGridFSBucket.findr>revisioncCstd|d|i}t||jj||d}|dkr,t|d}|d|dtn |d|dt z t |}t |j ||dWSt yTtd||fw) aOpens a Stream from which the application can read the contents of `filename` and optional `revision`. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) grid_out = fs.open_download_stream_by_name("test_file") contents = grid_out.read() Returns an instance of :class:`~gridfs.grid_file.GridOut`. Raises :exc:`~gridfs.errors.NoFile` if no such version of that file exists. Raises :exc:`~ValueError` filename is not a string. :Parameters: - `filename`: The name of the file to read from. - `revision` (optional): Which revision (documents with the same filename and different uploadDate) of the file to retrieve. Defaults to -1 (the most recent revision). - `session` (optional): a :class:`~pymongo.client_session.ClientSession` :Note: Revision numbers are defined as follows: - 0 = the original stored file - 1 = the first revision - 2 = the second revision - etc... - -2 = the second most recent revision - -1 = the most recent revision .. versionchanged:: 3.6 Added ``session`` parameter. r?r9rrAr>rBrCrD)rrrorErFrGrHrIrrrJr rmrKr )r*r?rr7rLrMrHr4r+r+r,open_download_stream_by_nameos (  z)GridFSBucket.open_download_stream_by_namecCsJ|j|||d}|D]}||q WddS1swYdS)aWrite the contents of `filename` (with optional `revision`) to `destination`. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) # Get file to write to file = open('myfile','wb') fs.download_to_stream_by_name("test_file", file) Raises :exc:`~gridfs.errors.NoFile` if no such version of that file exists. Raises :exc:`~ValueError` if `filename` is not a string. :Parameters: - `filename`: The name of the file to read from. - `destination`: A file-like object that implements :meth:`write`. - `revision` (optional): Which revision (documents with the same filename and different uploadDate) of the file to retrieve. Defaults to -1 (the most recent revision). - `session` (optional): a :class:`~pymongo.client_session.ClientSession` :Note: Revision numbers are defined as follows: - 0 = the original stored file - 1 = the first revision - 2 = the second revision - etc... - -2 = the second most recent revision - -1 = the most recent revision .. versionchanged:: 3.6 Added ``session`` parameter. r9N)rr2)r*r?rrr7r<rr+r+r,download_to_stream_by_names - "z'GridFSBucket.download_to_stream_by_name new_filenamecCs@t||jjd|idd|ii|d}|jstd||fdS)aRenames the stored file with the specified file_id. For example:: my_db = MongoClient().test fs = GridFSBucket(my_db) # Get _id of file to rename file_id = fs.upload_from_stream("test_file", "data I want to store!") fs.rename(file_id, "new_test_name") Raises :exc:`~gridfs.errors.NoFile` if no file with file_id exists. :Parameters: - `file_id`: The _id of the file to be renamed. - `new_filename`: The new name of the file. - `session` (optional): a :class:`~pymongo.client_session.ClientSession` .. versionchanged:: 3.6 Added ``session`` parameter. r3z$setr?r9zN) rbrcrdrer rrfrgrrrr-rrrr ryrzrapplyrr}r~r rrrTr rErrrr+r+r+r,rs C ; = 0 3 ! !1 7 1rN)*re collectionsrtypingrrrrrZ bson.objectidrZ gridfs.errorsr Zgridfs.grid_filer r r r rrZpymongorrrZpymongo.client_sessionrZpymongo.collectionrZpymongo.commonrZpymongo.databaserZpymongo.errorsrZpymongo.read_preferencesrZpymongo.write_concernr__all__objectrrr+r+r+r,s(