How to Set up Laravel Amazon S3 File Upload?

laravel-aws-s3-file-upload

Setting up the file storage features with AWS S3 bucket was not so easy to work with, using the custom code but the Laravel made that easy to set up and use the default Storage and Files system features to use any other storage disk. Here is this tutorial you will learn how to make the AWS S3 bucket working with upload files using Laravel.

Now, you will need the following Credentials in your .env file to setup the connection with you AWS S3 storage bucket, this is basic and most important part to make the link working for uploading the files directly to the storage space.

AWS_ENDPOINT=your-aws-key-endpoint-here
AWS_ACCESS_KEY_ID=your-access-key-ID-here
AWS_SECRET_ACCESS_KEY=your-secret-keye-here
AWS_DEFAULT_REGION=us-east-2 ( it could be different as per your own defined region )
AWS_BUCKET=your-bucket
AWS_USE_PATH_STYLE_ENDPOINT=false

Now you need to mention the s3 disk configuration in your config->filesystem.php file. Perhaps in the latest Laravel installation, the aws s3 .env file variable already defined and configured and named as  's3', but if that not available, then you have to configure them as like below:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
        'throw' => false,
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
        'throw' => false,
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
        'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        'throw' => false,
    ],

],

Now, you just have to use the File system Storage Facade methods to work this file upload working with mentioning the disk method in it like this:

use Illuminate\Support\Facades\Storage;

Now use any of the following methods to store your file.
Default Traditional file upload function

$request->file('upload_file')->storePublicly("/media", 's3');

Disk defined file upload function with public access

Storage::disk('s3')->put($file_path_to_store, fopen($request->file('upload_file'), 'r+'), 'public');

Modified renamed file upload function

$file_name = (string) Str::orderedUuid()->getHex(); // create unique ID for the to rename the uplaoded file
$ext = $request->file('upload_file')->getClientOriginalExtension(); // file extension
$file_path = "media/" .  $file_name.'_'.time().'.'.$ext; // create unique new file name with the folder path to store on disk/space
Storage::disk('s3')->put($file_path, fopen($request->file('upload_file'), 'r+'), 'public'); // store the file with public access

If you will forgot to mention the ‘s3’ disk here, the file will be uploaded to default disk which is configured to Local space.
If you want to know about the on-demand or direct configuration of the filesystem, then you can refer this link for your help.

Please follow and like us:

Related Posts

Leave a Reply

Share