Skip to main content

Upload files to AWS S3 using laravel

Today, I am going to share with you How to file upload in AWS s3 using Laravel 5. Laravel 5 introduce new feature in FileSystem that makes easy to upload file or image or docs etc in S3 server.

When we use s3 driver for Storage FileSystem we require to install "league/flysystem-aws-s3-v3" composer package for amazon api. So you have to just follow few step to do this from scratch.

First we need to install flysystem-aws-s3-v3 Package.
Run below command in CMD/Terminal

composer require league/flysystem-aws-s3-v3

Now you have to add following details on your config/filesystems.php file.


's3' => [
            'driver' => 's3',
            'key' => 'aws-access-key',
            'secret' => 'aws-secret-key',
            'region' => 'aws-region',
            'bucket' => 'bucket-name,
        ],
Now we need to create a S3 Filesystem instance, define the path relative to our bucket, and upload the file. We will use the $s->put() method, and pass three perameters.

Now put the below code into your controller:
-Filepath relative to your bucket
-The contents of the file
-Permission of the file (optional)

$files = $request->file('image');
if(!empty($files)) {
$imageName = time().rand(11,99).'.'.$files->getClientOriginalExtension();
$s3 = \Storage::disk('s3');
$filePath = '/folder-name/'.$imageName;
$s3->put($filePath, file_get_contents($files), 'public');


If You are getting "SSL certificate problem: unable to get local issuer certificate" Error
than add Below line at the end of php.ini.:-

curl.cainfo="/path/to/downloaded/cacert.pem" // Do not forget to write between quotes
Like: curl.cainfo = "F:\test\php\cacert.pem"

Download the latest cacert.pem from https://curl.haxx.se/ca/cacert.pem



Comments

  1. Get Laravel development Solutions from a best Laravel Development Company in India & USA. We provide industry wise web Solutions like Health, Pharma, Realstate, IT etc. Contact us : +91-9806724185 or Contact@expresstechsoftwares.com

    ReplyDelete
  2. Those guidelines additionally worked to become a good way to recognize that other people online have identical fervor like mine to grasp a great deal more around this condition. and I could assume you are an expert on this subject. Same as your blog i found another one Amazon Master Class .Actually I was looking for the same information on internet for Amazon Master Class and came across your blog. I am impressed by the information that you have on this blog. Thanks a million and please keep up the gratifying work.

    ReplyDelete
  3. This post is so interactive and informative.keep update more information...
    CCNA course in Anna Nagar
    ccna course in Chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Configure MySQL to handle camel case (or case sensitive) table names and column names

The default settings provided of MySQL enforce a strict lowercase conversion for all the table and database names. To allow camel case in mysql we need to made following changes in mysql configuration file.   lower_case_table_names It can easily be added to the MySQL my.ini file, usually found in the following folder: C:\xampp\mysql\bin The easiest way to take care of this is to add the following to your my.ini file under the [mysqld] section. [mysqld] ... lower_case_table_names = 2 And Restart Xampp.

Ionic Cordova FCM Execution failed for task ':app:processDebugGoogleServices' Error

I am new to Ionic framework version1 and working on an ionic app. I am trying to make push notifications work through FCM(Firebase Cloud Messaging ), with cordova-plugin-fcm-with-dependecy-updated  plugin in ionic. When I run ionic cordova build android , it gives build failure error: Execution failed for task ':app:processDebugGoogleServices'. > No matching client found for package name 'io.ionic.starter'  I am sharing it because if anyone has the same problem, Please check the widget id in config.xml and package_name in google-services.json are the same.