Skip to content

Instantly share code, notes, and snippets.

@zoparga
Last active February 22, 2024 13:24
Show Gist options
  • Save zoparga/33ba4100cf3399a074f90f1f4e2138ed to your computer and use it in GitHub Desktop.
Save zoparga/33ba4100cf3399a074f90f1f4e2138ed to your computer and use it in GitHub Desktop.
Laravel create temp url for files
//AppServiceProvider.php
Storage::disk('local')->buildTemporaryUrlsUsing(function ($path, $expiration, $options) {
return URL::temporarySignedRoute(
'local.temp',
$expiration,
array_merge($options, ['path' => $path])
);
});
// web.php
Route::get('local/temp/{path}', function (string $path) {
if (! \request()->hasValidSignature()) {
abort(401);
}
return Storage::disk('local')->download($path);
})
->where('path', '.*')
->name('local.temp');
// ANYWHERE
$disk = Storage::disk('releases-local');
return $disk->temporaryUrl($this->path, now()->addMinutes(15));
@babacarcissedia
Copy link

Yep, had to do exactly this. Laravel has no mention of it. Thank you

@zoparga
Copy link
Author

zoparga commented Feb 22, 2024

Thanks for the compliment!

To be honest, laravel documentation partially mentions (https://gist.github.com/zoparga/33ba4100cf3399a074f90f1f4e2138ed?permalink_comment_id=4927572#gistcomment-4927572) but not a full implementation.

@babacarcissedia
Copy link

That is what I mean. The implementation was not complete. Had to figure it out from the route to controller definition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment