Skip to content
A file-like MogileFS client for Java
Java Shell
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


A file-like MogileFS client for Java.


  • like API
  • Supports streams of unknown length
  • Unit/Integration tests
  • Spring friendly
  • Tracker connection pooling with balancing between hosts and strategies for dealing with failed nodes
  • Local file system implementation for faking in tests (fm.last.moji.local.LocalFileSystemMoji)


Using plain-old-Java

    String hosts = ",";
    String domain = "testdomain";

    Moji moji = new SpringMojiBean(hosts, domain);

Using the Spring framework

Set some properties for your context:


Import the Moji Spring context:

    <import resource="moji-context.xml" />

Or create a Moji spring bean:

    <bean id="moji" class="fm.last.moji.spring.SpringMojiBean" destroy-method="close">
      <constructor-arg value="${moji.tracker.address}" />
      <constructor-arg value="${moji.domain}" />
      <property name="maxActive" value="${}" />
      <property name="maxIdle" value="${moji.pool.max.idle:10}" />
      <property name="testOnBorrow" value="${moji.pool.test.on.borrow:true}" />


Create/update a remote file

    MojiFile rickRoll = moji.getFile("rick-astley");
    moji.copyToMogile(new File("never-gonna-give-you-up.mp3"), rickRoll);

Or in a given storage class:

    MojiFile rickRoll = moji.getFile("rick-astley", "music-meme");

Get the remote file size

    long length = rickRoll.length();

Rename the remote file


Check the existence of a remote file

    MojiFile abba = moji.getFile("voulez-vous");
    if (abba.exists()) {

Delete the remote file


Download a remote file

    MojiFile fooFighters = moji.getFile("stacked-actors");
    fooFighters.copyToFile(new File("foo-fighters.mp3"));

Modify the storage class of a remote file


Stream from a remote file

    InputStream stream = null;
    try {
      stream = fooFighters.getInputStream();
      // Do something streamy
    } finally {

Stream to a remote file

This will either create a new file or overwrite an existing file's contents

    OutputStream stream = null;
    try {
      stream = fooFighters.getOutputStream();
      // Do something streamy
      //   stream.write(...);
    } finally {

List remote files by prefix

    List<MojiFile> files = moji.list("abba-");
    for(MojiFile file : files) {
      // abba-waterloo, abba-voulez-vous, abba-fernado, etc.

Impose a limit on the number of items returned:

    List<MojiFile> files = moji.list("abba-", 10);
    for(MojiFile file : files) {
      // abba-waterloo, abba-voulez-vous, abba-fernado, etc. - maximum of 10

Get the locations of a remote file

    File fooFighters = moji.getFile("in-your-honour"); 
    List<URL> paths = fooFighters.getPaths();
    //,, etc

Get the attributes of a remote file

Note: this is only supported on more recent versions of MogileFS.

    File fooFighters = moji.getFile("in-your-honour"); 
    MojiFileAttributes attributes = fooFighters.getAttibutes();

    String storageClass = attributes.getStorageClass();
    int deviceCount = attributes.getDeviceCount();
    int fid = attributes.getFid();

Running the integration tests

To run the integration tests you need:

  • A unix-type system (for shell script execution - FIXME)
  • A test MogileFS tracker and a storage node (installation instructions)
  • mogdelete, moglistkeys, mogupload
  • uuencode (apt-get install sharutil)

MogileFS integration test properties config:

  • These properties should be set in /
  • Set your Tracker address with the property:

  • Declare your Mogile domain with the property:

  • Declare two storage classes in your Mogile instance and assign them with these properties:

  • Choose a key prefix to avoid any key clashes with real data (you're using a test instance right?) or other tests. Otherwise we might get unexpected behaviour and file deletions:



This project uses the Maven build system.

Further work

We plan to support the Java 7 FileSystem abstraction now it's been officially released.


Copyright 2012

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Something went wrong with that request. Please try again.