Hey Folks, In this article we will discuss on How we can store the data in AWS S3 Bucket in Laravel. Amazon S3 is a simple storage service which is one of the most powerful cloud storage solutions that allows us to store the data and retrieve the information or data at any amount.
Introduction
So, in this blog, we guide you how to integrate the AWS S3 into your laravel project to store the files or any kind of data in just a few steps. So before proceeding to the next step we will ensure some things at their place.
- Make sure you have an AWS account with the access of the S3 Bucket.
- You have successfully installed the Laravel application with the version 10 (recommended)
- Also composer is already installed in your system.
So, these are the basic things that need to be in your system and their places. So let’s go with the steps to integrate the AWS S3 in your laravel application.
Step 1 : Need to Install AWS SDK for PHP
In your Laravel Project, Laravel provides the built-in feature for the Amazon AWS S3 via the leage/flysystem-aws-s3-v3 package. Make sure you installed it. If not installed yet then please run the following command that is given below.
composer require league/flysystem-aws-s3-v3
Step 2 : Configure the AWS S3 Bucket in Laravel
When you installed the package, Now you need to configure your aws credentials in your laravel projects. So for that you need to add those credentials in your laravel application.
1) Add AWS Credentials in laravel backend .env file : Please open your laravel .env file and add the following detail in that file. Make sure all data is correct.
AWS_ACCESS_KEY_ID=your_aws_access_key
AWS_SECRET_ACCESS_KEY=your_aws_secret_key
AWS_DEFAULT_REGION=your_region (e.g. us-east-1)
AWS_BUCKET=your_bucket_name
AWS_URL=your_s3_bucket_url
2) Update config/filesystems.php : As you already know that laravel provides the built-in feature for the multiple storage disks. So, please update the filesystems.php config file to include the S3.
'filesystems' => [
'disks' => [
'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 3 : Upload Files to AWS S3 Bucket
use Illuminate\Support\Facades\Storage;
public function uploadFile(Request $request)
{
$file = $request->file('file');
$path = Storage::disk('s3')->put('uploads', $file);
$url = Storage::disk('s3')->url($path);
return response()->json(['file_url' => $url]);
}
Cool, Most of the stuff you already completed. Now there is time to upload the files to the AWS S3 Bucket. So you can store the data in AWS S3 bucket laravel using the Laravel’s Storage facade.
Now lets see some examples of how to retrieve the file from aws s3 buket, Getting the All files from s3 bucket laravel, Getting file from s3 bucket laravel and deleting the file from s3.
Retrieving the Files : Use this PHP code to retrieve the File from AWS S3 Bucket.
$fileUrl = Storage::disk('s3')->url('uploads/example.jpg');
Get All Files from S3 Bucket Laravel :
$files = Storage::disk('s3')->files('uploads');
Get Files from S3 Bucket Laravel:
$fileContents = Storage::disk('s3')->get('uploads/example.jpg');
Deleting a File : By using the following code you can easily delete the specific files from the AWS S3 bucket.
Storage::disk('s3')->delete('uploads/example.jpg');
Step 4 : Testing the Integration
<form action="/upload" method="post" enctype="multipart/form-data">
@csrf
<input type="file" name="file">
<button type="submit">Upload</button>
</form>
Now, it’s time to start the integration of AWS S3 Bucket with Laravel. You can test it by uploading the file via a form in your laravel application and verify them in your aws s3 bucket.
Conclusion
As we covered the many topics in this article that how to integrate the AWS S3 bucket with Laravel that allow you to store and manage the files in the cloud or AWS. So please follow the steps that I shared above. You can use this power of amazon s3 to improve the enhancement of your application’s file storage capabilities.
If you are facing any kind of errors or issue then please connect with us or you can check the Laravel S3 Configuration for further troubleshooting. If you like this article then please share this with your coding teammates.