MongoDB::GridFS - A file storage utility
version v0.704.2.0
use MongoDB::GridFS; my $grid = $database->get_gridfs; my $fh = IO::File->new("myfile", "r"); $grid->insert($fh, {"filename" => "mydbfile"});
There are two interfaces for GridFS: a file-system/collection-like interface (insert, remove, drop, find_one) and a more general interface (get, put, delete). Their functionality is the almost identical (get, put and delete are always safe ops, insert, remove, and find_one are optionally safe), using one over the other is a matter of preference.
Core documentation on GridFS: http://dochub.mongodb.org/core/gridfs.
The number of bytes per chunk. Defaults to 261120 (255kb).
The prefix used for the collections. Defaults to "fs".
Collection in which file metadata is stored. Each document contains md5 and length fields, plus user-defined metadata (and an _id).
Actual content of the files stored. Each chunk contains up to 4Mb of data, as well as a number (its order within the file) and a files_id (the _id of the file in the files collection it belongs to).
my $file = $grid->get($id);
Get a file from GridFS based on its _id. Returns a MongoDB::GridFS::File.
To retrieve a file based on metadata like filename, use the "find_one" method instead.
filename
my $id = $grid->put($fh, {filename => "pic.jpg"});
Inserts a file into GridFS, adding a MongoDB::OID as the _id field if the field is not already defined. This is a wrapper for MongoDB::GridFS::insert, see that method below for more information.
MongoDB::GridFS::insert
To use a filename as the _id for subsequent get calls, you must set _id explicitly:
get
$grid->put($fh, {_id => "pic.jpg", filename => "pic.jpg"}); my $file = $grid->get("pic.jpg");
Returns the _id field.
$grid->delete($id)
Removes the file with the given _id. Will die if the remove is unsuccessful. Does not return anything on success.
my $file = $grid->find_one({"filename" => "foo.txt"});
Returns a matching MongoDB::GridFS::File or undef.
$grid->remove({"filename" => "foo.txt"});
Cleanly removes files from the database. $options is a hash of options for the remove. Possible options are:
$options
This method doesn't return anything.
my $id = $gridfs->insert($fh, {"content-type" => "text/html"});
Reads from a file handle into the database. Saves the file with the given metadata. The file handle must be readable. $options can be {"safe" = true}>, which will do safe inserts and check the MD5 hash calculated by the database against an MD5 hash calculated by the local filesystem. If the two hashes do not match, then the chunks already inserted will be removed and the program will die.
{"safe" =
Because MongoDB::GridFS::insert takes a file handle, it can be used to insert very long strings into the database (as well as files). $fh must be a FileHandle (not just the native file handle type), so you can insert a string with:
$fh
# open the string like a file my $basic_fh; open($basic_fh, '<', \$very_long_string); # turn the file handle into a FileHandle my $fh = FileHandle->new; $fh->fdopen($basic_fh, 'r'); $gridfs->insert($fh);
@files = $grid->drop;
Removes all files' metadata and contents.
@files = $grid->all;
Returns a list of the files in the database.
Kristina Chodorow <kristina@mongodb.org>
David Golden <david.golden@mongodb.org>
Mike Friedman <friedo@mongodb.com>
Florian Ragwitz <rafl@debian.org>
This software is Copyright (c) 2014 by MongoDB, Inc..
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
To install MongoDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongoDB
CPAN shell
perl -MCPAN -e shell install MongoDB
For more information on module installation, please visit the detailed CPAN module installation guide.