Laravel-backup: Custom path on DO Spaces

Created on 19 Jul 2018  路  2Comments  路  Source: spatie/laravel-backup

Unfortunately, I couldn't find the option to set a custom destination path for Digital Ocean's Spaces. Isn't it available or did I just not find it?
Aiming to save backups to: /backups/{appname}/

Most helpful comment

Here is how I was able to get the backups to a specific folder in the DO Space

composer.json

"league/flysystem-aws-s3-v3": "^1.0",
"league/flysystem-cached-adapter": "^1.0",

app\Providers\SpacesStorageServiceProvider.php

<?php

namespace App\Providers;

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

class SpacesStorageServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('spaces', function ($app, $config) {
            $client = new S3Client([
                'credentials'             => [
                    'key'    => $config["key"],
                    'secret' => $config["secret"]
                ],
                'region'                  => $config["region"],
                'version'                 => "latest",
                'bucket_endpoint'         => false,
                'use_path_style_endpoint' => false,
                'endpoint'                => $config["endpoint"],
                ''
            ]);
            return new Filesystem(new AwsS3Adapter($client, $config["bucket"], $config['root']));
        });
    }
}

config\app.php

/*
 * Application Service Providers...
 */
App\Providers\SpacesStorageServiceProvider::class,

config\backup.php

'backup' => [
    'destination' => [
            'disks' => [
            'spaces_backup'
            ],
      ],
]

config\filesystems.php

'spaces_backup' => [
    'driver'   => 'spaces',
    'key'      => env('DO_SPACES_KEY'),
    'secret'   => env('DO_SPACES_SECRET'),
    'endpoint' => env('DO_SPACES_ENDPOINT'),
    'region'   => env('DO_SPACES_REGION'),
    'bucket'   => env('DO_SPACES_BUCKET'),
    'root'     => 'backups'
],       

.env

DO_SPACES_KEY="{KEY}"
DO_SPACES_SECRET="{SECRET}"
DO_SPACES_ENDPOINT="https://{REGION}.digitaloceanspaces.com"
DO_SPACES_REGION="{REGION}"
DO_SPACES_BUCKET="{BUCKET NAME}"

All 2 comments

Here is how I was able to get the backups to a specific folder in the DO Space

composer.json

"league/flysystem-aws-s3-v3": "^1.0",
"league/flysystem-cached-adapter": "^1.0",

app\Providers\SpacesStorageServiceProvider.php

<?php

namespace App\Providers;

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

class SpacesStorageServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('spaces', function ($app, $config) {
            $client = new S3Client([
                'credentials'             => [
                    'key'    => $config["key"],
                    'secret' => $config["secret"]
                ],
                'region'                  => $config["region"],
                'version'                 => "latest",
                'bucket_endpoint'         => false,
                'use_path_style_endpoint' => false,
                'endpoint'                => $config["endpoint"],
                ''
            ]);
            return new Filesystem(new AwsS3Adapter($client, $config["bucket"], $config['root']));
        });
    }
}

config\app.php

/*
 * Application Service Providers...
 */
App\Providers\SpacesStorageServiceProvider::class,

config\backup.php

'backup' => [
    'destination' => [
            'disks' => [
            'spaces_backup'
            ],
      ],
]

config\filesystems.php

'spaces_backup' => [
    'driver'   => 'spaces',
    'key'      => env('DO_SPACES_KEY'),
    'secret'   => env('DO_SPACES_SECRET'),
    'endpoint' => env('DO_SPACES_ENDPOINT'),
    'region'   => env('DO_SPACES_REGION'),
    'bucket'   => env('DO_SPACES_BUCKET'),
    'root'     => 'backups'
],       

.env

DO_SPACES_KEY="{KEY}"
DO_SPACES_SECRET="{SECRET}"
DO_SPACES_ENDPOINT="https://{REGION}.digitaloceanspaces.com"
DO_SPACES_REGION="{REGION}"
DO_SPACES_BUCKET="{BUCKET NAME}"

@mnelson7982 Thank you Matthew for your sharing, the snippet makes me a boost of understanding how the S3adapter really working.

For anyone looking for a solution for this issue, apart from following Matthew's methods, you may also need to have these two config specified in config/medialibrary.php:

    'url_generator' => \App\SpacesUrlGenerator::class,
    'path_generator' => \App\SpacesPathGenerator::class,

It simply because by default Spatie\MediaLibrary\UrlGenerator\UrlGenerator (and PathGenerator similarly) will be looking for {storage_driver_name}UrlGenerator.

MediaLibrary only provides the default LocalUrlGenerator and S3UrlGenerator, so you can just create a class and extend S3UrlGenerator then that's it.

namespace App;

use Spatie\MediaLibrary\UrlGenerator\S3UrlGenerator;

class SpacesUrlGenerator extends S3UrlGenerator
{
    // if you want to overwrite anything
}
Was this page helpful?
0 / 5 - 0 ratings