Upload Files/Images to Amazon s3 Cloud Using Laravel 7, 6 Filesystem

Upload Files/Images to Amazon s3 Cloud Using Laravel 7, 6 Filesystem

How to Upload Files on Amazon s3 Cloud using Laravel Filesystem. In this tutorial we would love to share with you, how you can easily upload files or image on amazon s3 cloud storage.

Here we will also learn how to create an account, create buckets and policies on amazon s3 cloud storage. Where you can find credentials.

In this tutorial, We will explain to you each things about upload images or files on amazon s3 cloud storage.

How to upload file to s3 using laravel filesystem?

Follow the below-given steps, you can upload files or images on amazon s3 cloud storage using laravel 6 filesystem:

  • Install Laravel App
  • Setup amazon s3 bucket
  • Setup amazon s3 Cloud Storage Credentials
  • Install s3 package
  • Create Route
  • Generate a Controller
  • Create a View File
  • Start Development Server

Install Laravel App

First of all, we need to install the laravel fresh application. Use the below command and download fresh new laravel setup :

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

Setup amazon s3 bucket

Now We need to create account on amazon s3 to store our images/files. First, we need to sign up for Amazon.

You should follow this link to signup.  After successfully signing you can create your bucket. You can see the below image for better understanding.

create bucket account on amazon s3 cloud storage
create bucket account on amazon s3 cloud storage

Now we need to create a bucket policy, so we need to go to this link. And the page looks like this.

You can see the page looks like this.

You have created a bucket policy, Then copy-paste into a bucket policy. You can see the below image.

amazon s3 bucket policy

Now we will go here to get our Access Key Id and Secret Access Key. and put the credentials into the laravel project .env file.

Setup amazon s3 Cloud Storage Credentials

Next, we need to put the API Key and Secret Key in .env file. You can add the following field in your .env file

AWS_ACCESS_KEY_ID=xxxxx 
AWS_SECRET_ACCESS_KEY=xxxx 
AWS_DEFAULT_REGION=ap-south-1 
AWS_BUCKET=laravelimage 

Install s3 package

In this step, we need to install s3 package via the Composer package manager in laravel project.

composer require league/flysystem-aws-s3-v3

Create Route

We will create two routes in web.php file. Go to app/routes/web.php file and create two below routes here :

 Route::get('image', 'ImageController@index');
 Route::post('store', 'ImageController@store');

Generate Controller

We need to create a controller name ImageController. Use the below command and create Controller :

php artisan make:controller ImageController

After successfully create controller go to app/controllers/ImageController.php and put the below code :

<?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');
         }
    }
}

Create Blade view

In this step, we need to create blade view file. Go to app/resources/views and create one file name image.blade.php :

<!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>

Start Development Server

We need to start the development server. Use the PHP artisan serve command and start your server :

 php artisan serve
If you want to run the project diffrent port so use this below command
php artisan serve --port=8080

Now we are ready to run our example so run bellow command to quick run.

 http://localhost:8000/image
 Or direct hit in your browser
 http://localhost/laravelS3/public/image

Conclusion

In this tutorial, We have successfully uploaded images or files on amazon s3 cloud storage using laravel filesystem.

You may like

  1. Laravel 6 CKEditor with Image Upload
  2. Laravel 6 Ajax Image Upload Tutorial Example From Scratch
  3. Laravel 6 Intervention Upload Image Using Ajax – Example
  4. Laravel 6 Upload Image to Database with Validation

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

One reply to Upload Files/Images to Amazon s3 Cloud Using Laravel 7, 6 Filesystem

  1. Thanks a lot this work for me!! maybe you can show how to delete this image !!^^

Leave a Reply

Your email address will not be published. Required fields are marked *