Laravel-medialibrary: Specify folder for S3 storage

Created on 13 Jul 2017  路  8Comments  路  Source: spatie/laravel-medialibrary

Is it possible to specify a path for S3 storage?

This problem manifests when using a multi-environment setup. I have 4 environments, local, dev, stage and production ... all using a single bucket. When the DB ids for a media object clash they overwrite each other.

Perhaps I'm approaching this incorrectly, any advice is appreciated.

Most helpful comment

@mindmergedesign of course, info below.

First up, make sure you publish the medialibrary configs ...

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"

In my app I have a namespace App\Helpers within this namespace I created a class called EnvironmentPathGenerator the contents of this class are at the end of this comment, but it's really simple and basically just prepends the env to the file path.

In order to make use of the custom path generator class, I then needed to update the file config/medialibrary.php and point the configuration variable custom_path_generator_class at my custom path generator class.

My configuration change within config/medialibrary.php is as follows -

'custom_path_generator_class' => App\Helpers\EnvironmentPathGenerator::class,

And the code for EnvironmentPathGenerator is as follows -

namespace App\Helpers;

use Spatie\MediaLibrary\Media;
use Spatie\MediaLibrary\PathGenerator\PathGenerator;

class EnvironmentPathGenerator implements PathGenerator
{
    protected $path;

    public function __construct()
    {
        $this->path = app()->env . '/';
    }

    public function getPath(Media $media) : string
    {
        return $this->path . $media->id . '/';
    }

    public function getPathForConversions(Media $media) : string
    {
        return $this->getPath($media) . 'conversions/';
    }
}

I hope this helps,
Regards,
Ray.

All 8 comments

Check out this part of the documentation:
https://docs.spatie.be/laravel-medialibrary/v5/advanced-usage/using-a-custom-directory-structure#using-a-custom-directory-structure

I assume you could just add a config var with a folder path to those functions, then you can you change that config var for each environment.

This is perfect and much better than I hoped for. I've implemented and it's working as expected. Thanks for helping me with this. I'll be sure to RTFM next time :)

I owe you guys a few postcards.

@wsray would you mind sharing how you implemented this? I am not really sure where to add the config file containing the functions.

@mindmergedesign of course, info below.

First up, make sure you publish the medialibrary configs ...

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"

In my app I have a namespace App\Helpers within this namespace I created a class called EnvironmentPathGenerator the contents of this class are at the end of this comment, but it's really simple and basically just prepends the env to the file path.

In order to make use of the custom path generator class, I then needed to update the file config/medialibrary.php and point the configuration variable custom_path_generator_class at my custom path generator class.

My configuration change within config/medialibrary.php is as follows -

'custom_path_generator_class' => App\Helpers\EnvironmentPathGenerator::class,

And the code for EnvironmentPathGenerator is as follows -

namespace App\Helpers;

use Spatie\MediaLibrary\Media;
use Spatie\MediaLibrary\PathGenerator\PathGenerator;

class EnvironmentPathGenerator implements PathGenerator
{
    protected $path;

    public function __construct()
    {
        $this->path = app()->env . '/';
    }

    public function getPath(Media $media) : string
    {
        return $this->path . $media->id . '/';
    }

    public function getPathForConversions(Media $media) : string
    {
        return $this->getPath($media) . 'conversions/';
    }
}

I hope this helps,
Regards,
Ray.

@wsray Thank you so much! I'll give it a try later today.

@wsray it worked perfectly, Thanks again!

Its good to note that the import has changed from use Spatie\MediaLibrary\Media; to use Spatie\MediaLibrary\Models\Media;

Updated for medialibrary v7:

Run:

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"

Add this to your config/medialibrary.php file:

    'path_generator' => App\Helpers\EnvironmentPathGenerator::class,

And finally your AppHelpersEnvironmentPathGenerator.php looks like this

<?php

namespace App\Helpers;

use Spatie\MediaLibrary\Models\Media;
use Spatie\MediaLibrary\PathGenerator\PathGenerator;

// Customize the path where the image gets stored (on the local filesystem, on S3, etc)
class EnvironmentPathGenerator implements PathGenerator
{
    protected $path;

    public function __construct()
    {
        $this->path = app()->env;
    }

    public function getPath(Media $media): string
    {
        return $this->path . $media->id . "/";
    }

    public function getPathForConversions(Media $media): string
    {
        return $this->getPath($media) . "conversions/";
    }

    public function getPathForResponsiveImages(Media $media): string
    {
        return $this->getPath($media) . "responsive/";
    }
}


Was this page helpful?
0 / 5 - 0 ratings

Related issues

Radiergummi picture Radiergummi  路  4Comments

mohammad6006 picture mohammad6006  路  4Comments

intrepidws picture intrepidws  路  3Comments

amrnn90 picture amrnn90  路  3Comments

aaronfullerton picture aaronfullerton  路  4Comments