hosni/laravel-temporary-urls-minio

Fix temporary url generation when using MinIO as S3 backend

Installs: 124

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/hosni/laravel-temporary-urls-minio

v1.0.0 2025-09-08 14:01 UTC

This package is auto-updated.

Last update: 2025-09-13 06:23:19 UTC


README

Latest Version on Packagist Total Downloads License Laravel

🚀 Introduction

By default, Laravel’s temporaryUrl() method works perfectly with S3-compatible storage.
It generates a temporary URL to an object in your S3 bucket, easy peasy!

But when you switch to MinIO, things get a little wild. Temporary URLs might not work if the generated endpoint isn’t directly accessible by your client (browser, mobile app, etc).

You may have run into this when using temporaryUrl() directly, or when a package like spatie/laravel-medialibrary calls $media->getTemporaryUrl() behind the scenes.

From the Laravel documentation:

Warning

Generating temporary storage URLs via the temporaryUrl method may not work when using MinIO if the endpoint is not accessible by the client. , Laravel Filesystem Docs

But here’s the BIG question:

How do you make MinIO’s temporaryUrl() work in Laravel?

This package is your plug-and-play hero! Just install it, and let the magic happen. 🎩✨

📦 Installation

Install the package via Composer:

composer require hosni/laravel-temporary-urls-minio

Laravel will auto-discover the service provider using Package Discovery. No extra steps, just sit back and relax!

⚙️ Configuration

In your .env file, set your MinIO configuration like a boss:

AWS_ACCESS_KEY_ID=minioadmin
AWS_SECRET_ACCESS_KEY=minioadmin
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=my-bucket

# Internal MinIO endpoint (used for signing)
AWS_ENDPOINT=http://minio:9000

# Publicly accessible endpoint (used in signed URLs)
MINIO_PUBLIC_URL=https://storage.example.com

Next, in config/filesystems.php:

'disks' => [
    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
        'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        'throw' => false,
        'report' => false,

    /**
     * Add this to your s3 disk configuration.
     * This is where the magic happens! 🪄
     */
    'temporary_url' => env('MINIO_PUBLIC_URL'), // 👈 used for rewriting signed URLs
    ],
],

🛠 Usage

Just use Laravel’s filesystem like you always do:

use Illuminate\Support\Facades\Storage;

$url = Storage::disk('minio')->temporaryUrl(
    'uploads/myfile.jpg',
    now()->addMinutes(5)
);

return $url;

👉 The returned $url will always be valid and accessible from your client. You can thank me later! 😎

You can also use this with spatie/laravel-medialibrary:

$mediaItems = $yourModel->getMedia();
$temporaryS3Url = $mediaItems[0]->getTemporaryUrl(now()->addMinutes(5));

No need to install packages like coreproc/laravel-minio-media-library-provider, which take a different (and less effective) approach! Their solution simply returns the MinIO endpoint URL, which is only accessible inside your Docker network, not so helpful if you want to share files with the outside world. 😅

With this package, your temporary URLs are always accessible, inside or outside Docker. No hacks, no headaches, just happy URLs!

🔍 How It Works

  1. Laravel signs the request using your internal MinIO endpoint (AWS_ENDPOINT).
  2. This package swoops in and intercepts the signed URL.
  3. It rewrites the host from the internal endpoint to the public URL you configured (MINIO_PUBLIC_URL).
  4. The result? A valid, signed, publicly accessible temporary URL. 🎉

✅ Example

If your .env looks like this:

AWS_ENDPOINT=http://minio:9000
MINIO_PUBLIC_URL=https://cdn.example.com

Then, when you do:

Storage::disk('minio')->temporaryUrl('photos/pic.jpg', now()->addMinutes(10));

You’ll get something like:

https://cdn.example.com/photos/pic.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...

Instead of:

http://minio:9000/photos/pic.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...

📖 Credits

📝 License

This package is open-sourced software, licensed under the MIT license.