Reputation: 61
I make a disk in filesystem like:
'avatars' => [
'driver' => 'local',
'root' => storage_path('app/avatars')
],
is it possible / how can I ,create a temporary URL for files in this disk? i tried
return Storage::disk('avatars')->temporaryUrl('pp12.jpeg', now()->addMinutes(5));
but its show me this error:
RuntimeException
This driver does not support creating temporary URLs.
Upvotes: 1
Views: 12254
Reputation: 476
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])
);
});
Step 2: Add the route in web.php
Route::get('local/temp/{path}', function (string $path){
return Storage::disk('local')->download($path);
})
->middleware('signed')
->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));
Upvotes: 4
Reputation: 9476
As noted in the documentation, and by the error message, temporary URLs are a feature of the S3 driver, and are not supported on any other driver.
If you don't want to use S3 to store your files (and you should probably consider doing so since it's a cheap and flexible way of hosting large numbers of files), then you will need to add this functionality yourself by creating a custom route that makes the file available for a limited period of time.
There's a number of ways you could achieve this depending on your use case, but I'd suggest looking into JWT - you can generate a JWT that expires after the appropriate time period, include the JWT in the URL as a query parameter, get the token in the route, and use it to prevent access to that route after the token has expired.
Upvotes: 1