Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.
Amazon Web Services (AWS) Simple Storage Service (S3) is a strong and dependable option for file management and storage. Easily integrate S3 for file storage with Laravel, one of the most popular PHP frameworks. Even if you're not familiar with using AWS S3 in Laravel, this blog will walk you through the process step by step.
 

What is AWS S3?

 

AWS S3 is a cloud storage service provided by Amazon Web Services (AWS). It offers object storage with a simple web interface to store and retrieve any amount of data at any time.
S3 is designed to deliver durability, making it an ideal choice for storing critical data and backups.
 

Why Use AWS S3 in Laravel?

 

Laravel, a popular PHP framework, integrates well with AWS S3 to handle file uploads and storage. Storing files locally may not be efficient in large-scale applications, and AWS S3 offers advantages over local storage

-  Scalability: As your app grows, S3 scales effortlessly.
-  Security: AWS provides strong encryption and access control features.
-  Backup & Recovery: S3 offers easy backup and recovery options.
-  Cost-Effective: You only pay for what you store, making it affordable for projects of all sizes.
 

Configuring AWS S3 Storage in Laravel

 

Step 1: Install AWS SDK in Laravel

 

First, make sure your Laravel app is set up and running. To use S3, we need to install the AWS SDK (Software Development Kit). Luckily, Laravel makes it easy. Run the following command in your terminal:
 
composer require league/flysystem-aws-s3-v3
This installs the necessary package to work with AWS S3 in Laravel.
 

Step 2: Set Up the AWS S3 Account

 

If you don’t already have an AWS account, you’ll need to create one at https://aws.amazon.com. Once logged in:
 
1. Search for "S3" in the services menu.
2. Create a new S3 bucket. A bucket is simply a container to hold your files.
3. Note down your Bucket Name, Access Key, and Secret Key—you’ll need these later.
 

Step 3: Configure Your Laravel App for S3

 

Laravel’s configuration files are where you connect the app to S3. Open your .env file and edit the value of following lines:
 
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
AWS_DEFAULT_REGION=your-region
AWS_BUCKET=your-bucket-name
 
Make sure to replace your-access-key, your-secret-key, your-region, and your-bucket-name with the details from your AWS account. The AWS_DEFAULT_REGION is usually something like us-east-1 or eu-west-1.
 

Basic usage with AWS S3

 

Upload Files to S3

 

With the setup complete, you can now upload files to your S3 bucket. Laravel’s file storage functions work seamlessly with S3.
Here’s an example of how to upload a file:
 
use Illuminate\Support\Facades\Storage;
public function uploadFile(Request $request)
{
    if ($request->hasFile('file')) {
        $file = $request->file('file');
        $path = $file->store('uploads', 's3'); // 'uploads' is the folder in your bucket
    
        // Make the file publicly accessible
        Storage::disk('s3')->setVisibility($path, 'public');

 

        return "File uploaded to: " . $path;
    }
    return "No file selected.";
}
 
In this example, we check if the file exists, then upload it to the uploads folder in the S3 bucket. The setVisibility method ensures the file is publicly accessible.
 

Access Files from S3

 

To access the uploaded files, you can generate URLs directly from the S3 bucket. Here’s how:
 
$url = Storage::disk('s3')->url('uploads/your-file-name.jpg');
return $url;
 
This gives you the file's URL so you can display it in your app.
 

Deleting Files from S3

 

Deleting files from S3 is just as easy as uploading. You can remove a file using:
 
Storage::disk('s3')->delete('uploads/your-file-name.jpg');
 

Advanced Features (Optional)

 

S3 offers many advanced features, but you don’t have to use them right away. As you get more comfortable, you can explore options like:
-  Versioning: Keep track of file versions.
-  Access Control: Set up more detailed access permissions for your files.
-  Lifecycle Rules: Automatically delete or archive files after a certain period.
 

Conclusion

 

Integrating AWS S3 with Laravel can significantly enhance your file storage capabilities, giving your application scalability, security, and cost-efficiency. Following this guide, you can easily upload, access, and manage files on AWS S3 from your Laravel application.
With AWS S3, you no longer have to worry about running out of storage space or managing complex file systems. You can focus on building your application while AWS takes care of the rest.
 

Reference

Written by
Author Avatar
Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

COMPANY PROFILE

Please check out our Company Profile.

Download

COMPANY PORTFOLIO

Explore my work!

Download

ASK ISB Vietnam ABOUT DEVELOPMENT

Let's talk about your project!

Contact US