Store blobs of data in GridFS.
See also
An instance of GridFS on top of a single Database. You must call open() before using the MotorGridFS object.
Parameters : |
|
---|
See general MongoDB documentation
Delete a file from GridFS by "_id".
Removes 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 : |
|
---|
If a callback is passed, returns None, else returns a Future.
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 : |
|
---|
If a callback is passed, returns None, else returns a Future.
Get a file from GridFS by "_id".
Returns an instance of GridOut, which provides a file-like interface for reading.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
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 : |
|
---|
If a callback is passed, returns None, else returns a Future.
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.
An index on {filename: 1, uploadDate: -1} will automatically be created when this method is called the first time.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
List the names of all files stored in this instance of GridFS.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
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 : |
|
---|
If a callback is passed, returns None, else returns a Future.
Actually initialize. Takes an optional callback, or returns a Future that resolves to self when opened.
Parameters : |
|
---|
Put data into GridFS as a new file.
Equivalent to doing:
@gen.coroutine
def f(data, **kwargs):
try:
f = yield my_gridfs.new_file(**kwargs)
yield f.write(data)
finally:
yield 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 MotorGridIn() 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 : |
|
---|
If a callback is passed, returns None, else returns a Future.
Class to write data to GridFS. If instantiating directly, you must call open() before using the MotorGridIn object. However, application developers should not generally need to instantiate this class - see new_file().
Any of the file level options specified in the GridFS Spec may be passed as keyword arguments. Any additional keyword arguments will be set as additional fields on the file document. Valid keyword arguments include:
- "_id": unique ID for this file (default: ObjectId) - this "_id" must not have already been used for another file
- "filename": human name for the file
- "contentType" or "content_type": valid mime-type for the file
- "chunkSize" or "chunk_size": size of each of the chunks, in bytes (default: 256 kb)
- "encoding": encoding used for this file. In Python 2, any unicode that is written to the file will be converted to a str. In Python 3, any str that is written to the file will be converted to bytes.
Parameters : |
|
---|
The '_id' value for this file.
This attribute is read-only.
Set an arbitrary metadata attribute on the file. Stores value on the server as a key-value pair within the file document once the file is closed. If the file is already closed, calling set will immediately update the file document on the server.
Metadata set on the file appears as attributes on a MotorGridOut object created from the file.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
Flush the file and close it.
A closed file cannot be written any more. Calling close() more than once is allowed.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
Actually initialize. Takes an optional callback, or returns a Future that resolves to self when opened.
Parameters : |
|
---|
Set an arbitrary metadata attribute on the file. Stores value on the server as a key-value pair within the file document once the file is closed. If the file is already closed, calling set will immediately update the file document on the server.
Metadata set on the file appears as attributes on a MotorGridOut object created from the file.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
Write data to the file. There is no return value.
data can be either a string of bytes or a file-like object (implementing read()). If the file has an encoding attribute, data can also be a unicode (str in python 3) instance, which will be encoded as encoding before being written.
Due to buffering, the data may not actually be written to the database until the close() method is called. Raises ValueError if this file is already closed. Raises TypeError if data is not an instance of str (bytes in python 3), a file-like object, or an instance of unicode (str in python 3). Unicode data is only allowed if the file has an encoding attribute.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
Write a sequence of strings to the file.
Does not add seperators.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
Chunk size for this file.
This attribute is read-only.
Is this file closed?
Mime-type for this file.
Name of this file.
Length (in bytes) of this file.
This attribute is read-only and can only be read after close() has been called.
MD5 of the contents of this file (generated on the server).
This attribute is read-only and can only be read after close() has been called.
Alias for filename.
Class to read data out of GridFS.
Application developers should generally not need to instantiate this class directly - instead see the methods provided by MotorGridFS.
The '_id' value for this file.
This attribute is read-only.
Actually initialize. Takes an optional callback, or returns a Future that resolves to self when opened.
Parameters : |
|
---|
Read at most size bytes from the file (less if there isn’t enough data).
The bytes are returned as an instance of str (bytes in python 3). If size is negative or omitted all data is read.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
Read one line or up to size bytes from the file.
Parameters : |
|
---|
If a callback is passed, returns None, else returns a Future.
Set the current position of this file.
Parameters : |
|
---|
Write the contents of this file to a tornado.web.RequestHandler. This method will call flush on the RequestHandler, so ensure all headers have already been set. For a more complete example see the implementation of GridFSHandler.
Returns a Future.
class FileHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@gen.coroutine
def get(self, filename):
db = self.settings['db']
fs = yield motor.MotorGridFS(db()).open()
try:
gridout = yield fs.get_last_version(filename)
except gridfs.NoFile:
raise tornado.web.HTTPError(404)
self.set_header("Content-Type", gridout.content_type)
self.set_header("Content-Length", gridout.length)
yield gridout.stream_to_handler(self)
self.finish()
See also
Tornado RequestHandler
Return the current position of this file.
List of aliases for this file.
This attribute is read-only.
Chunk size for this file.
This attribute is read-only.
Mime-type for this file.
This attribute is read-only.
Name of this file.
This attribute is read-only.
Length (in bytes) of this file.
This attribute is read-only.
MD5 of the contents of this file (generated on the server).
This attribute is read-only.
Metadata attached to this file.
This attribute is read-only.
Alias for filename.
This attribute is read-only.
Date that this file was first uploaded.
This attribute is read-only.