Upload images to your S3 instance using Laravel

By Santanu Aich

Upload images to your S3 instance using Laravel

S3 is a package of Amazon Web Services(AWS), where we can store a large number of files. So, basically, it is a storage device.

It has one big advantage. You can configure the permission access of files from your AWS Console.

Configure S3 with Laravel:

<> open the link https://aws.amazon.com/console

<> hover your mouse on My Account- AWS Management Console

<> create your account or if you have an account then log in here

<> move to storage and click on S3

<> click on create a bucket and make a bucket for your project. Here you need to put some values like Bucket Name, Region, etc.

Open your Laravel project. You have a .env file in the root folder. Open it and put the below code in your .env file.





*** you get all the credential when you create a Bucket in AWS console

Move to /your-laravel-project/config/filesystems.php and write down the below code

‘s3’ => [
‘driver’ => ‘s3’,
‘key’ => env(‘AWS_ACCESS_KEY_ID’),
‘secret’ => env(‘AWS_SECRET_ACCESS_KEY’),
‘region’ => env(‘AWS_DEFAULT_REGION’),
‘bucket’ => env(‘AWS_BUCKET’),


For uploading the file, create a new controller in your laravel project. Please check the below code, here I have commented on the process.


namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use League\Flysystem\AwsS3v3\AwsS3Adapter;   //  link aws adapter with laravel
use Illuminate\Support\Facades\Storage;              // link with S3 storage
use Illuminate\Contracts\Filesystem\Filesystem;class AwsController extends Controller {
public function index(Request $request){                                           if ($request->hasFile(‘filedoc’)) {                                                                                                                                           $filenamewithextension = $request->all()[‘filedoc’]->getClientOriginalName();
                                               //get filename without extension
                                               $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
                                                //get file extension
                                               $extension = $request->all()[‘filedoc’]->getClientOriginalExtension();
                                               //filename to store
                                             $filenametostore = $filename.’_’.time().’.’.$extension;
                                              //Upload File to s3
                                Storage::disk(‘s3’)->put($filenametostore, fopen($request->all()[‘filedoc’], ‘r+’), ‘public’);
                                            //echo $url = Storage::disk(‘s3’)->url($request->all()[‘filedoc’]);
                                          echo Storage::disk(‘s3’)->url($filenametostore);



Hope it will work for you.

Santanu Aich Author
Senior Software Engineer , Openweb Solutions

Senior Software Engineer at Openweb Solutions

Posts created 6

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top