In this tutorial you will learn about the Laravel 8 File Image Upload to AWS S3 Cloud Bucket and its application with practical example.
In this Laravel 8 File Image Upload to AWS S3 Cloud Bucket tutorial I will show you how to Upload Files on Amazon s3 bucket using Laravel Filesystem. In this tutorial you will learn to Upload Files on Amazon s3 bucket in laravel. In this article I will share example to upload files or images on amazon s3 bucket cloud storage. You will also learn to integrate amazon s3 bucket in laravel for image or file upload. As well as I will also guide you through process to use amazon s3 bucket cloud storage in laravel application to upload and store files or images.
Laravel 8 File Image Upload to AWS S3 Cloud Bucket
In this step by step tutorial I will demonstrate you with example to upload files or images on amazon s3 bucket cloud storage. Please follow the instruction given below:
- Step 1 – Install Laravel App
- Step 2 – Setup amazon s3 bucket
- Step 3 – Setup amazon s3 Cloud Storage Credentials
- Step 4 – Install s3 package
- Step 5 – Create File Upload Route
- Step 6 – Create File Upload Controller
- Step 7 – Create a View File
- Step 8 – Start Development Server
Step 1 – Install Laravel App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel laravelS3 |
Step 2 – Setup amazon s3 bucket
First of all you need to sign up for AWS services follow this link to signup. After successfully signing up you can create your bucket.
Step 3 – Setup amazon s3 Cloud Storage Credentials
After signing up you are required to create your bucket using amazon s3 cloud storage services. After creating amazon s3 bucket so you need to put the API Key and Secret Key in .env file.
1 2 3 4 |
AWS_ACCESS_KEY_ID=xxxxx AWS_SECRET_ACCESS_KEY=xxxx AWS_DEFAULT_REGION=ap-south-1 AWS_BUCKET=laravelimage |
Step 4 – Install s3 package
In this step, we will install s3 Package via the composer dependency manager. Use the following command to install s3 Package.
1 |
composer require league/flysystem-aws-s3-v3 |
Step 5 – Create File Upload Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ImageController; Route::get('image', [ ImageController::class, 'index' ]); Route::post('store', [ ImageController::class, 'store' ]); |
Step 6 – Create File Upload Controller
Now, lets create a controller named ImageController using command given below –
1 |
php artisan make:controller ImageController |
Once the above command executed, it will create a controller file ImageController.php in app/Http/Controllers/ directory. Open the ImageController.php file and put the following code in it.
app/Http/Controllers/ImageController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Storage; class ImageController extends Controller { public function index() { return view('image'); } public function store(Request $request) { $this->validate($request, ['image' => 'required|image']); if($request->hasfile('image')) { $file = $request->file('image'); $name=time().$file->getClientOriginalName(); $filePath = 'images/' . $name; Storage::disk('s3')->put($filePath, file_get_contents($file)); return back()->with('success','Image Uploaded successfully'); } } } |
Step 7 – Create Blade view
Now we will create blade view file. Go to app/resources/views and create a file image.blade.php
image.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Upload Image to Amazon s3 cloud Storage Using laravel</title> <link rel="stylesheet" href="{{asset('css/app.css')}}"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Laravel Upload Image to Amazon s3 cloud Storage Tutorial</h2><br/> @if (\Session::has('success')) <div class="alert alert-success"> <p>{{ \Session::get('success') }}</p> </div><br /> @endif @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form method="post" action="{{url('store')}}" enctype="multipart/form-data"> @csrf <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <input type="file" name="image"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <button type="submit" class="btn btn-success">Upload</button> </div> </div> </form> </div> </body> </html> |
Step 8 – Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 2 3 |
http://localhost:8000/image Or direct hit in your browser http://localhost/laravelS3/public/image |