How to Setup s3 in Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023

categoryLaravel

In this tutorial we will learn how to setup s3 in laravel.

 

How to setup s3 in laravel

To set up Amazon S3 in Laravel, you need to follow these steps:

 

Step 1 - Install the AWS SDK for PHP using Composer by running the following command in your Laravel project directory:

composer require aws/aws-sdk-php

 

Step 2 - Add your AWS credentials to your Laravel .env file:

AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_DEFAULT_REGION=your_region
AWS_BUCKET=your_bucket_name

 

Step 3 - Create a new S3 disk configuration in your Laravel config/filesystems.php file:

'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 4 - You can now use the Storage facade to interact with S3. For example, to upload a file to S3, you can use the putFile method:

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

This will upload the file to the specified S3 bucket in the specified folder.

 

That's it! You should now be able to use S3 to store and retrieve files in your Laravel application.

Comments 0

Leave a comment