When you have stored files privately in Amazon s3 which you want to make public only for selected users for a limited amount of time, you can achieve that using the Storage facade in Laravel.
To create temporary files, you can use temporaryUrl method from the Illuminate\Support\Facade\Storage facade. You can use the method on the following syntax.
use Illuminate\Support\Facades\Storage;
$temporarySignedUrl = Storage::disk('s3')->temporaryUrl("filepath.pdf", now()->addMinutes(10));
temporaryUrl method accepts two parameters as follows,
- Path: This parameter accepts the full path of the file in the s3 bucket
- Expiry Time: You can set the date for the expiry of the link.
Top comments (5)
For the unsuspecting develoer, if you are saving the pre-signed urls in db, make sure the column type is text. i spent a whole day trying to save it in a string column, resulting in incomplete url (laravel chpped the end of the url string since the url does not fit in the string column).
default string length is 100 which is probably why it didnt work.
$table->string('url', length: 1000);
something like this should work
Great. Thanks for sharing !
Very helpful
Thank you Sonika