__init__.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. # Copyright 2009-present MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """GridFS is a specification for storing large objects in Mongo.
  15. The :mod:`gridfs` package is an implementation of GridFS on top of
  16. :mod:`pymongo`, exposing a file-like interface.
  17. .. mongodoc:: gridfs
  18. """
  19. from bson.py3compat import abc
  20. from gridfs.errors import NoFile
  21. from gridfs.grid_file import (GridIn,
  22. GridOut,
  23. GridOutCursor,
  24. DEFAULT_CHUNK_SIZE,
  25. _clear_entity_type_registry,
  26. _disallow_transactions)
  27. from pymongo import (ASCENDING,
  28. DESCENDING)
  29. from pymongo.common import UNAUTHORIZED_CODES, validate_string
  30. from pymongo.database import Database
  31. from pymongo.errors import ConfigurationError, OperationFailure
  32. class GridFS(object):
  33. """An instance of GridFS on top of a single Database.
  34. """
  35. def __init__(self, database, collection="fs", disable_md5=False):
  36. """Create a new instance of :class:`GridFS`.
  37. Raises :class:`TypeError` if `database` is not an instance of
  38. :class:`~pymongo.database.Database`.
  39. :Parameters:
  40. - `database`: database to use
  41. - `collection` (optional): root collection to use
  42. - `disable_md5` (optional): When True, MD5 checksums will not be
  43. computed for uploaded files. Useful in environments where MD5
  44. cannot be used for regulatory or other reasons. Defaults to False.
  45. .. versionchanged:: 3.11
  46. Running a GridFS operation in a transaction now always raises an
  47. error. GridFS does not support multi-document transactions.
  48. .. versionchanged:: 3.1
  49. Indexes are only ensured on the first write to the DB.
  50. .. versionchanged:: 3.0
  51. `database` must use an acknowledged
  52. :attr:`~pymongo.database.Database.write_concern`
  53. .. mongodoc:: gridfs
  54. """
  55. if not isinstance(database, Database):
  56. raise TypeError("database must be an instance of Database")
  57. database = _clear_entity_type_registry(database)
  58. if not database.write_concern.acknowledged:
  59. raise ConfigurationError('database must use '
  60. 'acknowledged write_concern')
  61. self.__collection = database[collection]
  62. self.__files = self.__collection.files
  63. self.__chunks = self.__collection.chunks
  64. self.__disable_md5 = disable_md5
  65. def new_file(self, **kwargs):
  66. """Create a new file in GridFS.
  67. Returns a new :class:`~gridfs.grid_file.GridIn` instance to
  68. which data can be written. Any keyword arguments will be
  69. passed through to :meth:`~gridfs.grid_file.GridIn`.
  70. If the ``"_id"`` of the file is manually specified, it must
  71. not already exist in GridFS. Otherwise
  72. :class:`~gridfs.errors.FileExists` is raised.
  73. :Parameters:
  74. - `**kwargs` (optional): keyword arguments for file creation
  75. """
  76. return GridIn(
  77. self.__collection, disable_md5=self.__disable_md5, **kwargs)
  78. def put(self, data, **kwargs):
  79. """Put data in GridFS as a new file.
  80. Equivalent to doing::
  81. try:
  82. f = new_file(**kwargs)
  83. f.write(data)
  84. finally:
  85. f.close()
  86. `data` can be either an instance of :class:`str` (:class:`bytes`
  87. in python 3) or a file-like object providing a :meth:`read` method.
  88. If an `encoding` keyword argument is passed, `data` can also be a
  89. :class:`unicode` (:class:`str` in python 3) instance, which will
  90. be encoded as `encoding` before being written. Any keyword arguments
  91. will be passed through to the created file - see
  92. :meth:`~gridfs.grid_file.GridIn` for possible arguments. Returns the
  93. ``"_id"`` of the created file.
  94. If the ``"_id"`` of the file is manually specified, it must
  95. not already exist in GridFS. Otherwise
  96. :class:`~gridfs.errors.FileExists` is raised.
  97. :Parameters:
  98. - `data`: data to be written as a file.
  99. - `**kwargs` (optional): keyword arguments for file creation
  100. .. versionchanged:: 3.0
  101. w=0 writes to GridFS are now prohibited.
  102. """
  103. grid_file = GridIn(
  104. self.__collection, disable_md5=self.__disable_md5, **kwargs)
  105. try:
  106. grid_file.write(data)
  107. finally:
  108. grid_file.close()
  109. return grid_file._id
  110. def get(self, file_id, session=None):
  111. """Get a file from GridFS by ``"_id"``.
  112. Returns an instance of :class:`~gridfs.grid_file.GridOut`,
  113. which provides a file-like interface for reading.
  114. :Parameters:
  115. - `file_id`: ``"_id"`` of the file to get
  116. - `session` (optional): a
  117. :class:`~pymongo.client_session.ClientSession`
  118. .. versionchanged:: 3.6
  119. Added ``session`` parameter.
  120. """
  121. gout = GridOut(self.__collection, file_id, session=session)
  122. # Raise NoFile now, instead of on first attribute access.
  123. gout._ensure_file()
  124. return gout
  125. def get_version(self, filename=None, version=-1, session=None, **kwargs):
  126. """Get a file from GridFS by ``"filename"`` or metadata fields.
  127. Returns a version of the file in GridFS whose filename matches
  128. `filename` and whose metadata fields match the supplied keyword
  129. arguments, as an instance of :class:`~gridfs.grid_file.GridOut`.
  130. Version numbering is a convenience atop the GridFS API provided
  131. by MongoDB. If more than one file matches the query (either by
  132. `filename` alone, by metadata fields, or by a combination of
  133. both), then version ``-1`` will be the most recently uploaded
  134. matching file, ``-2`` the second most recently
  135. uploaded, etc. Version ``0`` will be the first version
  136. uploaded, ``1`` the second version, etc. So if three versions
  137. have been uploaded, then version ``0`` is the same as version
  138. ``-3``, version ``1`` is the same as version ``-2``, and
  139. version ``2`` is the same as version ``-1``.
  140. Raises :class:`~gridfs.errors.NoFile` if no such version of
  141. that file exists.
  142. :Parameters:
  143. - `filename`: ``"filename"`` of the file to get, or `None`
  144. - `version` (optional): version of the file to get (defaults
  145. to -1, the most recent version uploaded)
  146. - `session` (optional): a
  147. :class:`~pymongo.client_session.ClientSession`
  148. - `**kwargs` (optional): find files by custom metadata.
  149. .. versionchanged:: 3.6
  150. Added ``session`` parameter.
  151. .. versionchanged:: 3.1
  152. ``get_version`` no longer ensures indexes.
  153. """
  154. query = kwargs
  155. if filename is not None:
  156. query["filename"] = filename
  157. _disallow_transactions(session)
  158. cursor = self.__files.find(query, session=session)
  159. if version < 0:
  160. skip = abs(version) - 1
  161. cursor.limit(-1).skip(skip).sort("uploadDate", DESCENDING)
  162. else:
  163. cursor.limit(-1).skip(version).sort("uploadDate", ASCENDING)
  164. try:
  165. doc = next(cursor)
  166. return GridOut(
  167. self.__collection, file_document=doc, session=session)
  168. except StopIteration:
  169. raise NoFile("no version %d for filename %r" % (version, filename))
  170. def get_last_version(self, filename=None, session=None, **kwargs):
  171. """Get the most recent version of a file in GridFS by ``"filename"``
  172. or metadata fields.
  173. Equivalent to calling :meth:`get_version` with the default
  174. `version` (``-1``).
  175. :Parameters:
  176. - `filename`: ``"filename"`` of the file to get, or `None`
  177. - `session` (optional): a
  178. :class:`~pymongo.client_session.ClientSession`
  179. - `**kwargs` (optional): find files by custom metadata.
  180. .. versionchanged:: 3.6
  181. Added ``session`` parameter.
  182. """
  183. return self.get_version(filename=filename, session=session, **kwargs)
  184. # TODO add optional safe mode for chunk removal?
  185. def delete(self, file_id, session=None):
  186. """Delete a file from GridFS by ``"_id"``.
  187. Deletes all data belonging to the file with ``"_id"``:
  188. `file_id`.
  189. .. warning:: Any processes/threads reading from the file while
  190. this method is executing will likely see an invalid/corrupt
  191. file. Care should be taken to avoid concurrent reads to a file
  192. while it is being deleted.
  193. .. note:: Deletes of non-existent files are considered successful
  194. since the end result is the same: no file with that _id remains.
  195. :Parameters:
  196. - `file_id`: ``"_id"`` of the file to delete
  197. - `session` (optional): a
  198. :class:`~pymongo.client_session.ClientSession`
  199. .. versionchanged:: 3.6
  200. Added ``session`` parameter.
  201. .. versionchanged:: 3.1
  202. ``delete`` no longer ensures indexes.
  203. """
  204. _disallow_transactions(session)
  205. self.__files.delete_one({"_id": file_id}, session=session)
  206. self.__chunks.delete_many({"files_id": file_id}, session=session)
  207. def list(self, session=None):
  208. """List the names of all files stored in this instance of
  209. :class:`GridFS`.
  210. :Parameters:
  211. - `session` (optional): a
  212. :class:`~pymongo.client_session.ClientSession`
  213. .. versionchanged:: 3.6
  214. Added ``session`` parameter.
  215. .. versionchanged:: 3.1
  216. ``list`` no longer ensures indexes.
  217. """
  218. _disallow_transactions(session)
  219. # With an index, distinct includes documents with no filename
  220. # as None.
  221. return [
  222. name for name in self.__files.distinct("filename", session=session)
  223. if name is not None]
  224. def find_one(self, filter=None, session=None, *args, **kwargs):
  225. """Get a single file from gridfs.
  226. All arguments to :meth:`find` are also valid arguments for
  227. :meth:`find_one`, although any `limit` argument will be
  228. ignored. Returns a single :class:`~gridfs.grid_file.GridOut`,
  229. or ``None`` if no matching file is found. For example::
  230. file = fs.find_one({"filename": "lisa.txt"})
  231. :Parameters:
  232. - `filter` (optional): a dictionary specifying
  233. the query to be performing OR any other type to be used as
  234. the value for a query for ``"_id"`` in the file collection.
  235. - `*args` (optional): any additional positional arguments are
  236. the same as the arguments to :meth:`find`.
  237. - `session` (optional): a
  238. :class:`~pymongo.client_session.ClientSession`
  239. - `**kwargs` (optional): any additional keyword arguments
  240. are the same as the arguments to :meth:`find`.
  241. .. versionchanged:: 3.6
  242. Added ``session`` parameter.
  243. """
  244. if filter is not None and not isinstance(filter, abc.Mapping):
  245. filter = {"_id": filter}
  246. _disallow_transactions(session)
  247. for f in self.find(filter, *args, session=session, **kwargs):
  248. return f
  249. return None
  250. def find(self, *args, **kwargs):
  251. """Query GridFS for files.
  252. Returns a cursor that iterates across files matching
  253. arbitrary queries on the files collection. Can be combined
  254. with other modifiers for additional control. For example::
  255. for grid_out in fs.find({"filename": "lisa.txt"},
  256. no_cursor_timeout=True):
  257. data = grid_out.read()
  258. would iterate through all versions of "lisa.txt" stored in GridFS.
  259. Note that setting no_cursor_timeout to True may be important to
  260. prevent the cursor from timing out during long multi-file processing
  261. work.
  262. As another example, the call::
  263. most_recent_three = fs.find().sort("uploadDate", -1).limit(3)
  264. would return a cursor to the three most recently uploaded files
  265. in GridFS.
  266. Follows a similar interface to
  267. :meth:`~pymongo.collection.Collection.find`
  268. in :class:`~pymongo.collection.Collection`.
  269. If a :class:`~pymongo.client_session.ClientSession` is passed to
  270. :meth:`find`, all returned :class:`~gridfs.grid_file.GridOut` instances
  271. are associated with that session.
  272. :Parameters:
  273. - `filter` (optional): a SON object specifying elements which
  274. must be present for a document to be included in the
  275. result set
  276. - `skip` (optional): the number of files to omit (from
  277. the start of the result set) when returning the results
  278. - `limit` (optional): the maximum number of results to
  279. return
  280. - `no_cursor_timeout` (optional): if False (the default), any
  281. returned cursor is closed by the server after 10 minutes of
  282. inactivity. If set to True, the returned cursor will never
  283. time out on the server. Care should be taken to ensure that
  284. cursors with no_cursor_timeout turned on are properly closed.
  285. - `sort` (optional): a list of (key, direction) pairs
  286. specifying the sort order for this query. See
  287. :meth:`~pymongo.cursor.Cursor.sort` for details.
  288. Raises :class:`TypeError` if any of the arguments are of
  289. improper type. Returns an instance of
  290. :class:`~gridfs.grid_file.GridOutCursor`
  291. corresponding to this query.
  292. .. versionchanged:: 3.0
  293. Removed the read_preference, tag_sets, and
  294. secondary_acceptable_latency_ms options.
  295. .. versionadded:: 2.7
  296. .. mongodoc:: find
  297. """
  298. return GridOutCursor(self.__collection, *args, **kwargs)
  299. def exists(self, document_or_id=None, session=None, **kwargs):
  300. """Check if a file exists in this instance of :class:`GridFS`.
  301. The file to check for can be specified by the value of its
  302. ``_id`` key, or by passing in a query document. A query
  303. document can be passed in as dictionary, or by using keyword
  304. arguments. Thus, the following three calls are equivalent:
  305. >>> fs.exists(file_id)
  306. >>> fs.exists({"_id": file_id})
  307. >>> fs.exists(_id=file_id)
  308. As are the following two calls:
  309. >>> fs.exists({"filename": "mike.txt"})
  310. >>> fs.exists(filename="mike.txt")
  311. And the following two:
  312. >>> fs.exists({"foo": {"$gt": 12}})
  313. >>> fs.exists(foo={"$gt": 12})
  314. Returns ``True`` if a matching file exists, ``False``
  315. otherwise. Calls to :meth:`exists` will not automatically
  316. create appropriate indexes; application developers should be
  317. sure to create indexes if needed and as appropriate.
  318. :Parameters:
  319. - `document_or_id` (optional): query document, or _id of the
  320. document to check for
  321. - `session` (optional): a
  322. :class:`~pymongo.client_session.ClientSession`
  323. - `**kwargs` (optional): keyword arguments are used as a
  324. query document, if they're present.
  325. .. versionchanged:: 3.6
  326. Added ``session`` parameter.
  327. """
  328. _disallow_transactions(session)
  329. if kwargs:
  330. f = self.__files.find_one(kwargs, ["_id"], session=session)
  331. else:
  332. f = self.__files.find_one(document_or_id, ["_id"], session=session)
  333. return f is not None
  334. class GridFSBucket(object):
  335. """An instance of GridFS on top of a single Database."""
  336. def __init__(self, db, bucket_name="fs",
  337. chunk_size_bytes=DEFAULT_CHUNK_SIZE, write_concern=None,
  338. read_preference=None, disable_md5=False):
  339. """Create a new instance of :class:`GridFSBucket`.
  340. Raises :exc:`TypeError` if `database` is not an instance of
  341. :class:`~pymongo.database.Database`.
  342. Raises :exc:`~pymongo.errors.ConfigurationError` if `write_concern`
  343. is not acknowledged.
  344. :Parameters:
  345. - `database`: database to use.
  346. - `bucket_name` (optional): The name of the bucket. Defaults to 'fs'.
  347. - `chunk_size_bytes` (optional): The chunk size in bytes. Defaults
  348. to 255KB.
  349. - `write_concern` (optional): The
  350. :class:`~pymongo.write_concern.WriteConcern` to use. If ``None``
  351. (the default) db.write_concern is used.
  352. - `read_preference` (optional): The read preference to use. If
  353. ``None`` (the default) db.read_preference is used.
  354. - `disable_md5` (optional): When True, MD5 checksums will not be
  355. computed for uploaded files. Useful in environments where MD5
  356. cannot be used for regulatory or other reasons. Defaults to False.
  357. .. versionchanged:: 3.11
  358. Running a GridFS operation in a transaction now always raises an
  359. error. GridFSBucket does not support multi-document transactions.
  360. .. versionadded:: 3.1
  361. .. mongodoc:: gridfs
  362. """
  363. if not isinstance(db, Database):
  364. raise TypeError("database must be an instance of Database")
  365. db = _clear_entity_type_registry(db)
  366. wtc = write_concern if write_concern is not None else db.write_concern
  367. if not wtc.acknowledged:
  368. raise ConfigurationError('write concern must be acknowledged')
  369. self._bucket_name = bucket_name
  370. self._collection = db[bucket_name]
  371. self._disable_md5 = disable_md5
  372. self._chunks = self._collection.chunks.with_options(
  373. write_concern=write_concern,
  374. read_preference=read_preference)
  375. self._files = self._collection.files.with_options(
  376. write_concern=write_concern,
  377. read_preference=read_preference)
  378. self._chunk_size_bytes = chunk_size_bytes
  379. def open_upload_stream(self, filename, chunk_size_bytes=None,
  380. metadata=None, session=None):
  381. """Opens a Stream that the application can write the contents of the
  382. file to.
  383. The user must specify the filename, and can choose to add any
  384. additional information in the metadata field of the file document or
  385. modify the chunk size.
  386. For example::
  387. my_db = MongoClient().test
  388. fs = GridFSBucket(my_db)
  389. grid_in = fs.open_upload_stream(
  390. "test_file", chunk_size_bytes=4,
  391. metadata={"contentType": "text/plain"})
  392. grid_in.write("data I want to store!")
  393. grid_in.close() # uploaded on close
  394. Returns an instance of :class:`~gridfs.grid_file.GridIn`.
  395. Raises :exc:`~gridfs.errors.NoFile` if no such version of
  396. that file exists.
  397. Raises :exc:`~ValueError` if `filename` is not a string.
  398. :Parameters:
  399. - `filename`: The name of the file to upload.
  400. - `chunk_size_bytes` (options): The number of bytes per chunk of this
  401. file. Defaults to the chunk_size_bytes in :class:`GridFSBucket`.
  402. - `metadata` (optional): User data for the 'metadata' field of the
  403. files collection document. If not provided the metadata field will
  404. be omitted from the files collection document.
  405. - `session` (optional): a
  406. :class:`~pymongo.client_session.ClientSession`
  407. .. versionchanged:: 3.6
  408. Added ``session`` parameter.
  409. """
  410. validate_string("filename", filename)
  411. opts = {"filename": filename,
  412. "chunk_size": (chunk_size_bytes if chunk_size_bytes
  413. is not None else self._chunk_size_bytes)}
  414. if metadata is not None:
  415. opts["metadata"] = metadata
  416. return GridIn(
  417. self._collection,
  418. session=session,
  419. disable_md5=self._disable_md5,
  420. **opts)
  421. def open_upload_stream_with_id(
  422. self, file_id, filename, chunk_size_bytes=None, metadata=None,
  423. session=None):
  424. """Opens a Stream that the application can write the contents of the
  425. file to.
  426. The user must specify the file id and filename, and can choose to add
  427. any additional information in the metadata field of the file document
  428. or modify the chunk size.
  429. For example::
  430. my_db = MongoClient().test
  431. fs = GridFSBucket(my_db)
  432. grid_in = fs.open_upload_stream_with_id(
  433. ObjectId(),
  434. "test_file",
  435. chunk_size_bytes=4,
  436. metadata={"contentType": "text/plain"})
  437. grid_in.write("data I want to store!")
  438. grid_in.close() # uploaded on close
  439. Returns an instance of :class:`~gridfs.grid_file.GridIn`.
  440. Raises :exc:`~gridfs.errors.NoFile` if no such version of
  441. that file exists.
  442. Raises :exc:`~ValueError` if `filename` is not a string.
  443. :Parameters:
  444. - `file_id`: The id to use for this file. The id must not have
  445. already been used for another file.
  446. - `filename`: The name of the file to upload.
  447. - `chunk_size_bytes` (options): The number of bytes per chunk of this
  448. file. Defaults to the chunk_size_bytes in :class:`GridFSBucket`.
  449. - `metadata` (optional): User data for the 'metadata' field of the
  450. files collection document. If not provided the metadata field will
  451. be omitted from the files collection document.
  452. - `session` (optional): a
  453. :class:`~pymongo.client_session.ClientSession`
  454. .. versionchanged:: 3.6
  455. Added ``session`` parameter.
  456. """
  457. validate_string("filename", filename)
  458. opts = {"_id": file_id,
  459. "filename": filename,
  460. "chunk_size": (chunk_size_bytes if chunk_size_bytes
  461. is not None else self._chunk_size_bytes)}
  462. if metadata is not None:
  463. opts["metadata"] = metadata
  464. return GridIn(
  465. self._collection,
  466. session=session,
  467. disable_md5=self._disable_md5,
  468. **opts)
  469. def upload_from_stream(self, filename, source, chunk_size_bytes=None,
  470. metadata=None, session=None):
  471. """Uploads a user file to a GridFS bucket.
  472. Reads the contents of the user file from `source` and uploads
  473. it to the file `filename`. Source can be a string or file-like object.
  474. For example::
  475. my_db = MongoClient().test
  476. fs = GridFSBucket(my_db)
  477. file_id = fs.upload_from_stream(
  478. "test_file",
  479. "data I want to store!",
  480. chunk_size_bytes=4,
  481. metadata={"contentType": "text/plain"})
  482. Returns the _id of the uploaded file.
  483. Raises :exc:`~gridfs.errors.NoFile` if no such version of
  484. that file exists.
  485. Raises :exc:`~ValueError` if `filename` is not a string.
  486. :Parameters:
  487. - `filename`: The name of the file to upload.
  488. - `source`: The source stream of the content to be uploaded. Must be
  489. a file-like object that implements :meth:`read` or a string.
  490. - `chunk_size_bytes` (options): The number of bytes per chunk of this
  491. file. Defaults to the chunk_size_bytes of :class:`GridFSBucket`.
  492. - `metadata` (optional): User data for the 'metadata' field of the
  493. files collection document. If not provided the metadata field will
  494. be omitted from the files collection document.
  495. - `session` (optional): a
  496. :class:`~pymongo.client_session.ClientSession`
  497. .. versionchanged:: 3.6
  498. Added ``session`` parameter.
  499. """
  500. with self.open_upload_stream(
  501. filename, chunk_size_bytes, metadata, session=session) as gin:
  502. gin.write(source)
  503. return gin._id
  504. def upload_from_stream_with_id(self, file_id, filename, source,
  505. chunk_size_bytes=None, metadata=None,
  506. session=None):
  507. """Uploads a user file to a GridFS bucket with a custom file id.
  508. Reads the contents of the user file from `source` and uploads
  509. it to the file `filename`. Source can be a string or file-like object.
  510. For example::
  511. my_db = MongoClient().test
  512. fs = GridFSBucket(my_db)
  513. file_id = fs.upload_from_stream(
  514. ObjectId(),
  515. "test_file",
  516. "data I want to store!",
  517. chunk_size_bytes=4,
  518. metadata={"contentType": "text/plain"})
  519. Raises :exc:`~gridfs.errors.NoFile` if no such version of
  520. that file exists.
  521. Raises :exc:`~ValueError` if `filename` is not a string.
  522. :Parameters:
  523. - `file_id`: The id to use for this file. The id must not have
  524. already been used for another file.
  525. - `filename`: The name of the file to upload.
  526. - `source`: The source stream of the content to be uploaded. Must be
  527. a file-like object that implements :meth:`read` or a string.
  528. - `chunk_size_bytes` (options): The number of bytes per chunk of this
  529. file. Defaults to the chunk_size_bytes of :class:`GridFSBucket`.
  530. - `metadata` (optional): User data for the 'metadata' field of the
  531. files collection document. If not provided the metadata field will
  532. be omitted from the files collection document.
  533. - `session` (optional): a
  534. :class:`~pymongo.client_session.ClientSession`
  535. .. versionchanged:: 3.6
  536. Added ``session`` parameter.
  537. """
  538. with self.open_upload_stream_with_id(
  539. file_id, filename, chunk_size_bytes, metadata,
  540. session=session) as gin:
  541. gin.write(source)
  542. def open_download_stream(self, file_id, session=None):
  543. """Opens a Stream from which the application can read the contents of
  544. the stored file specified by file_id.
  545. For example::
  546. my_db = MongoClient().test
  547. fs = GridFSBucket(my_db)
  548. # get _id of file to read.
  549. file_id = fs.upload_from_stream("test_file", "data I want to store!")
  550. grid_out = fs.open_download_stream(file_id)
  551. contents = grid_out.read()
  552. Returns an instance of :class:`~gridfs.grid_file.GridOut`.
  553. Raises :exc:`~gridfs.errors.NoFile` if no file with file_id exists.
  554. :Parameters:
  555. - `file_id`: The _id of the file to be downloaded.
  556. - `session` (optional): a
  557. :class:`~pymongo.client_session.ClientSession`
  558. .. versionchanged:: 3.6
  559. Added ``session`` parameter.
  560. """
  561. gout = GridOut(self._collection, file_id, session=session)
  562. # Raise NoFile now, instead of on first attribute access.
  563. gout._ensure_file()
  564. return gout
  565. def download_to_stream(self, file_id, destination, session=None):
  566. """Downloads the contents of the stored file specified by file_id and
  567. writes the contents to `destination`.
  568. For example::
  569. my_db = MongoClient().test
  570. fs = GridFSBucket(my_db)
  571. # Get _id of file to read
  572. file_id = fs.upload_from_stream("test_file", "data I want to store!")
  573. # Get file to write to
  574. file = open('myfile','wb+')
  575. fs.download_to_stream(file_id, file)
  576. file.seek(0)
  577. contents = file.read()
  578. Raises :exc:`~gridfs.errors.NoFile` if no file with file_id exists.
  579. :Parameters:
  580. - `file_id`: The _id of the file to be downloaded.
  581. - `destination`: a file-like object implementing :meth:`write`.
  582. - `session` (optional): a
  583. :class:`~pymongo.client_session.ClientSession`
  584. .. versionchanged:: 3.6
  585. Added ``session`` parameter.
  586. """
  587. with self.open_download_stream(file_id, session=session) as gout:
  588. for chunk in gout:
  589. destination.write(chunk)
  590. def delete(self, file_id, session=None):
  591. """Given an file_id, delete this stored file's files collection document
  592. and associated chunks from a GridFS bucket.
  593. For example::
  594. my_db = MongoClient().test
  595. fs = GridFSBucket(my_db)
  596. # Get _id of file to delete
  597. file_id = fs.upload_from_stream("test_file", "data I want to store!")
  598. fs.delete(file_id)
  599. Raises :exc:`~gridfs.errors.NoFile` if no file with file_id exists.
  600. :Parameters:
  601. - `file_id`: The _id of the file to be deleted.
  602. - `session` (optional): a
  603. :class:`~pymongo.client_session.ClientSession`
  604. .. versionchanged:: 3.6
  605. Added ``session`` parameter.
  606. """
  607. _disallow_transactions(session)
  608. res = self._files.delete_one({"_id": file_id}, session=session)
  609. self._chunks.delete_many({"files_id": file_id}, session=session)
  610. if not res.deleted_count:
  611. raise NoFile(
  612. "no file could be deleted because none matched %s" % file_id)
  613. def find(self, *args, **kwargs):
  614. """Find and return the files collection documents that match ``filter``
  615. Returns a cursor that iterates across files matching
  616. arbitrary queries on the files collection. Can be combined
  617. with other modifiers for additional control.
  618. For example::
  619. for grid_data in fs.find({"filename": "lisa.txt"},
  620. no_cursor_timeout=True):
  621. data = grid_data.read()
  622. would iterate through all versions of "lisa.txt" stored in GridFS.
  623. Note that setting no_cursor_timeout to True may be important to
  624. prevent the cursor from timing out during long multi-file processing
  625. work.
  626. As another example, the call::
  627. most_recent_three = fs.find().sort("uploadDate", -1).limit(3)
  628. would return a cursor to the three most recently uploaded files
  629. in GridFS.
  630. Follows a similar interface to
  631. :meth:`~pymongo.collection.Collection.find`
  632. in :class:`~pymongo.collection.Collection`.
  633. If a :class:`~pymongo.client_session.ClientSession` is passed to
  634. :meth:`find`, all returned :class:`~gridfs.grid_file.GridOut` instances
  635. are associated with that session.
  636. :Parameters:
  637. - `filter`: Search query.
  638. - `batch_size` (optional): The number of documents to return per
  639. batch.
  640. - `limit` (optional): The maximum number of documents to return.
  641. - `no_cursor_timeout` (optional): The server normally times out idle
  642. cursors after an inactivity period (10 minutes) to prevent excess
  643. memory use. Set this option to True prevent that.
  644. - `skip` (optional): The number of documents to skip before
  645. returning.
  646. - `sort` (optional): The order by which to sort results. Defaults to
  647. None.
  648. """
  649. return GridOutCursor(self._collection, *args, **kwargs)
  650. def open_download_stream_by_name(self, filename, revision=-1, session=None):
  651. """Opens a Stream from which the application can read the contents of
  652. `filename` and optional `revision`.
  653. For example::
  654. my_db = MongoClient().test
  655. fs = GridFSBucket(my_db)
  656. grid_out = fs.open_download_stream_by_name("test_file")
  657. contents = grid_out.read()
  658. Returns an instance of :class:`~gridfs.grid_file.GridOut`.
  659. Raises :exc:`~gridfs.errors.NoFile` if no such version of
  660. that file exists.
  661. Raises :exc:`~ValueError` filename is not a string.
  662. :Parameters:
  663. - `filename`: The name of the file to read from.
  664. - `revision` (optional): Which revision (documents with the same
  665. filename and different uploadDate) of the file to retrieve.
  666. Defaults to -1 (the most recent revision).
  667. - `session` (optional): a
  668. :class:`~pymongo.client_session.ClientSession`
  669. :Note: Revision numbers are defined as follows:
  670. - 0 = the original stored file
  671. - 1 = the first revision
  672. - 2 = the second revision
  673. - etc...
  674. - -2 = the second most recent revision
  675. - -1 = the most recent revision
  676. .. versionchanged:: 3.6
  677. Added ``session`` parameter.
  678. """
  679. validate_string("filename", filename)
  680. query = {"filename": filename}
  681. _disallow_transactions(session)
  682. cursor = self._files.find(query, session=session)
  683. if revision < 0:
  684. skip = abs(revision) - 1
  685. cursor.limit(-1).skip(skip).sort("uploadDate", DESCENDING)
  686. else:
  687. cursor.limit(-1).skip(revision).sort("uploadDate", ASCENDING)
  688. try:
  689. grid_file = next(cursor)
  690. return GridOut(
  691. self._collection, file_document=grid_file, session=session)
  692. except StopIteration:
  693. raise NoFile(
  694. "no version %d for filename %r" % (revision, filename))
  695. def download_to_stream_by_name(self, filename, destination, revision=-1,
  696. session=None):
  697. """Write the contents of `filename` (with optional `revision`) to
  698. `destination`.
  699. For example::
  700. my_db = MongoClient().test
  701. fs = GridFSBucket(my_db)
  702. # Get file to write to
  703. file = open('myfile','wb')
  704. fs.download_to_stream_by_name("test_file", file)
  705. Raises :exc:`~gridfs.errors.NoFile` if no such version of
  706. that file exists.
  707. Raises :exc:`~ValueError` if `filename` is not a string.
  708. :Parameters:
  709. - `filename`: The name of the file to read from.
  710. - `destination`: A file-like object that implements :meth:`write`.
  711. - `revision` (optional): Which revision (documents with the same
  712. filename and different uploadDate) of the file to retrieve.
  713. Defaults to -1 (the most recent revision).
  714. - `session` (optional): a
  715. :class:`~pymongo.client_session.ClientSession`
  716. :Note: Revision numbers are defined as follows:
  717. - 0 = the original stored file
  718. - 1 = the first revision
  719. - 2 = the second revision
  720. - etc...
  721. - -2 = the second most recent revision
  722. - -1 = the most recent revision
  723. .. versionchanged:: 3.6
  724. Added ``session`` parameter.
  725. """
  726. with self.open_download_stream_by_name(
  727. filename, revision, session=session) as gout:
  728. for chunk in gout:
  729. destination.write(chunk)
  730. def rename(self, file_id, new_filename, session=None):
  731. """Renames the stored file with the specified file_id.
  732. For example::
  733. my_db = MongoClient().test
  734. fs = GridFSBucket(my_db)
  735. # Get _id of file to rename
  736. file_id = fs.upload_from_stream("test_file", "data I want to store!")
  737. fs.rename(file_id, "new_test_name")
  738. Raises :exc:`~gridfs.errors.NoFile` if no file with file_id exists.
  739. :Parameters:
  740. - `file_id`: The _id of the file to be renamed.
  741. - `new_filename`: The new name of the file.
  742. - `session` (optional): a
  743. :class:`~pymongo.client_session.ClientSession`
  744. .. versionchanged:: 3.6
  745. Added ``session`` parameter.
  746. """
  747. _disallow_transactions(session)
  748. result = self._files.update_one({"_id": file_id},
  749. {"$set": {"filename": new_filename}},
  750. session=session)
  751. if not result.matched_count:
  752. raise NoFile("no files could be renamed %r because none "
  753. "matched file_id %i" % (new_filename, file_id))