#development #laravel #php

Using the Laravel Filesystem, it's very easy to use cloud providers as regular filesystems.

By default, Amazon S3 (compatible) filesystems are suppported out-of-the-box.

In my setup, I wanted to use Dropbox instead.

As Laravel's filesystem is based on Flysystem, I started with installing a Flysystem driver for Dropbox:

$ composer install spatie/flysystem-dropbox

The next step is to create a provider under app/Providers/DropboxServiceProvider.php:

 1<?php
 2
 3namespace App\Providers;
 4
 5use Illuminate\Filesystem\FilesystemAdapter;
 6use Illuminate\Support\Facades\Storage;
 7use Illuminate\Support\ServiceProvider;
 8use League\Flysystem\Filesystem;
 9use Spatie\Dropbox\Client;
10use Spatie\FlysystemDropbox\DropboxAdapter;
11
12class DropboxServiceProvider extends ServiceProvider
13{
14    public function register()
15    {
16    }
17
18    public function boot()
19    {
20        Storage::extend('dropbox', function ($app, $config) {
21            $adapter = new DropboxAdapter(new Client(
22                $config['authorization_token']
23            ));
24
25            return new FilesystemAdapter(
26                new Filesystem($adapter, $config),
27                $adapter,
28                $config
29            );
30        });
31    }
32}

The provider extends the Storage class by adding a custom provider called "dropbox" in our example.

FilesystemAdapater is the link between Flysystem and what Laravel expects.

Don't forget to register your provider in config/app.php under the key providers.

 1<?php
 2
 3return [
 4    // ...
 5
 6    'providers' => [
 7        // ...
 8
 9        App\Providers\DropboxServiceProvider::class,
10
11        // ...
12    ],
13
14    // ...
15];

The next step is to add a new filesystem to config/filesystems.php:

 1<?php
 2
 3return [
 4    // ...
 5
 6    'disks' => [
 7        // ...
 8
 9        'dropbox-backup' => [
10            'driver' => 'dropbox',
11            'authorization_token' => env('DROPBOX_ACCESS_TOKEN'),
12        ],
13    ],
14
15    // ...
16];

The last step is to generate an access token for Dropbox and add it to your .env file:

1DROPBOX_ACCESS_TOKEN=<your-access-token>