Using FilePad for storing and retrieving files¶
FilePad utility provides the api to add and delete arbitrary files of arbitray sizes to MongoDB(filepad).
The is achieved by inserting the entire file contents to GridFS and storing the id returned by the
GridFS insertion, the user provided label and the metadata in a document in the filepad. In the following
file contents refers to the file contents stored in GridFS and
document refers to the
associated mongodb document that stores the
label and other miscellaneous information
pertaining to the file.
Create a FilePad object:
fp = FilePad.auto_load()
To add a file:
file_id, label = fp.add(<path>, <label>, compress=True/False, metadata=<metadata>)
<path> is a string path to the file to be inserted,
<label> is some
unique label that can be used to retrieve the file, the ‘compress’ argument value tells whether or not to compress
the file contents before insertion,
<metadata> is a python dictionary input that will stored in the key ‘metadata’.
A bare minimum document in the filepad database consists of keys
to store the string representation of the object id returned by GridFS), ``label``(used to store the
user assigned label for the file), ``original_file_name ,
On successful insertion the
file_id and the
label are returned.
Retrieve file contents and the associated document by the label:
file_contents, doc = fp.get_file(<label>)
where the returned values
doc are the contents of the file with label
and the document attached to it respectively.
Retrieve file contents and the associated document by the file id:
file_contents, doc = fp.get_file_by_id(<file_id>)
<file_id> is the file id associated with the file(the id returned during insertion)
Retrieve all the file contents and the associated documents by a general mongo query:
all_files = fp.get_file_by_query(<query>)
<query> is monogo query dict and the returned values
all_files is a list of
tuples that match the query.
To delete the contents of the file and the associated document by label:
To delete the file contents and the associated document by the file id:
To delete all the file contents and the associated documents by a general mongo query: