Tools for representing files stored in GridFS.
Open a “file” in GridFS.
Application developers should generally not need to instantiate this class directly - instead see the open() method.
Only a single opened GridFile instance may exist for a file in gridfs at any time. Care must be taken to close GridFile instances when done using them. GridFile instances support the context manager protocol (the “with” statement).
Raises TypeError if file_spec is not an instance of dict, database is not an instance of Database, or collection is not an instance of basestring.
The file_spec argument must be a query specifying the file to open. The first file matching the query will be opened. If no such files exist, a new file is created using the metadata in file_spec. The valid fields in a file_spec are as follows:
"_id": unique ID for this file (default: ObjectId)
"filename": human name for the file
"contentType": valid mime-type for the file
"length": size of the file, in bytes
Note
only used for querying, automatically set for inserts
"chunkSize": size of each of the chunks, in bytes (default: 256 kb)
"uploadDate": date when the object was first stored
Note
only used for querying, automatically set for inserts
"aliases": array of alias strings
"metadata": document containing arbitrary metadata
| Parameters: |
|
|---|
Close the GridFile.
A closed GridFile cannot be read or written any more. Calling close() more than once is allowed.
Read at most size bytes from the file (less if there isn’t enough data).
The bytes are returned as an instance of str. If size is negative or omitted all data is read. Raises ValueError if this GridFile is already closed.
| Parameters: |
|
|---|
Rename this GridFile.
Due to buffering, the rename might not actually occur until flush() or close() is called.
| Parameters: |
|
|---|
Set the current position of this GridFile (read-mode files only).
| Parameters: |
|
|---|
Write a string to the GridFile. There is no return value.
Due to buffering, the string may not actually show up in the database until the flush() or close() method is called. Raises ValueError if this GridFile is already closed. Raises TypeErrer if data is not an instance of str.
| Parameters: |
|
|---|
Write a sequence of strings to the file.
Does not add seperators.