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])
);
});
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');
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));
PS
Your feedback is welcome if you want to suggest improvements or know any better way.
Top comments (10)
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.
Yes correct, in my real app, I check the signature like this.
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 lotGlad it helped you, and a nice tip.
Thanks
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
Glad to help.
I created package dev.to/abrardev99/laravel-package-... 😍
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
I didn't get your point.