Sep 28, 2014 | laravel, laravel 5, 5.0

Laravel 5.0 - Cloud File Drivers

Series

This is a series of posts on New Features in Laravel 5.0.

!
Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

In Laravel 5, you can now use the same Filesystem class which previously simplified interactions with the local filesystem, and now also use it with S3 and Rackspace. It's a simple interface, powered on the backend by Frank de Jonge's Flysystem.

That means that you can write your app just like you did using local file storage:

/**
 * Save a thing
 * @param Thing  $thing
 * @param string $filename
 */
public function saveThing(Thing $thing, $filename)
{
    File::put('uploads/' . $filename, $thing);
}

But now you can, at any point, change your production app settings to use an external host (we'll use s3 in our example) instead, without changing a line of your business logic.

Making the switch to s3

First, you have to add the cloud provider's dependency to composer.json; for s3, it's the AWS SDK (aws\aws-sdk-php).

$ composer require aws\aws-sdk-php

Then, edit config/filesystems.php (or config/production/filesystems.php, so you're only configuring for the production site), change the default driver from local to s3, and then add your s3 credentials to the s3 section of the disks array:

return [
    'default' => 's3',
    'disks' => [
        's3' => [
            'driver' => 's3',
            'key' => 'fslkfqweoirqew',
            'secret' => '24j12oin12oi5nio251',
            'bucket' => 'my-awesome-website-bucket'                        
        ]
    ]
];

Cloud default vs. normal default

Uniquely, the Filesystem config has two defaults: The Filesystem default (which is injected when you typehint Illuminate\Contracts\Filesystem\Filesystem, and also bound to the Container as filesystem.disk) and the Cloud default (which is injected when you typehint Illuminate\Contracts\Filesystem\Cloud, and also bound to the Container as filesystem.cloud). This way you can have any given environment have a default local and a default cloud filesystem config.

If you're using the façade, you're going to get the default default by default, rather than the cloud default. (Say that five times fast...)

Notes

  • You could also use the same credentials for multiple environments and just use different buckets; or you could use different credentials; or you could even use different drivers. It's entirely up to you.
  • All of the usual File methods are available against these new drivers—put, get, etc.
  • Remember to inject the Filesystem Contract (Illuminate\Contracts\Filesystem\Filesystem) instead of using the façade if you're accessing files anywhere other than your controllers.
  • I didn't know this, but Jeffrey Way shows you how you can in your code (not in config) define which driver you want to use by injecting the Filesystem Factory. Learn more at his Laracast about Multiple Filesystems

Conclusion

Once you've installed the AWS SDK and edited filesystems.php, all of your file operations are now happening against your s3 account. That's it! No extra work, no extra steps: you're up and running, storing and accessing files in the cloud like a pro. Way to go.


Comments? I'm @stauffermatt on Twitter


Tags: laravel  •  laravel 5  •  5.0


This is part of a series of posts on New Features in Laravel 5.0:

  1. Sep 10, 2014 | laravel, 5.0, laravel 5
  2. Sep 10, 2014 | laravel, 5.0, laravel 5
  3. Sep 12, 2014 | laravel, laravel 5, 5.0
  4. Sep 20, 2014 | laravel, 5.0, laravel 5
  5. Sep 28, 2014 | laravel, laravel 5, 5.0
  6. Sep 30, 2014 | laravel, 5.0, laravel 5
  7. Oct 9, 2014 | laravel, 5.0, laravel 5
  8. Oct 10, 2014 | laravel, 5.0, laravel 5
  9. Oct 10, 2014 | laravel, 5.0, laravel 5
  10. Nov 20, 2014 | laravel, 5.0, laravel 5
  11. Jan 2, 2015 | laravel, 5.0, commands, laravel 5
  12. Jan 16, 2015 | laravel, laravel 5
  13. Jan 19, 2015 | laravel 5, laravel
  14. Jan 21, 2015 | laravel, events, 5.0, laravel 5
  15. Jan 26, 2015 | laravel, laravel 5
  16. Feb 1, 2015 | laravel, laravel 5
  17. Feb 14, 2015 | laravel 5, laravel, eloquent

Subscribe

For quick links to fresh content, and for more thoughts that don't make it to the blog.