Laravel File Upload S3

Laravel 8 S3 File Upload Tutorial With Example

Laravel 8 S3 File Upload Tutorial With Example

In this post we will give you Laravel 8 S3 File Upload Tutorial With Example, hear for Laravel 8 S3 File Upload Tutorial With Example we will give you details about it.

here we step by step explain to you how to upload images in the s3. you can also upload files in s3 using laravel but in this article, we will upload the images in the s3 using laravel.

So you can follow the below steps for upload image in s3 using laravel 8.

Step 1: Install Laravel 7

We are going to install laravel 8, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.

composer create-project --prefer-dist laravel/laravel laravel_s3_image

Step 2: Install S3 Package

Now, We will install an amazon s3 package using the below command.

 
composer require league/flysystem-aws-s3-v3

Step 3: Setup amazon Bucket

here in this step, you have to login or sign up on your AWS console https://console.aws.Alpha XR. after then you can create a bucket. so you can follow the URL for Create an Amazon S3 Bucket.

Step 4: Setup Amazon S3 Cloud Storage Credentials

After the complete installation of the amazon s3 package. we have to API credentials configuration. now we will open the .env file and add API credentials. See below changes in a .env file.

AWS_ACCESS_KEY_ID=Access Key Id
AWS_SECRET_ACCESS_KEY=Secret Access Key
AWS_DEFAULT_REGION=Bucket Region
AWS_BUCKET=Bucket Name

Step 5: Create Controller
Here below command help to create the controller.

php artisan make:controller ImageUploadController

app/Http/ImageUpload.php

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class ImageuploadController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function image_upload()
    {
        return view('image_upload');
    }
  
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function upload_post_image(Request $request)
    {
        $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
        if($request->hasfile('image'))
        {
            $file = $request->file('image');
            $imageName=time().$file->getClientOriginalName();
            $filePath = 'images/' . $imageName;
            Storage::disk('s3')->put($filePath, file_get_contents($file));
	    return back()->with('success','You have successfully upload image.');
        }   
    }
}
?>

Step 6: Create Route
Add the following route code in the “routes/web.php” file.

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/ 
 
Route::get('image-upload', 'ImageuploadController@image_upload')->name('image.upload');
Route::post('image-upload', 'ImageuploadController@upload_post_image')->name('upload.post.image');
?>

Step 7: Create Blade Files

So finally, first we will open the image-upload.blade.php file and then paste the following code.
resources/views/image-upload.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Laravel 8 S3 Image Upload tutorial example - XpertPhp</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1https://www.onlinecode.org/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="row">
			<div class="col-lg-12">
				@if ($message = Session::get('success'))
				<div class="alert alert-success alert-block">
					<button type="button" class="close" data-dismiss="alert">×</button>
						<strong>{{ $message }}</strong>
				</div>
				@endif
				@if (count($errors) > 0)
					<div class="alert alert-danger">
						<strong>Whoops!</strong> There were some problems with your input.
						<ul>
							@foreach ($errors->all() as $error)
								<li>{{ $error }}</li>
							@endforeach
						</ul>
					</div>
				@endif
			</div>
		</div>	
  
        <form action="{{ route('upload.post.image') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="row">  
                <div class="col-md-6">
                    <input type="file" name="image" class="form-control">
                </div>
   
                <div class="col-md-6">
                    <button type="submit" class="btn btn-success">Upload</button>
                </div>
            </div>
        </form>
	</div>
</body>
</html>

Step 8: Run Our Laravel Application

We can start the server and run this example using the below command.

php artisan serve

Now we will run our example using the below Url in the browser.

http://127.0.0.1:8000/image-upload

Hope this code and post will helped you for implement Laravel 8 S3 File Upload Tutorial With Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs https://onlinecode.org