Skip to main content

How to Upload and Store Files on AWS S3 in Laravel

How to Upload and Store Files on AWS S3 in Laravel

Uploading files to AWS S3 is a common requirement in modern Laravel applications. Amazon S3 provides scalable, secure, and cost-effective cloud storage for files such as images, documents, and media assets.

Prerequisites

  • A Laravel application
  • An AWS account
  • An S3 bucket created in AWS

Step 1: Install AWS S3 Driver

Install the Flysystem AWS S3 package using Composer:

composer require league/flysystem-aws-s3-v3 "^3.0"

Step 2: Configure AWS Credentials

Add the following configuration values to your .env file:

AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=your_bucket_name
AWS_USE_PATH_STYLE_ENDPOINT=false
Tip: Always keep your AWS credentials private and never commit them to GitHub.

Step 3: Configure the S3 Filesystem Disk

Open the config/filesystems.php file and make sure the S3 disk is configured correctly:

'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'),
],

Step 4: Create a File Upload Form

Create a simple Blade form for uploading files:

<form action="{{ route('upload') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <input type="file" name="file" required>
    <button type="submit">Upload</button>
</form>

Step 5: Handle File Upload in Controller

Create a controller method to store the uploaded file in your S3 bucket:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

public function upload(Request $request)
{
    $request->validate([
        'file' => 'required|file|max:2048'
    ]);

    $path = $request->file('file')->store('uploads', 's3');

    return response()->json([
        'message' => 'File uploaded successfully',
        'path' => $path,
        'url' => Storage::disk('s3')->url($path)
    ]);
}

Step 6: Define the Route

Add the upload route in routes/web.php:

use App\Http\Controllers\FileController;

Route::post('/upload', [FileController::class, 'upload'])->name('upload');

Step 7: Make Files Public (Optional)

If you want uploaded files to be publicly accessible, set their visibility:

Storage::disk('s3')->setVisibility($path, 'public');

Common Issues

  • Incorrect AWS credentials
  • S3 bucket permission issues
  • Incorrect AWS region
Pro Tip: Use IAM roles and restrictive policies to improve security when working with S3.

Conclusion

Uploading and storing files on AWS S3 in Laravel is simple using Laravel’s filesystem abstraction. With the correct configuration and security practices, S3 becomes a powerful storage solution for scalable applications.

Comments

Popular posts from this blog

How to Use L5-Swagger for API Documentation in Laravel

Integrating Swagger in Laravel: Annotations, JSON, and YAML What is Swagger? Swagger (OpenAPI) is a powerful tool for generating interactive API documentation. It helps developers understand and test your API easily. ✅ Step-by-Step Guide to Setup L5-Swagger 1. Install L5-Swagger Package composer require "darkaonline/l5-swagger" 2. Publish Config & View Files This command publishes the config file to config/l5-swagger.php : php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider" 3. Configure Swagger (Optional) Edit the file config/l5-swagger.php to update: API Title Base Path Directories to scan for annotations 4. Add Swagger Annotations Add these before your controller class: /** * @OA\Info( * version="1.0.0", * title="Your API Title", * description=...

How to Send Emails in Node.js using Nodemailer and Ethereal

How to Send Email in Node.js using Nodemailer Email functionality is essential in modern web applications. Whether you're sending confirmation emails, password resets, or notifications, Node.js with Nodemailer makes this simple. In this blog, we'll walk through setting up email sending using Node.js , Express , and Ethereal Email for testing. ๐Ÿงพ Prerequisites Node.js installed Basic knowledge of Express.js Internet connection ๐Ÿ“ Project Structure project-folder/ ├── index.js ├── .env ├── package.json └── app/ └── controller/ └── emailSendController.js ๐Ÿ“ฆ Step 1: Install Dependencies npm init -y npm install express nodemailer dotenv npm install --save-dev nodemon ๐Ÿ” Configure nodemon (Optional but Recommended) Update your package.json with a custom start script: "scripts": { "start": "nodemon index.js" } ๐Ÿ” Step 2: Create a .env File Create a .env file a...

How to Integrate RevenueCat Webhook in Laravel

How to Integrate RevenueCat Webhook in Laravel If you're using RevenueCat to manage in-app subscriptions for your mobile app (iOS, Android, etc.), it’s important to connect it with your backend. Laravel makes it easy to handle incoming webhooks for real-time events like purchases, renewals, cancellations, and more. ๐Ÿ“Œ What is RevenueCat? RevenueCat is a powerful subscription management platform for mobile apps. It simplifies in-app purchases across iOS, Android, and Stripe. With webhooks, it notifies your server in real-time when important events happen, such as a new purchase or a cancellation. ๐Ÿ”— Webhook Events from RevenueCat Common webhook events include: INITIAL_PURCHASE RENEWAL CANCELLATION BILLING_ISSUE EXPIRATION SUBSCRIBER_ALIAS ๐Ÿ”— For a full list of event payloads, visit: RevenueCat Sample Events ✅ Step-by-Step Guide to Integration 1. Create the Route // routes/ap...