Laravel Download File From AWS s3 Bucket Example

Laravel download file from aws s3 bucket to local storage; Through this tutorial, i am going to show you how to download and store file from aws s3 bucket to local storage in laravel apps.

Laravel download file from AWS S3 Bucket

Follow the following steps to download and store file from aws s3 bucket to local storage in laravel apps; is as follows:

  • 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 Download Route
  • Step 6 – Create File Download Controller
  • Step 7 – Start Development Server

Step 1 – Install Laravel App

Run the following command on command prompt to download fresh new laravel setup; is as follows:

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

Step 2 – Setup amazon s3 bucket

Setup amazon aws s3 bucket account; so you need to create account on amazon s3 to store our images/files. First, you need to sign up for Amazon.

Use this link signup to signup in amazon aws s3 and get key and secret.

Step 3 – Setup amazon s3 Cloud Storage Credentials

Setup amazon s3 cloud storage credentials; so you 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 

Step 4 – Install s3 package

To use the following command on command prompt to install aws s3 package; is as follows:

composer require league/flysystem-aws-s3-v3

Step 5 – Create File Download Route

Go to app/routes/web.php file and create two below routes here; as follows:

<?php
 
   
 
use Illuminate\Support\Facades\Route;
 
   
 
use App\Http\Controllers\FileDownloadController;
 
   
 
   
 
Route::get('download', [ FileDownloadController::class, 'index' ]);

Step 6 – Create File Download Controller

Run the following command on command prompt to create FileDownloadController; is as follows:

php artisan make:controller ImageController

Then visit to app/http/controllers/FileDownloadController.php and add the below code into it: is as follows:

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Storage;
use App\Models\Image;
 
class FileDownloadController extends Controller
{
 
    public function index(Request $request) 
    {
        $id = 1;
        $attachment = Image::find($id);
 
        $headers = [
 
            'Content-Type'        => 'application/jpeg',
 
            'Content-Disposition' => 'attachment; filename="'. $attachment->name .'"',
 
        ];
 
        return \Response::make(Storage::disk('s3')->get($attachment->url), 200, $headers);
    }
}

Step 7 – Start Development Server

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

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.