How to Upload files to S3 with Laravel

When you’re developing a Laravel project, you typically upload files to your local Storage folder or public folder. However there are some case that you have to use a remote file server that is dedicated for file hosting. So, the project is more optimized and saves the disk space of your hosting account as well. So, today we’ll cover How to Upload files S3 with Laravel.

Here I don’t explain How to install Laravel and do the basic configuration. Because you should know that already. Therefore we’ll be stick to the point on this so we’re creating a basic file upload form and call a method on form submit, and write the file upload logic in the method

OK, let’s get started!

Upload files to S3 with Laravel : Prerequisite

  1. Laravel installation
  2. AWS S3 Bucket

Get AWS S3 Credentials

Firstly head over to your AWS S3 bucket and get the necessary credentials to access their APIs.

Configure Laravel with the credentials.

After that, add those credentials in the .env file of your Laravel installation. We normally put these credentials in .env file for security reasons rather than hard-coding the config files of Laravel.

AWS_ACCESS_KEY_ID={PUT YOUR ACCESS KEY ID HERE}
AWS_SECRET_ACCESS_KEY={PUT YOUR SECRET ACCESS KEY HERE}
AWS_DEFAULT_REGION={PUT REGION OF AWS HERE}
AWS_BUCKET={PUT BUCKET NAME HERE}
AWS_URL={PUT BUCKET URL HERE}

Now we need to download the necessary Laravel module to work with these APIs. Type the following command in the terminal.

composer require league/flysystem-aws-s3-v3 ~1.0

Great! Now all the dependencies are complete. Let’s do the coding part which is quiet simple.

Create File Upload Form


<form action="{{route('ROUTE NAME')}}">
	@csrf
	<input type="file" name="uploaded_file">
	<button type="submit">Submit</button>
</form>

Here I don’t do any styles. Because I use minimal design effort to save time. However you’re free to use any css for styles.

Also, make sure you add certain routes for the action of the above form.

Write File upload Logic

Now it’s time to create a method in the controller to call upon when the form is submitted.


public function fileUpload(Request $request){

   if($request->hasFile('uploaded_file')) {

      //file extension
      $extension = $request->file('uploaded_file')->getClientOriginalExtension();

      //new filename
      $new_filename = time().'.'.$extension;

      //Upload File
      Storage::disk('s3')->put($new_filename, fopen($request->file('uploaded_file'), 'r+'), 'uploads');

      //Do the DB queries to save file URL
   }
}

And make sure you import Storage facade as well.

use Illuminate\Support\Facades\Storage;

And you can view and delete the file using the following pieces of code.

$url = Storage::disk('s3')->url('FILE NAME');
$url = Storage::disk('s3')->delete('FILE NAME');

That’s it!


Food for thought: Zip and download files directory from Amazon S3 with Laravel

Leave a Reply

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