The purpose of this article is to show how to integrate the Flysystem file system library with Lumen framework. It is recommended to use Graham Campbell’s Flysystem wrapper. Let’s begin.
Installation
Install Graham Campbell’s flysystem wrapper.
$ composer require graham-campbell/flysystem
Depending on where you want to store your files there are other dependencies required. For instance if you want to store your files in an sftp location there is an adapter for it that you have to install. Here is a list of all the available adapters:
- The AwsS3 adapter requires
league/flysystem-aws-s3-v3
- The Azure adapter requires
league/flysystem-azure
- The Copy adapter requires
league/flysystem-copy
- The Dropbox adapter requires
league/flysystem-dropbox
- The GridFS adapter requires
league/flysystem-gridfs
- The Rackspace adapter requires
league/flysystem-rackspace
- The Sftp adapter requires
league/flysystem-sftp
- The WebDav adapter requires
league/flysystem-webdav
- The ZipAdapter adapter requires
league/flysystem-ziparchive
- The adapter caching support requires
league/flysystem-cached-adapter
- The eventable filesystem support requires
league/flysystem-eventable-filesystem
So go ahead and install the one or ones that you intend to use.
After you have installed the Flysystem and the Adapter/s you need to register the service provider. Open up the bootstrap/app.php file and add this line of code somewhere in the middle (i.e. after the $app
variable is defined and before it is returned).
$app->register(GrahamCampbell\Flysystem\FlysystemServiceProvider::class);
Configuration
The Flysystem requires configuration for each of the adapters that you intend to use. To do this first copy the flysystem.php config file from the package. That is copy vendor/graham-campbell/flysystem/config/flysystem.php
file to your config
folder. Here is what the file looks like.
<?php /* * This file is part of Laravel Flysystem. * * (c) Graham Campbell <graham@alt-three.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ return [ /* |-------------------------------------------------------------------------- | Default Connection Name |-------------------------------------------------------------------------- | | Here you may specify which of the connections below you wish to use as | your default connection for all work. Of course, you may use many | connections at once using the manager class. | */ 'default' => 'local', /* |-------------------------------------------------------------------------- | Flysystem Connections |-------------------------------------------------------------------------- | | Here are each of the connections setup for your application. Examples of | configuring each supported driver is shown below. You can of course have | multiple connections per driver. | */ 'connections' => [ 'awss3' => [ 'driver' => 'awss3', 'key' => 'your-key', 'secret' => 'your-secret', 'bucket' => 'your-bucket', 'region' => 'your-region', 'version' => 'latest', // 'bucket_endpoint' => false, // 'calculate_md5' => true, // 'scheme' => 'https', // 'endpoint' => 'your-url', // 'prefix' => 'your-prefix', // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'azure' => [ 'driver' => 'azure', 'account-name' => 'your-account-name', 'api-key' => 'your-api-key', 'container' => 'your-container', // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'copy' => [ 'driver' => 'copy', 'consumer-key' => 'your-consumer-key', 'consumer-secret' => 'your-consumer-secret', 'access-token' => 'your-access-token', 'token-secret' => 'your-token-secret', // 'prefix' => 'your-prefix', // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'dropbox' => [ 'driver' => 'dropbox', 'token' => 'your-token', 'app' => 'your-app', // 'prefix' => 'your-prefix', // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'ftp' => [ 'driver' => 'ftp', 'host' => 'ftp.example.com', 'port' => 21, 'username' => 'your-username', 'password' => 'your-password', // 'root' => '/path/to/root', // 'passive' => true, // 'ssl' => true, // 'timeout' => 20, // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'gridfs' => [ 'driver' => 'gridfs', 'server' => 'mongodb://localhost:27017', 'database' => 'your-database', // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'local' => [ 'driver' => 'local', 'path' => storage_path('files'), // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'null' => [ 'driver' => 'null', // 'eventable' => true, // 'cache' => 'foo' ], 'rackspace' => [ 'driver' => 'rackspace', 'endpoint' => 'your-endpoint', 'region' => 'your-region', 'username' => 'your-username', 'apiKey' => 'your-api-key', 'container' => 'your-container', // 'internal' => false, // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'replicate' => [ 'driver' => 'replicate', 'source' => 'your-source-adapter', 'replica' => 'your-replica-adapter', // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'sftp' => [ 'driver' => 'sftp', 'host' => 'sftp.example.com', 'port' => 22, 'username' => 'your-username', 'password' => 'your-password', // 'privateKey' => 'path/to/or/contents/of/privatekey', // 'root' => '/path/to/root', // 'timeout' => 20, // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'webdav' => [ 'driver' => 'webdav', 'baseUri' => 'http://example.org/dav/', 'userName' => 'your-username', 'password' => 'your-password', // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], 'zip' => [ 'driver' => 'zip', 'path' => storage_path('files.zip'), // 'visibility' => 'public', // 'pirate' => false, // 'eventable' => true, // 'cache' => 'foo' ], ], /* |-------------------------------------------------------------------------- | Flysystem Cache |-------------------------------------------------------------------------- | | Here are each of the cache configurations setup for your application. | There are currently two drivers: illuminate and adapter. Examples of | configuration are included. You can of course have multiple connections | per driver as shown. | */ 'cache' => [ 'foo' => [ 'driver' => 'illuminate', 'connector' => null, // null means use default driver 'key' => 'foo', // 'ttl' => 300 ], 'bar' => [ 'driver' => 'illuminate', 'connector' => 'redis', // config/cache.php 'key' => 'bar', 'ttl' => 600, ], 'adapter' => [ 'driver' => 'adapter', 'adapter' => 'local', // as defined in connections 'file' => 'flysystem.json', 'ttl' => 600, ], ], ];
You can now modify the file to set your configuration. You would observe that there are three config options in the config above.
Default Connection Name
This option ('default'
) is where you may specify which of the connections below you wish to use as your default connection for all work. The default value for this setting is 'local'
.
Flysystem Connections
This option ('connections'
) is where each of the connections are setup for your application. Examples of configuring each supported driver are included in the config file.
Flysystem Cache
This option ('cache'
) is where each of the cache configurations setup for your application. There are currently two drivers: illuminate and adapter. Examples of configuration are included.
Usage
use GrahamCampbell\Flysystem\Facades\Flysystem; // writing this: Flysystem::connection('local')->read('test.txt'); // is identical to writing this: Flysystem::read('test.txt'); // and is also identical to writing this: Flysystem::connection()->read('test.txt'); // this is because the local connection is configured to be the default Flysystem::getDefaultConnection(); // this will return local // we can change the default connection Flysystem::setDefaultConnection('foo'); // the default is now foo
If you prefer to use dependency injection over facades, then you can easily inject the manager like so:
use GrahamCampbell\Flysystem\FlysystemManager; class Foo { protected $flysystem; public function __construct(FlysystemManager $flysystem) { $this->flysystem = $flysystem; } public function bar() { $this->flysystem->read('test.txt'); } }
And.. you are good to go! Hope this article was helpful.