Amazon S3 (Simple Storage Service) is one of the most dependable and scalable cloud storage solutions. It is an excellent option for use when building a Laravel application that requires the ability to upload files (such as images, documents or backups).

When we create web applications today, it is common to have the need to allow users to upload files; using local “storage” to save the uploaded files works fine for small projects, but it quickly becomes insufficient as the application grows. Saving files locally will take up an incredible amount of server disk space and make it much more difficult to load balance.
In the following tutorial, you will see the step by step process to upload files from your Laravel application to a bucket in Amazon S3.
Prerequisites
You will need to have the following before starting this tutorial:
- A running Laravel installation.
- An active AWS Account.
- An S3 Bucket created in your AWS console.
- AWS Access Keys (Access Key ID and Secret Access Key) for an IAM user with S3 permissions.
Steps to Upload Files to Amazon S3 Bucket in Laravel:
Step 1: Install S3 Driver
By default, Laravel creates a composer.json file with the necessary dependencies. However, to use the S3 driver, we need to install the Flysystem S3 package.
Open your terminal and run the following command:
composer require league/flysystem-aws-s3-v3 "^3.0"This package allows Laravel’s filesystem to communicate with Amazon S3.
Step 2: Configure Environment Variables
After you have installed the package, you will want to set your AWS credentials. In the .env file located in your project’s root, find the following keys and replace them with your own credentials for AWS S3.
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=your-bucket-name
AWS_USE_PATH_STYLE_ENDPOINT=falseNote: Make sure to replace your-access-key-id, your-secret-access-key, us-east-1, and your-bucket-name with your actual AWS credentials.
Step 3: Create Controller for File Upload
Next, we will make a controller that will contain the logic for uploading files to S3. You can do this by executing the following command:
php artisan make:controller S3UploadControllerOpen the newly created file at app/Http/Controllers/S3UploadController.php and add the following code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class S3UploadController extends Controller
{
public function uploadForm()
{
return view('s3-upload');
}
public function uploadFile(Request $request)
{
// Validate the file
$request->validate([
'file' => 'required|mimes:jpg,png,pdf,csv|max:2048',
]);
if ($request->hasFile('file')) {
$file = $request->file('file');
// Upload file to S3
$path = Storage::disk('s3')->put('uploads', $file);
// Get the file URL
$url = Storage::disk('s3')->url($path);
return back()->with('success', 'File uploaded successfully to S3!')->with('url', $url);
}
return back()->with('error', 'Error uploading file.');
}
}Explanation:
- We use Storage::disk(‘s3’)->put() to upload the file to the “uploads” folder inside your S3 bucket.
- The url() method retrieves the public URL of the uploaded file (ensure your bucket policy allows public access if you need to display it).
Step 4: Create Routes
Next, we need to define the routes to display the upload form and handle the POST request. Open routes/web.php and add:
use App\Http\Controllers\S3UploadController;
Route::get('s3-upload', [S3UploadController::class, 'uploadForm']);
Route::post('s3-upload', [S3UploadController::class, 'uploadFile'])->name('s3.upload');Step 5: Create the Blade View
Finally, create a simple Blade file to display the upload form. Create a new file resources/views/s3-upload.blade.php and add the following code:
<!DOCTYPE html>
<html>
<head>
<title>Laravel S3 File Upload Example - MageComp</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h2 class="text-center mb-4">Upload File to Amazon S3 in Laravel</h2>
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }} <br>
<strong>File URL:</strong> <a href="{{ session('url') }}" target="_blank">View File</a>
</div>
@endif
<div class="card p-4">
<form action="{{ route('s3.upload') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label class="form-label">Choose File:</label>
<input type="file" name="file" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Upload to S3</button>
</form>
</div>
</div>
</body>
</html>Conclusion
That’s it! You have successfully integrated Amazon S3 with your Laravel application.
Using S3 ensures that your application remains lightweight and your files are stored securely on the cloud. You can now easily handle large file uploads without worrying about local server storage limits.

FAQ
1. Is Amazon S3 free to use?
Amazon S3 is not completely free. AWS provides a free tier with limited storage, but charges apply based on usage, storage size, and data transfer.
2. Can I upload multiple files to S3 in Laravel?
Yes. You can loop through multiple files and upload them using the same Storage::disk(‘s3’) method.
3. Can I use S3 for large file uploads?
Yes! AWS S3 provides a way for you to upload very large files as well as being a great option to use for large audio/video files due to its multipart upload functionality.
4. What is the maximum file size I can upload?
When you upload a single file, the maximum size allowed is 5GB. If you need to upload larger files to AWS S3, then AWS S3 has a multipart upload feature that allows you to break down the file into several smaller parts up to 5 terabytes.



