MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / GridFS

Class GridFS

gridfs/synchronous/grid_file.py:70–456  ·  view source on GitHub ↗

An instance of GridFS on top of a single Database.

Source from the content-addressed store, hash-verified

68
69
70class GridFS:
71 """An instance of GridFS on top of a single Database."""
72
73 def __init__(self, database: Database[Any], collection: str = "fs"):
74 """Create a new instance of :class:`GridFS`.
75
76 Raises :class:`TypeError` if `database` is not an instance of
77 :class:`~pymongo.database.Database`.
78
79 :param database: database to use
80 :param collection: root collection to use
81
82 .. versionchanged:: 4.0
83 Removed the `disable_md5` parameter. See
84 :ref:`removed-gridfs-checksum` for details.
85
86 .. versionchanged:: 3.11
87 Running a GridFS operation in a transaction now always raises an
88 error. GridFS does not support multi-document transactions.
89
90 .. versionchanged:: 3.7
91 Added the `disable_md5` parameter.
92
93 .. versionchanged:: 3.1
94 Indexes are only ensured on the first write to the DB.
95
96 .. versionchanged:: 3.0
97 `database` must use an acknowledged
98 :attr:`~pymongo.database.Database.write_concern`
99
100 .. seealso:: The MongoDB documentation on `gridfs <https://dochub.mongodb.org/core/gridfs>`_.
101 """
102 if not isinstance(database, Database):
103 raise TypeError(f"database must be an instance of Database, not {type(database)}")
104
105 database = _clear_entity_type_registry(database)
106
107 if not database.write_concern.acknowledged:
108 raise ConfigurationError("database must use acknowledged write_concern")
109
110 self._collection = database[collection]
111 self._files = self._collection.files
112 self._chunks = self._collection.chunks
113
114 def new_file(self, **kwargs: Any) -> GridIn:
115 """Create a new file in GridFS.
116
117 Returns a new :class:`~gridfs.grid_file.GridIn` instance to
118 which data can be written. Any keyword arguments will be
119 passed through to :meth:`~gridfs.grid_file.GridIn`.
120
121 If the ``"_id"`` of the file is manually specified, it must
122 not already exist in GridFS. Otherwise
123 :class:`~gridfs.errors.FileExists` is raised.
124
125 :param kwargs: keyword arguments for file creation
126 """
127 return GridIn(self._collection, **kwargs)

Calls

no outgoing calls

Tested by 3

test_gridfsMethod · 0.72