How to use temporary download URLs in Laravel

Categorized as Laravel

If you want to prevent unnecessary access to your cloud-based files, you can try temporary download URLs in Laravel. As an example, you can give your user the redirect URL to Amazon S3 storage, and that ULR expires in 5 seconds (it’s more than enough time, but you can play around with this value).

public function download(File $file)
{
  // Initiate file download by redirecting to a temporary Cloud URL that expires in 5 seconds
  return redirect()->to(
    Storage::disk('s3')->temporaryUrl($file->name, now()->addSeconds(5))
  );
}

Leave a reply

Your email address will not be published.