Skip to content

Instantly share code, notes, and snippets.

@stijnh92
Last active April 16, 2018 08:04
Show Gist options
  • Save stijnh92/4dd4cf1a59e890707d514f06deec29c5 to your computer and use it in GitHub Desktop.
Save stijnh92/4dd4cf1a59e890707d514f06deec29c5 to your computer and use it in GitHub Desktop.
S3 version 2 compatible storage driver for Laravel 5.1+
<?php
/**
* @file
* Usage:
* 1. remove the V3 AWS PHP sdk when present
* composer remove "league/flysystem-aws-s3-v3"
* 2. install the V3 AWS PHP sdk when present
* composer require "league/flysystem-aws-s3-v2"
* 3. Add the provider to your config/app.php:
* 'providers' => [
* // Other Service Providers
* App\Providers\S3v2ServiceProvider::class,
* ],
* 4. Add your S3 credentials to config/filesystems.php:
* 'disks' => [
* 'remote' => [
* 'driver' => 's3v2',
* 'key' => 'ACCESS_KEY',
* 'secret' => 'SECRET_KEY',
* 'bucket' => 'BUCKET_NAME',
* 'base_url' => 'BASE_URL'
* ],
* ],
*/
namespace App\Providers;
use Aws\S3\S3Client;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\AwsS3v2\AwsS3Adapter;
use League\Flysystem\Filesystem;
class S3v2ServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
Storage::extend('s3v2', function ($app, $config) {
$client = S3Client::factory([
'key' => $config['key'],
'secret' => $config['secret'],
'base_url' => $config['base_url']
]);
return new Filesystem(new AwsS3Adapter($client, $config['bucket']));
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
@malc0mn
Copy link

malc0mn commented Apr 16, 2018

Point 2. in the comment you added still has V3 in in it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment