Friday, January 14, 2022

[FIXED] AWS S3 file upload using cakephp 3

Issue

I'm trying to implement file upload to amazon s3. I'm getting following error

Cannot redeclare GuzzleHttp\uri_template() (previously declared in /var/www/html/appname/vendor/guzzlehttp/guzzle/src/functions‌​.php:17) File /var/www/html/appname/vendor/aws/GuzzleHttp/functions.php 

In upload controller, using below code to upload

 require_once("../vendor/aws/aws-autoloader.php"); 
 use Aws\S3\S3Client; 

 public function upload(){
   $s3 = S3Client::factory(array(   'version' =>
 'latest',   'region'  => 'ap-south-1',   'credentials' => array(
 'key' => 'key',
 'secret'  => 'secret'   ) ));

    if ($this->request->is('post'))
    {
    if(!empty($this->request->data['file']['name']))
        {
            $fileName = $this->request->data['file']['name'];

                   $s3->putObject([
                        'Bucket'       => backetname,
                        'Key'          => $fileName,
                        'SourceFile'   => $this->request->data['file']['tmp_name'],
                        'ContentType'  => 'image/jpeg',
                        'ACL'          => 'public-read',
                        'StorageClass' => 'REDUCED_REDUNDANCY'
                    ]);
         }                       

      }
}

Solution

You are creating an object of S3 client outside of the method which is the reason that $s3 in method is null.

Either you need to create object in method itself or you can store the S3 client object in class property and use with $this->s3->putObject()

Better would be to create a component, something like below:

<?php
 namespace App\Controller\Component;
 use Cake\Controller\Component;
 use Aws\S3\S3Client;

 class AmazonComponent extends Component{
    public $config = null;
    public $s3 = null;

    public function initialize(array $config){
        parent::initialize($config);
        $this->config = [
           's3' => [
               'key' => 'YOUR_KEY',
               'secret' => 'YOUR_SECRET',
               'bucket' => 'YOUR_BUCKET',
           ]
        ];
        $this->s3 = S3Client::factory([
            'credentials' => [
               'key' => $this->config['s3']['key'],
               'secret' => $this->config['s3']['secret']
            ],
        'region' => 'eu-central-1',
        'version' => 'latest'
        ]);
    }
}

And use this component in your controller. Example below:

class UploadController extends AppController{
    public $components = ['Amazon'];

    public function upload(){
         $objects = $this->Amazon->s3->putObject([
                        'Bucket'       => backetname,
                        'Key'          => $fileName,
                        'SourceFile'   => $this->request->data['file']['tmp_name'],
                        'ContentType'  => 'image/jpeg',
                        'ACL'          => 'public-read',
                        'StorageClass' => 'REDUCED_REDUNDANCY'
                    ]);
    }
 }


Answered By - Dileep Kumar

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.