'Spatie media library upload to Amazon s3 bucket subfolder

I am using spatie media library in Laravel with the below code to upload images to s3 bucket

$file = $this->fileUploadModel
             ->addMediaFromDisk($file->path(), 's3')
             ->toMediaCollection();

The image is saved to s3 bucket on the format:

  • my_bucket_name/1/image_name.png
  • my_bucket_name/2/image_name.png etc

However I want a way to store the images inside an images folder ie.

my_bucket_name/images/1/image_name.png

By using only Laravel you can do that with a simple:

$file->store('images','s3');

How can I do that?



Solution 1:[1]

I implemented the following solution:

Un file config/filesystems.php I defined the following disk:

's3-media' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'root' => 'images/media',
    ],

In position root of the s3-media array I indicate the main path where the images will be stored.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anderson Mendoza Aguiar