Posted by Arjun Arjun on Wednesday 19th June 2019, 1 minute read

How to Upload, download, remove Files to Amazon S3 Using Laravel

How to Upload Laravel Laravel 5 Download S3

Create a controller called UploadController.php with following code.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController extends Controller

    public function index()
        $files = Storage::disk('s3')->files('files');

        $data = [];
        foreach($files as $file) {
            $data[] = [
                'name' => basename($file),
                'downloadUrl' => url('/download/'.base64_encode($file)),
                'removeUrl' => url('/remove/'.base64_encode($file)),

        return view('upload', ['files' => $data]);

    public function store(Request $request)
        $this->validate($request, [
            'file' => 'required|max:2048'

        if ($request->hasFile('file')) {
            $file = $request->file('file');
            $name = time() . $file->getClientOriginalName();
            $filePath = 'files/' . $name;
            Storage::disk('s3')->put($filePath, file_get_contents($file));

        return back()->withSuccess('File uploaded successfully');

    public function destroy($file)
        $file = base64_decode($file);
        return back()->withSuccess('File was deleted successfully');

    public function download($file) 
        $file = base64_decode($file);
        $name = basename($file);
        Storage::disk('s3')->download($file, $name);
        return back()->withSuccess('File downloaded successfully');

Create a view file

Create a file called upload.blade.php in the resources/views directory and place below inside it.

<div class="row">
               <h3>Upload File</h3>
               <div class="card col-sm-12">

                       <form action="{{ url('/store') }}" method="POST" enctype="multipart/form-data" class="form-inline">
                           {{ csrf_field() }}

                           <div class="form-group">
                               <label for="file">Select File</label>
                               <input class="form-control-file" type="file" name="file" id="file">

                            <button type="submit" class="btn btn-primary mb-2">Upload</button>

               <div class="row">
               <div class="card col-sm-12">
                   @if (count($files) > 0)
                        @foreach ($files as $file)
                            <a href="{{ url($file['downloadUrl']) }}">{{ $file['name'] }}</a>
                            <form action="{{ url($file['removeUrl']) }}" method="POST">
                                {{ csrf_field() }}
                                {{ method_field('DELETE') }}
                                <button type="submit" class="btn btn-default">Remove</button>
                       <p>Nothing found</p>

Define Routes

Open your routes/web.php directory and add following route definitions to it.

Route::get('/', '[email protected]');
Route::post('/store', '[email protected]');
Route::get('download/{file}','[email protected]');
Route::delete('remove/{file}','[email protected]');

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.