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.
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/";
}
}
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\Helperswithin this namespace I created a class calledEnvironmentPathGeneratorthe 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.phpand point the configuration variablecustom_path_generator_classat my custom path generator class.My configuration change within
config/medialibrary.phpis as follows -'custom_path_generator_class' => App\Helpers\EnvironmentPathGenerator::class,And the code for
EnvironmentPathGeneratoris as follows -I hope this helps,
Regards,
Ray.