Laravel Generate Temporary URLs of AWS S3 Files

Sometimes, we want to share a file with someone for a limited time. We can easily do it in Laravel. In this article, I’m going to share how to generate temporary URLs of AWS S3 file in Laravel. Let’s get started:

Table of Contents

  1. Generate Temp URLs
  2. More Parameters

Generate Temp URLs

By using temporaryUrl() method, we can easily create temp URLs in Laravel. We have to pass two mandatory parameters in the method. One is file path and another one is duration.

Have a look at an example:

use Illuminate\Support\Facades\Storage;

$url = Storage::temporaryUrl(
    'filename.png', now()->addMinutes(10)

Laravel creates pre-signed URLs using AWS SDK.

More Parameters

We can pass more additional S3 request parameters to the temporaryUrl() method like:

use Illuminate\Support\Facades\Storage;

$url = Storage::temporaryUrl(
        'ResponseContentType' => 'application/octet-stream',
        'ResponseContentDisposition' => 'attachment; filename=file2.png',

That’s all, artisans. Thanks for reading.