Hidden Methods Intermediate Since Laravel 5.5

Storage::temporaryUrl()

Generate a time-limited, signed URL to a private file — grant temporary access without making the file public.

Overview

Storage::temporaryUrl() creates a pre-signed URL that expires after a given time. It works with S3, GCS, and other cloud storage drivers, letting you grant time-limited access to files that are otherwise private.

Usage

use Illuminate\Support\Facades\Storage;

// Generate a URL that expires in 5 minutes
$url = Storage::disk('s3')->temporaryUrl(
    'invoices/invoice-2024-001.pdf',
    now()->addMinutes(5),
);

You can also pass additional S3 request parameters:

$url = Storage::disk('s3')->temporaryUrl(
    'reports/annual-2024.xlsx',
    now()->addHour(),
    [
        'ResponseContentDisposition' => 'attachment; filename="report.xlsx"',
        'ResponseContentType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    ],
);

Real-World Example

Serving private documents from a controller:

class DownloadInvoiceController
{
    public function __invoke(Invoice $invoice): RedirectResponse
    {
        $this->authorize('view', $invoice);

        $url = Storage::disk('s3')->temporaryUrl(
            $invoice->file_path,
            now()->addMinutes(10),
        );

        return redirect($url);
    }
}

For Local Disk

The local driver doesn't support temporary URLs natively, but you can use Laravel's signed URL feature as an alternative:

// In AppServiceProvider::boot()
Storage::disk('local')->buildTemporaryUrlsUsing(
    function (string $path, DateTime $expiration, array $options) {
        return URL::temporarySignedRoute(
            'files.download',
            $expiration,
            ['path' => $path],
        );
    }
);

When to Use

  • Serving private invoices, reports, or user uploads
  • Sharing files via email links that should expire
  • Download buttons for premium content
  • Any file that shouldn't have a permanent public URL