0
0
LaravelHow-ToBeginner · 4 min read

How to Use S3 Storage in Laravel: Simple Setup and Example

To use S3 storage in Laravel, configure your .env file with your AWS credentials and set the FILESYSTEM_DRIVER to s3. Then use Laravel's Storage facade to upload, retrieve, or delete files on S3 seamlessly.
📐

Syntax

Laravel uses the Storage facade to interact with filesystems like S3. You specify the disk as s3 and call methods like put, get, or delete.

Example method calls:

  • Storage::disk('s3')->put('filename.txt', 'contents'); - Upload a file.
  • Storage::disk('s3')->get('filename.txt'); - Retrieve file contents.
  • Storage::disk('s3')->delete('filename.txt'); - Delete a file.

Before using, configure AWS credentials and bucket in config/filesystems.php and your .env file.

php
<?php
use Illuminate\Support\Facades\Storage;

// Upload a file to S3
Storage::disk('s3')->put('folder/file.txt', 'File contents here');

// Get file contents from S3
$content = Storage::disk('s3')->get('folder/file.txt');

// Delete a file from S3
Storage::disk('s3')->delete('folder/file.txt');
💻

Example

This example shows how to upload a text file to S3 and then retrieve its contents using Laravel's Storage facade.

php
<?php
namespace App\Http\Controllers;

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

class S3ExampleController extends Controller
{
    public function uploadAndRead()
    {
        // Upload a file to S3
        Storage::disk('s3')->put('example-folder/hello.txt', 'Hello from Laravel S3!');

        // Read the file back
        $contents = Storage::disk('s3')->get('example-folder/hello.txt');

        return response()->json(['file_contents' => $contents]);
    }
}
Output
{"file_contents":"Hello from Laravel S3!"}
⚠️

Common Pitfalls

  • Missing AWS credentials: Forgetting to set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION in your .env causes authentication errors.
  • Wrong bucket name: Ensure AWS_BUCKET matches your actual S3 bucket name.
  • Filesystem driver not set: Set FILESYSTEM_DRIVER=s3 in .env or specify disk('s3') explicitly.
  • Permissions: Your AWS IAM user must have proper S3 permissions to read/write/delete files.
php
<?php
// Wrong way: Missing disk specification
Storage::put('file.txt', 'data'); // Uses default disk, not S3

// Right way: Specify S3 disk
Storage::disk('s3')->put('file.txt', 'data');
📊

Quick Reference

ActionCode Example
Upload fileStorage::disk('s3')->put('path/file.txt', 'contents');
Get file contentsStorage::disk('s3')->get('path/file.txt');
Delete fileStorage::disk('s3')->delete('path/file.txt');
Check if file existsStorage::disk('s3')->exists('path/file.txt');
Get file URLStorage::disk('s3')->url('path/file.txt');

Key Takeaways

Set AWS credentials and bucket info in your .env file before using S3 in Laravel.
Use Storage::disk('s3') to interact with files on Amazon S3.
Always verify your IAM permissions allow S3 file operations.
Specify the S3 disk explicitly if your default filesystem is not S3.
Use Laravel's Storage facade methods like put, get, and delete for easy file management.