gridfs async – Async tools for working with GridFS¶
GridFS is a specification for storing large objects in Mongo.
The gridfs package is an implementation of GridFS on top of
pymongo, exposing a file-like interface.
See also
The MongoDB documentation on gridfs.
- class gridfs.asynchronous.AsyncGridFS(database, collection='fs')¶
Create a new instance of
GridFS.Raises
TypeErrorif database is not an instance ofDatabase.- Parameters:
database (AsyncDatabase[Any]) – database to use
collection (str) – root collection to use
Changed in version 4.0: Removed the disable_md5 parameter. See disable_md5 parameter is removed for details.
Changed in version 3.11: Running a GridFS operation in a transaction now always raises an error. GridFS does not support multi-document transactions.
Changed in version 3.7: Added the disable_md5 parameter.
Changed in version 3.1: Indexes are only ensured on the first write to the DB.
Changed in version 3.0: database must use an acknowledged
write_concernSee also
The MongoDB documentation on gridfs.
- async delete(file_id, session=None)¶
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 (Any) –
"_id"of the file to deletesession (AsyncClientSession | None) – a
AsyncClientSession
- Return type:
None
Changed in version 3.6: Added
sessionparameter.Changed in version 3.1:
deleteno longer ensures indexes.
- async exists(document_or_id=None, session=None, **kwargs)¶
Check if a file exists in this instance of
GridFS.The file to check for can be specified by the value of its
_idkey, 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
Trueif a matching file exists,Falseotherwise. Calls toexists()will not automatically create appropriate indexes; application developers should be sure to create indexes if needed and as appropriate.- Parameters:
document_or_id (Any | None) – query document, or _id of the document to check for
session (AsyncClientSession | None) – a
AsyncClientSessionkwargs (Any) – keyword arguments are used as a query document, if they’re present.
- Return type:
bool
Changed in version 3.6: Added
sessionparameter.
- find(*args, **kwargs)¶
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
find()inCollection.If a
AsyncClientSessionis passed tofind(), all returnedGridOutinstances are associated with that session.- Parameters:
filter – A query document that selects which files to include in the result set. Can be an empty document to include all files.
skip – the number of files to omit (from the start of the result set) when returning the results
limit – the maximum number of results to return
no_cursor_timeout – 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 – a list of (key, direction) pairs specifying the sort order for this query. See
sort()for details.args (Any)
kwargs (Any)
- Return type:
Raises
TypeErrorif any of the arguments are of improper type. Returns an instance ofGridOutCursorcorresponding to this query.Changed in version 3.0: Removed the read_preference, tag_sets, and secondary_acceptable_latency_ms options.
Added in version 2.7.
See also
The MongoDB documentation on find.
- async find_one(filter=None, session=None, *args, **kwargs)¶
Get a single file from gridfs.
All arguments to
find()are also valid arguments forfind_one(), although any limit argument will be ignored. Returns a singleGridOut, orNoneif no matching file is found. For example:- Parameters:
filter (Any | None) – 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 (Any) – any additional positional arguments are the same as the arguments to
find().session (AsyncClientSession | None) – a
AsyncClientSessionkwargs (Any) – any additional keyword arguments are the same as the arguments to
find().
- Return type:
AsyncGridOut | None
Changed in version 3.6: Added
sessionparameter.
- async get(file_id, session=None)¶
Get a file from GridFS by
"_id".Returns an instance of
GridOut, which provides a file-like interface for reading.- Parameters:
file_id (Any) –
"_id"of the file to getsession (AsyncClientSession | None) – a
AsyncClientSession
- Return type:
Changed in version 3.6: Added
sessionparameter.
- async get_last_version(filename=None, session=None, **kwargs)¶
Get the most recent version of a file in GridFS by
"filename"or metadata fields.Equivalent to calling
get_version()with the default version (-1).- Parameters:
filename (str | None) –
"filename"of the file to get, or Nonesession (AsyncClientSession | None) – a
AsyncClientSessionkwargs (Any) – find files by custom metadata.
- Return type:
Changed in version 3.6: Added
sessionparameter.
- async get_version(filename=None, version=-1, session=None, **kwargs)¶
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
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
-1will be the most recently uploaded matching file,-2the second most recently uploaded, etc. Version0will be the first version uploaded,1the second version, etc. So if three versions have been uploaded, then version0is the same as version-3, version1is the same as version-2, and version2is the same as version-1.Raises
NoFileif no such version of that file exists.- Parameters:
filename (str | None) –
"filename"of the file to get, or Noneversion (int | None) – version of the file to get (defaults to -1, the most recent version uploaded)
session (AsyncClientSession | None) – a
AsyncClientSessionkwargs (Any) – find files by custom metadata.
- Return type:
Changed in version 3.6: Added
sessionparameter.Changed in version 3.1:
get_versionno longer ensures indexes.
- async list(session=None)¶
List the names of all files stored in this instance of
GridFS.- Parameters:
session (AsyncClientSession | None) – a
AsyncClientSession- Return type:
list[str]
Changed in version 3.6: Added
sessionparameter.Changed in version 3.1:
listno longer ensures indexes.
- new_file(**kwargs)¶
Create a new file in GridFS.
Returns a new
GridIninstance to which data can be written. Any keyword arguments will be passed through toGridIn().If the
"_id"of the file is manually specified, it must not already exist in GridFS. OtherwiseFileExistsis raised.- Parameters:
kwargs (Any) – keyword arguments for file creation
- Return type:
- async put(data, **kwargs)¶
Put 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
bytesor a file-like object providing aread()method. If an encoding keyword argument is passed, data can also be astrinstance, which will be encoded as encoding before being written. Any keyword arguments will be passed through to the created file - seeGridIn()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. OtherwiseFileExistsis raised.- Parameters:
data (Any) – data to be written as a file.
kwargs (Any) – keyword arguments for file creation
- Return type:
Any
Changed in version 3.0: w=0 writes to GridFS are now prohibited.
- class gridfs.asynchronous.AsyncGridFSBucket(db, bucket_name='fs', chunk_size_bytes=261120, write_concern=None, read_preference=None)¶
Create a new instance of
GridFSBucket.Raises
TypeErrorif database is not an instance ofDatabase.Raises
ConfigurationErrorif write_concern is not acknowledged.- Parameters:
database – database to use.
bucket_name (str) – The name of the bucket. Defaults to ‘fs’.
chunk_size_bytes (int) – The chunk size in bytes. Defaults to 255KB.
write_concern (Optional[WriteConcern]) – The
WriteConcernto use. IfNone(the default) db.write_concern is used.read_preference (Optional[_ServerMode]) – The read preference to use. If
None(the default) db.read_preference is used.db (AsyncDatabase[Any])
Changed in version 4.0: Removed the disable_md5 parameter. See disable_md5 parameter is removed for details.
Changed in version 3.11: Running a GridFSBucket operation in a transaction now always raises an error. GridFSBucket does not support multi-document transactions.
Changed in version 3.7: Added the disable_md5 parameter.
Added in version 3.1.
See also
The MongoDB documentation on gridfs.
- async delete(file_id, session=None)¶
Given 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
NoFileif no file with file_id exists.- Parameters:
file_id (Any) – The _id of the file to be deleted.
session (AsyncClientSession | None) – a
AsyncClientSession
- Return type:
None
Changed in version 3.6: Added
sessionparameter.
- async delete_by_name(filename, session=None)¶
Given a filename, delete this stored file’s files collection document(s) and associated chunks from a GridFS bucket.
For example:
my_db = AsyncMongoClient().test fs = AsyncGridFSBucket(my_db) await fs.upload_from_stream("test_file", "data I want to store!") await fs.delete_by_name("test_file")
Raises
NoFileif no file with the given filename exists.- Parameters:
filename (str) – The name of the file to be deleted.
session (AsyncClientSession | None) – a
AsyncClientSession
- Return type:
None
Added in version 4.12.
- async download_to_stream(file_id, destination, session=None)¶
Downloads 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
NoFileif no file with file_id exists.- Parameters:
file_id (Any) – The _id of the file to be downloaded.
destination (Any) – a file-like object implementing
write().session (AsyncClientSession | None) – a
AsyncClientSession
- Return type:
None
Changed in version 3.6: Added
sessionparameter.
- async download_to_stream_by_name(filename, destination, revision=-1, session=None)¶
Write 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
NoFileif no such version of that file exists.Raises
ValueErrorif filename is not a string.- Parameters:
filename (str) – The name of the file to read from.
destination (Any) – A file-like object that implements
write().revision (int) – Which revision (documents with the same filename and different uploadDate) of the file to retrieve. Defaults to -1 (the most recent revision).
session (AsyncClientSession | None) – a
AsyncClientSession
- 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
- Return type:
None
Changed in version 3.6: Added
sessionparameter.
- find(*args, **kwargs)¶
Find and return the files collection documents that match
filterReturns 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
find()inCollection.If a
AsyncClientSessionis passed tofind(), all returnedGridOutinstances are associated with that session.- Parameters:
filter – Search query.
batch_size – The number of documents to return per batch.
limit – The maximum number of documents to return.
no_cursor_timeout – 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 – The number of documents to skip before returning.
sort – The order by which to sort results. Defaults to None.
args (Any)
kwargs (Any)
- Return type:
- async open_download_stream(file_id, session=None)¶
Opens 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
GridOut.Raises
NoFileif no file with file_id exists.- Parameters:
file_id (Any) – The _id of the file to be downloaded.
session (AsyncClientSession | None) – a
AsyncClientSession
- Return type:
Changed in version 3.6: Added
sessionparameter.
- async open_download_stream_by_name(filename, revision=-1, session=None)¶
Opens 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
GridOut.Raises
NoFileif no such version of that file exists.Raises
ValueErrorfilename is not a string.- Parameters:
filename (str) – The name of the file to read from.
revision (int) – Which revision (documents with the same filename and different uploadDate) of the file to retrieve. Defaults to -1 (the most recent revision).
session (AsyncClientSession | None) – a
AsyncClientSession
- 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
- Return type:
Changed in version 3.6: Added
sessionparameter.
- open_upload_stream(filename, chunk_size_bytes=None, metadata=None, session=None)¶
Opens 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
GridIn.Raises
NoFileif no such version of that file exists. RaisesValueErrorif filename is not a string.- Parameters:
filename (str) – The name of the file to upload.
(options) (chunk_size_bytes`) – The number of bytes per chunk of this file. Defaults to the chunk_size_bytes in
GridFSBucket.metadata (Mapping[str, Any] | None) – 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 (AsyncClientSession | None) – a
AsyncClientSessionchunk_size_bytes (int | None)
- Return type:
Changed in version 3.6: Added
sessionparameter.
- open_upload_stream_with_id(file_id, filename, chunk_size_bytes=None, metadata=None, session=None)¶
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
GridIn.Raises
NoFileif no such version of that file exists. RaisesValueErrorif filename is not a string.- Parameters:
file_id (Any) – The id to use for this file. The id must not have already been used for another file.
filename (str) – The name of the file to upload.
(options) (chunk_size_bytes`) – The number of bytes per chunk of this file. Defaults to the chunk_size_bytes in
GridFSBucket.metadata (Mapping[str, Any] | None) – 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 (AsyncClientSession | None) – a
AsyncClientSessionchunk_size_bytes (int | None)
- Return type:
Changed in version 3.6: Added
sessionparameter.
- async rename(file_id, new_filename, session=None)¶
Renames 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
NoFileif no file with file_id exists.- Parameters:
file_id (Any) – The _id of the file to be renamed.
new_filename (str) – The new name of the file.
session (AsyncClientSession | None) – a
AsyncClientSession
- Return type:
None
Changed in version 3.6: Added
sessionparameter.
- async rename_by_name(filename, new_filename, session=None)¶
Renames the stored file with the specified filename.
For example:
my_db = AsyncMongoClient().test fs = AsyncGridFSBucket(my_db) await fs.upload_from_stream("test_file", "data I want to store!") await fs.rename_by_name("test_file", "new_test_name")
Raises
NoFileif no file with the given filename exists.- Parameters:
filename (str) – The filename of the file to be renamed.
new_filename (str) – The new name of the file.
session (AsyncClientSession | None) – a
AsyncClientSession
- Return type:
None
Added in version 4.12.
- async upload_from_stream(filename, source, chunk_size_bytes=None, metadata=None, session=None)¶
Uploads 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
NoFileif no such version of that file exists. RaisesValueErrorif filename is not a string.- Parameters:
filename (str) – The name of the file to upload.
source (Any) – The source stream of the content to be uploaded. Must be a file-like object that implements
read()or a string.(options) (chunk_size_bytes`) – The number of bytes per chunk of this file. Defaults to the chunk_size_bytes of
GridFSBucket.metadata (Mapping[str, Any] | None) – 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 (AsyncClientSession | None) – a
AsyncClientSessionchunk_size_bytes (int | None)
- Return type:
Changed in version 3.6: Added
sessionparameter.
- async upload_from_stream_with_id(file_id, filename, source, chunk_size_bytes=None, metadata=None, session=None)¶
Uploads 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
NoFileif no such version of that file exists. RaisesValueErrorif filename is not a string.- Parameters:
file_id (Any) – The id to use for this file. The id must not have already been used for another file.
filename (str) – The name of the file to upload.
source (Any) – The source stream of the content to be uploaded. Must be a file-like object that implements
read()or a string.(options) (chunk_size_bytes`) – The number of bytes per chunk of this file. Defaults to the chunk_size_bytes of
GridFSBucket.metadata (Mapping[str, Any] | None) – 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 (AsyncClientSession | None) – a
AsyncClientSessionchunk_size_bytes (int | None)
- Return type:
None
Changed in version 3.6: Added
sessionparameter.
Sub-modules: