DEV Community

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

Posted on • Edited 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.

Top comments (10)

Collapse
 
iflyinq profile image
Niels • Edited

You might as well want to add the following code to the route, currently its skipping the route's signature 😊
For the rest, great example! Thanks for sharing.

    if (! \request()->hasValidSignature()) {
        abort(401);
    }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
abrardev99 profile image
Abrar Ahmad

Yes correct, in my real app, I check the signature like this.

Collapse
 
kaboufl profile image
Romain L.

If you need to get files from subfolders ( with "/" symbol involved ), you might add ->where('path', '.*')->name('local.temp'); after the route, else if there's a "/" symbol it will look for another route. But huge snippet, now my dev environment is complete, thanks a lot

Collapse
 
abrardev99 profile image
Abrar Ahmad

Glad it helped you, and a nice tip.

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

Glad to help.

Collapse
 
abrardev99 profile image
Abrar Ahmad

I created package dev.to/abrardev99/laravel-package-... 😍

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

I didn't get your point.