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.
Create a new instance of GridFS.
Raises TypeError if database is not an instance of Database.
Parameters: |
|
---|
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_concern
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: |
|
---|
Changed in version 3.1: delete no longer ensures indexes.
Check if a file exists in this instance of 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 exists() will not automatically create appropriate indexes; application developers should be sure to create indexes if needed and as appropriate.
Parameters: |
|
---|
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() in Collection.
Parameters: |
|
---|
Raises TypeError if any of the arguments are of improper type. Returns an instance of GridOutCursor corresponding to this query.
Changed in version 3.0: Removed the read_preference, tag_sets, and secondary_acceptable_latency_ms options.
New in version 2.7.
Get a single file from gridfs.
All arguments to find() are also valid arguments for find_one(), although any limit argument will be ignored. Returns a single GridOut, or None if no matching file is found. For example:
file = fs.find_one({"filename": "lisa.txt"})
Parameters: |
|
---|
Get a file from GridFS by "_id".
Returns an instance of GridOut, which provides a file-like interface for reading.
Parameters: |
|
---|
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: |
|
---|
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 -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 NoFile if no such version of that file exists.
Parameters: |
|
---|
Changed in version 3.1: get_version no longer ensures indexes.
List the names of all files stored in this instance of GridFS.
Changed in version 3.1: list no longer ensures indexes.
Create a new file in GridFS.
Returns a new GridIn instance to which data can be written. Any keyword arguments will be passed through to GridIn().
If the "_id" of the file is manually specified, it must not already exist in GridFS. Otherwise FileExists is raised.
Parameters: |
|
---|
Put data in GridFS as a new file.
Equivalent to doing:
try:
f = new_file(**kwargs)
f.write(data)
finally:
f.close()
data can be either an instance of str (bytes in python 3) or a file-like object providing a read() method. If an encoding keyword argument is passed, data can also be a unicode (str in python 3) instance, which will be encoded as encoding before being written. Any keyword arguments will be passed through to the created file - see 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 FileExists is raised.
Parameters: |
|
---|
Changed in version 3.0: w=0 writes to GridFS are now prohibited.
Create a new instance of GridFSBucket.
Raises TypeError if database is not an instance of Database.
Raises ConfigurationError if write_concern is not acknowledged.
Parameters: |
|
---|
New in version 3.1.
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 NoFile if no file with file_id exists.
Parameters: |
|
---|
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 NoFile if no file with file_id exists.
Parameters: |
|
---|
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 NoFile if no such version of that file exists.
Raises ValueError if filename is not a string.
Parameters: |
|
---|---|
Note: | Revision numbers are defined as follows:
|
Find 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 find() in Collection.
Parameters: |
|
---|
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 NoFile if no file with file_id exists.
Parameters: |
|
---|
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 NoFile if no such version of that file exists.
Raises ValueError filename is not a string.
Parameters: |
|
---|---|
Note: | Revision numbers are defined as follows:
|
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)
grid_in, file_id = fs.open_upload_stream(
"test_file", chunk_size_bytes=4,
metadata={"contentType": "text/plain"})
grid_in.write("data I want to store!")
grid_in.close() # uploaded on close
Returns an instance of GridIn.
Raises NoFile if no such version of that file exists. Raises ValueError if filename is not a string.
Parameters: |
|
---|
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)
grid_in, file_id = fs.open_upload_stream(
ObjectId(),
"test_file",
chunk_size_bytes=4,
metadata={"contentType": "text/plain"})
grid_in.write("data I want to store!")
grid_in.close() # uploaded on close
Returns an instance of GridIn.
Raises NoFile if no such version of that file exists. Raises ValueError if filename is not a string.
Parameters: |
|
---|
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 NoFile if no file with file_id exists.
Parameters: |
|
---|
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 NoFile if no such version of that file exists. Raises ValueError if filename is not a string.
Parameters: |
|
---|
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 NoFile if no such version of that file exists. Raises ValueError if filename is not a string.
Parameters: |
|
---|
Sub-modules: