How to upload files to amazon s3 bucket using Laravel

Updated at 07-Feb-2022, By samar

How to upload files to amazon s3 bucket using Laravel

Hello everyone, in this post we will look at how to solve "How to upload files to amazon s3 bucket using Laravel" in programming.

You can upload files to amazon s3 bucket using Laravel. You just have to provide access key id, secret access key, default region and aws_bucket in .env file and use code Storage::disk('s3')->put('file-path/filename.jpg', file_get_contents($request->file)) to upload the file in the specified directory.
  • Upload file to amazon s3 bucket using Laravel

    $file = $request->document;
    $fileName = Str::uuid().'.'.$request->document->extension();
    $filePath =  'project/documents/';
    $path = Storage::disk('s3')->put($filePath.$fileName, file_get_contents($file));
    $s3URL = Storage::disk('s3')->url($path);
    

    Additional info 

    Provide AWS credentials in .env file

    AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXX
    AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    AWS_DEFAULT_REGION=XX-XXXXXXXXX-X
    AWS_BUCKET=XXXXXXXXXXXX

    Use (import) storage facade and Str

    use Illuminate\Support\Str;
    use Illuminate\Support\Facades\Storage;

Back to code snippet queries related laravel