Skip to content

Instantly share code, notes, and snippets.

@mazedlx
Last active March 7, 2023 16:45
Show Gist options
  • Save mazedlx/806d6f2f50d7287fd9a9040db10890ff to your computer and use it in GitHub Desktop.
Save mazedlx/806d6f2f50d7287fd9a9040db10890ff to your computer and use it in GitHub Desktop.
Laravel S3 ServiceProvider for Exoscale

How To Use Laravel with Exoscale Storage (AWS S3 compliant)

Make a new service provider

$ php artisan make:provider ExoscaleServiceProvider

Edit the provider to look like this

<?php

namespace App\Providers;

use Aws\S3\S3Client;
use League\Flysystem\Filesystem;
use Aws\Laravel\AwsServiceProvider;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\AwsS3v3\AwsS3Adapter;

class ExoscaleServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('s3', function ($app, $config) {
            $client = new S3Client([
                'credentials' => [
                    'key' => $config['key'],
                    'secret' => $config['secret'],
                ],
                'region' => $config['region'],
                'version' => $config['version'],
                'endpoint' => $config['url'],
            ]);

            return new Filesystem(new AwsS3Adapter($client, $config['bucket']));
        });
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Add the following driver to config/filesystems.php

'exoscale' => [
    'driver' => 's3',
    'key' => env('EXOSCALE_ACCESS_KEY_ID'),
    'secret' => env('EXOSCALE_SECRET_KEY'),
    'region' => env('EXOSCALE_REGION'),
    'bucket' => env('EXOSCALE_BUCKET'),
    'url' => env('EXOSCALE_URL'),
    'version' => env('EXOSCALE_VERSION'),
]

And finally, add the following vars to your .env file:

EXOSCALE_ACCESS_KEY_ID=YOUR_API_KEY
EXOSCALE_SECRET_KEY=YOUR_SECRET
EXOSCALE_REGION=YOUR_REGION
EXOSCALE_BUCKET=YOUR_BUCKET_NAME
EXOSCALE_URL=THE_STORAGE_API_ENDPOINT
EXOSCALE_VERSION=latest
@3Descape
Copy link

3Descape commented Mar 7, 2023

Hello!
Thank you very much for this little snipped. It helped me a lot!

Just wanted to add an update, for everyone else using Laravel 9, as it now uses Flysystem 3.x by default, resulting in some changes. The callback should now return a FilesystemAdapter directly https://laravel.com/docs/9.x/upgrade#flysystem-3.

use League\Flysystem\Filesystem;
use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;

Storage::extend('s3', function ($app, $config) {
            $adapter = new AwsS3V3Adapter(new S3Client([
                'credentials' => [
                    'key' => $config['key'],
                    'secret' => $config['secret'],
                ],
                'region' => $config['region'],
                'version' => $config['version'],
                'endpoint' => $config['url'],
            ]), $config['bucket']);

            return new FilesystemAdapter(new Filesystem($adapter, $config), $adapter, $config);
        });

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