DEV Community

Cover image for Laravel Temporary URL for local storage driver
Abrar Ahmad
Abrar Ahmad

Posted on

Laravel Temporary URL for local storage driver

Recently, I needed to use a temporary URL. Laravel supports temporary URLs out of the box with S3 drivers. I was using a local disk, but Laravel does not support a temporary URL feature for the local disk. If you are getting this exception, you are on the right article.
RuntimeException with message 'This driver does not support creating temporary URLs.'

I decided to add the temporary URL logic for my local disk. We'll accomplish this via Laravel signed URLs.

The below steps describe how to implement the getTemporaryUrl function in your Laravel app.

Step 1: Add the following code in the AppServiceProvider boot method.

Storage::disk('local')->buildTemporaryUrlsUsing(function ($path, $expiration, $options) {
            return URL::temporarySignedRoute(
                'local.temp',
                $expiration,
                array_merge($options, ['path' => $path])
            );
        });
Enter fullscreen mode Exit fullscreen mode

Notice that URL::temporarySignedRoute receives route name local.temp, which we need to define.

Step 2: Add the route in web.php

Route::get('local/temp/{path}', function (string $path){
    return Storage::disk('local')->download($path);
})->name('local.temp');
Enter fullscreen mode Exit fullscreen mode

This will allow downloading of the file using a temporary URL.

Now you can easily generate a temporary URL for the local disk.

disk = Storage::disk('releases-local');

return $disk->temporaryUrl($this->path, now()->addMinutes(5));
Enter fullscreen mode Exit fullscreen mode

PS

Your feedback is welcome if you want to suggest improvements or know any better way.

Discussion (5)

Collapse
phpyer profile image
Pierre

Thanks

Collapse
zockfoul profile image
Mauricio Zarallo

Thanks, I can't think of how to simulate this method to be able to test with dusk in github actions, I tried to use Storage::fake('s3') but in dusk it doesn't work..... very grateful

Collapse
abrardev99 profile image
Abrar Ahmad Author

Glad to help.

Collapse
phpyer profile image
Pierre

how could it be done ?, local HTTP server with a minio HTTPS configuration server, place the crt of the minio but I don't solve it

Collapse
abrardev99 profile image
Abrar Ahmad Author

I didn't get your point.