Integrate amazon s3 to laravel project

193 0 0 0

Last Updated : 2022-10-07 15:27:15

In this snippet I will teach you how to integrate amazon s3 with your laravel project

Hello guys, 


In this snippet I will teach you how to integrate amazon s3 to your laravel project .


First: Things you need to know 


what is AWS S3

AWS S3 meaning simple storage service and it is a driver that makes it easy for you to store your uploaded files on it and you can get and display it whenever you want. you have to know also that AWS S3 depends on buckets. Think of these buckets as small storage spaces or containers located in your AWS service


Second: What is required for this operation


To integrate AWS S3 with your laravel project you must have three things like below 


AWS ACCOUNT: an account in which you can create your buckets and start the storage process 


AWS USER: which you can get access key ID and secret access key to link your laravel project with AWS


LARAVEL PROJECT: in which you can upload and display all your files from AWS S3


Now, Let's jump into these processes


First [AWS ACCOUNT]


1- go to https://aws.amazon.com/ and start create your own account then login to your account.


2- after login to your account choose S3 services from the services list.


3- start adding new bucket with a unique name for each bucket and add any previliges you want to that bucket.


for now you bucket is ready to accept your uploads effectively 


Second [AWS USER]


Now let's create an authorized user to get access key ID and secret key ID to link laravel project with AWS S3


1- from services list select IAM service 


2- from the left sidebar choose Access Management tab then choose users 


3- start adding new user with all previliges you want and select Access key - Programmatic access type to get your key IDs


4- select next: permissions button then from set permissions tab choose attack existing policies directly then search for AmazonS3FullAccess permission and assign it to that user then next button 


5- you can add tags to that user if you want (it's optional by the way) and then next button and finally create user button.


6- at this stage your user is ready and you have to copy that user access key ID and secret access key (we will need them later)


Third [LARAVEL PROJECT]


1- after creating you project, we will need to install specific package to integrate with AWS S3 using the code below


composer require --with-all-dependencies league/flysystem-aws-s3-v3

2- now add your AWS S3 credentials in .env file like this


AWS_ACCESS_KEY_ID=your user access key
AWS_SECRET_ACCESS_KEY=your user secret key
AWS_DEFAULT_REGION=your bucket region
AWS_BUCKET=your bucket name
AWS_USE_PATH_STYLE_ENDPOINT=false

3- now it's ready to store and access any file you want to AWS S3, you can store data to AWS S3 using the code below


Storage::disk('s3')->put($path, $image);

4- you can display your files in blade files from AWS S3 like his


{{ Storage::disk('s3')->temporaryUrl($item->path, now()->addMinutes(30)) }}

// LIKE THIS
<img src="{{ Storage::disk('s3')->temporaryUrl($item->path, now()->addMinutes(30)) }}" alt="Car Image" width="120" height="120" class="rounded-circle">

I Hope you like it


 

Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.