JS Blog Software development, entertainment, lifestyle.

Add Dropbox filesystem driver in your laravel 5 project


Laravel version 5, which is still under active development and set to be released in November, comes with lots of cool features. One of them is Flysystem integration. Flysystem is a filesystem abstraction PHP package which allows you to easily swap out a local filesystem for a remote one (Amazon S3, Rackspace, Dropbox and others). For now Laravel provides drivers (wrappers around flysystem code for even easier access) for local, s3 and rackspace filesystem but adding a new one was not a problem for me and it was easier than I first thought!

Here I will try to explain how I added an adapter for Dropbox.

First thing I did was going to dropbox.com/developers to make a new app where I obtained app name and access token. Than I required dropbox sdk via composer and specified config options for dropbox driver – in config/filesystem.php under disks key I added the following:

'dropbox' => [
    'driver' => 'dropbox',
    'token' => 'YOUR_ACCESS_TOKEN',
    'app' => 'YOUR_APP_NAME'

Now go to the vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php file and add the following method where appopriate ((This is not done the right way though, but for scope of this tutorial it’s fine. A better way is to extend that class, add a new method and register new class with custom service provider. That way addition will not be lost if the core class, which we are extending, gets an update via composer.)).

use Dropbox\Client;
use League\Flysystem\Adapter\Dropbox as DropboxAdapter;
class FilesystemManager implements FactoryContract {
     * Create an instance of the given driver.
     * @param  array  $config
     * @return \Illuminate\Contracts\Filesystem\Cloud
    public function createDropboxDriver(array $config)
        $client = new Client($config['token'], $config['app']);

        return $this->decorate(new Flysystem(new DropboxAdapter($client)));

That’s it! Note that if you are doing an adapter for other filesystem, method name is very important. It must be create*Driver, where * represents actual driver’s name listed in disks key in config/filesystem.php. Also take a look at other create*Driver methods in that file and in Flysystem documentation for guidelines on how to write a new driver correctly.

Now you should access your files in Dropbox like this:

use Illuminate\Contracts\Filesystem\Factory as Filesystem;

get('/', function(Filesystem $filesystem)
    return $filesystem->disk('dropbox')->get('hello.txt');
By Jernej
JS Blog Software development, entertainment, lifestyle.

About me

Jernej Sila portrait
Hi, I'm Jernej, a software engineer from Slovenia. Welcome to my personal blog. For more about me, my work and hobbies, please visit my profile page.