Using MogileFS from Perl crumbs=>> MogileFS body<=

Using the provided Perl API, here are some examples of how to create the MogileFS object and then to create a new file within MogileFS, how to delete a file, and how to get a file out of MogileFS and to determine where the file is.

An example snippet to create a MogileFS object:

use MogileFS;
my $mogfs = MogileFS->new(domain => 'domain_name',
                          hosts  => [ '10.1.0.11:6001' ],
                          # only on NFS/disk based installations
                          root   => '/var/mogdata',);
die "Unable to initialize MogileFS object.\n" unless $mogfs;

Once you have a MogileFS object, it's easy to create new files and stick them in the system:

my $fh = $mogfs->new_file("file_key", "file_class");
die "Unable to allocate filehandle.\n" unless $fh;
$fh->print($file_contents);
die "Unable to save file to MogileFS.\n" unless $fh->close;

That's the simplest way to save a file. Make sure to check the return value of the close function to make sure that it is non-zero.

To get a file's contents easily, use this code:

my $file_contents = $mogfs->get_file_data("file_key");

Deleting a file is just as simple:

die "Unable to delete file.\n" unless
    $mogfs->delete("file_key");

If you want to get access to an actual representation of the file, you can also ask for the paths to the file. This function will return either an actual filesystem path if you're using an NFS/disk based system or a URL. For example:

my @paths = $mogfs->get_paths("file_key", $no_verify);

Of note is the second parameter. If set to a true value, this instructs MogileFS not to verify the validity of the first path before returning. If your code is going to verify the data you get back anyway, you might as well set this to true so that you get the paths much faster.

Those are the basics of using the MogileFS client library. This documentation will be expanded as needed.

<=body page?>