[FIXED] Laravel s3 multiple buckets

Issue

In my Laravel application, I need to manipulate files that reside in multiple buckets simultaneously in one session. I haven’t found a way to change the current bucket several times because the .env file looks like this:

S3_KEY='MY-KEY'
S3_SECRET='My Secret'
S3_REGION='us-east-1'
S3_BUCKET='first used bucket'
 

I found a place where you can do this:

Config::set('filesystems.disks.s3.bucket', 'another-bucket');
 

But it only works once. What I need is something like:

Storage::disk('s3')->put('/bucket name/path/filename.jpg', $file, 'public');
 

/bucket-name/ is any bucket you have created. what can i do? Thank you very much!

Solution

You are correct in that Config::set(); only works once per request. My estimation is that this is done intentionally to stop the kind of thing you are attempting to do in your code example.

In config/filesystems.php you can list any number of “disks”. These are locations of your file repositories. It looks like so:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root'   => storage_path('app'),
    ],

    'ftp' => [
        'driver'   => 'ftp',
        'host'     => 'ftp.example.com',
        'username' => 'your-username',
        'password' => 'your-password',

        // Optional FTP Settings...
        // 'port'     => 21,
        // 'root'     => '',
        // 'passive'  => true,
        // 'ssl'      => true,
        // 'timeout'  => 30,
    ],

    's3' => [
        'driver' => 's3',
        'key'    => env('S3_KEY',''),
        'secret' => env('S3_SECRET',''),
        'region' => env('S3_REGION',''),
        'bucket' => env('S3_BUCKET',''),
    ],
]

The Solution

The solution is to create a new disk of the extra buckets you want to use. Treat your buckets like different disks.

Note: The user that the S3_Key belongs to needs to have permissions to perform your required actions on the S3 buckets you are setting up as additional ‘disks’.

'disks' => [

    //All your other 'disks'
    ...

    //My default bucket details.
    's3' => [
        'driver' => 's3',
        'key'    => env('S3_KEY',''),
        'secret' => env('S3_SECRET',''),
        'region' => env('S3_REGION',''),
        'bucket' => env('S3_BUCKET',''),
    ],

    's3MyOtherBucketName' => [
        'driver' => 's3',
        'key'    => env('S3_KEY',''),
        'secret' => env('S3_SECRET',''),
        'region' => env('S3_REGION',''),
        'bucket' => 'myOtherBucketName',
    ],

    's3YetAnotherBucketName' => [
        'driver' => 's3',
        'key'    => env('S3_KEY',''),
        'secret' => env('S3_SECRET',''),
        'region' => env('S3_REGION',''),
        'bucket' => 'yetAnotherBucketName',
    ],
]

Then whenever you want to access the bucket of your choice call it like so:

Storage::disk('s3')->put($fileName, $data);
Storage::disk('s3MyOtherBucketName')->put($anotherFileName, $moreData);
Storage::disk('s3YetAnotherBucketName')->put($yetAnotherFileName, $evenMoreData);

Answered By – Samuel Hawksby-Robinson

Answer Checked By – Candace Johnson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published