On Demand Laravel Storage Disk Configuration

on-demand-storage

Laravel Storage Disk Configuration

Sometimes, we need to create on demand storage disk connection, either it is intensionally or requirement based. Where you wish to create a disk at runtime using a given configuration without that configuration actually being present in your application’s filesystems configuration file in the config folder. To accomplish this task, you may pass a configuration array to the Storage facade’s build method. Here is an example that will help you understand this:

use Illuminate\Support\Facades\Storage;
 
$disk = Storage::build([
    'driver' => 'local',
    'root' => '/path/to/root',
]);
 
$disk->put('image.jpg', $content);

Here You can see, that we have used the Storage Facade so that we can initiate the Storage class.

use Illuminate\Support\Facades\Storage;

Now you have to use the build method and pass your configuration array to it to make the connection.

$disk = Storage::build([
    'driver' => 'local',
    'root' => '/path/to/root',
]);

This will manage the connection with your required disk, like you can see here, we have tried to establish the connection with our local drive. You can use the same process and use your other disk options like AWS S3, FTP, SFTP, etc to connect, it will provide the on-demand, direct in-code connection. You don’t have to pass every connection detail in your filesystems configuration file.

$disk->put('image.jpg', $content);

Now, you can put/store your file in your desired location in the file system. You just have to pass the exact folder path in root value here root => ‘path/to/root/folder/to/save/file’. It will help you to store your file directly in that location.

For more information, you can check Laravel’s official website here. You can find more articles about Laravel here.

Please follow and like us:

Related Posts

Leave a Reply

Share