How to Make MinIO temporaryUrl()
Work in Laravel
Laravel’s Storage::disk('s3')->temporaryUrl()
method generates signed URLs for private files stored in MinIO, an S3-compatible object storage. When running MinIO inside Docker, you need a proper setup to ensure the signed URLs work seamlessly.
This guide provides a minimal and effective configuration for integrating Laravel with MinIO.
🔹 Step 1: Set a Stable Container Name
Modify your MinIO service in docker-compose.yml
to set a fixed container name:
minio:
image: "minio/minio:latest"
ports:
- "9000:9000"
- "8900:8900"
container_name: dev.minio.example.com
environment:
MINIO_ROOT_USER: "example"
MINIO_ROOT_PASSWORD: "password"
volumes:
- "example-minio:/data/minio"
networks:
- example-network
command: minio server /data/minio --console-address ":8900"
Why does this work?
- Setting
container_name: dev.minio.example.com
ensures that Laravel can always reach MinIO inside Docker. - This setup eliminates the need for
VIRTUAL_HOST
,VIRTUAL_PORT
, or network aliases.
🔹 Step 2: Update /etc/hosts
for Local Access
On your local machine, add the following entry to /etc/hosts
(Linux/macOS) or C:\Windows\System32\drivers\etc\hosts
(Windows):
127.0.0.1 dev.minio.example.com
This entry ensures that when you access http://dev.minio.example.com
, it correctly points to your local MinIO instance.
🔹 Step 3: Generate MinIO Credentials
Instead of using the default credentials, generate a new access key from the MinIO console (default URL: http://localhost:8900
):
- Open the MinIO Console: http://localhost:8900
- Log in with:
- Username:
example
- Password:
password
- Username:
- Navigate to Identity > Users.
- Click “Create User” and generate:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- Assign read and write policies to the user.
🔹 Step 4: Configure Laravel
Update your .env
file with the new MinIO credentials and the correct AWS_URL
:
AWS_ACCESS_KEY_ID=your_generated_access_key
AWS_SECRET_ACCESS_KEY=your_generated_secret_key
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=test-storage
AWS_URL=http://dev.marketplace-api.example.com:9000/test-storage
AWS_ENDPOINT=http://dev.minio.example.com:9000
AWS_USE_PATH_STYLE_ENDPOINT=true
Explanation:
Field | Description |
---|---|
AWS_URL | Your Laravel app’s URL followed by :minio-port/bucketname . |
AWS_ENDPOINT | Points to the MinIO container name with port 9000 . |
AWS_ACCESS_KEY_ID & AWS_SECRET_ACCESS_KEY | Credentials generated in MinIO Console. |
After updating the .env
file, clear Laravel’s configuration and cache:
php artisan config:clear
php artisan cache:clear
🔹 Step 5: Generate a Temporary URL in Laravel
Now you can generate a temporary signed URL in Laravel:
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
$filePath = 'documents/sample.pdf';
$expiration = Carbon::now()->addMinutes(60); // URL expires in 1 hour
$url = Storage::disk('s3')->temporaryUrl($filePath, $expiration);
return response()->json(['temporary_url' => $url]);
🎯 Summary
✔ Set a fixed MinIO container name in docker-compose.yml
✔ Update /etc/hosts
to map MinIO to 127.0.0.1
✔ Generate AWS credentials from MinIO console (localhost:8900
)
✔ Set AWS_URL
as your_application_url:minioport/bucketname
✔ Use Storage::disk('s3')->temporaryUrl()
to generate signed URLs
Your Laravel app is now fully integrated with MinIO for temporary signed URLs! This setup not only simplifies your configuration but also paves the way for a robust, scalable storage solution. Keep pushing forward, and happy coding! 🚀🚀