AWS S3 file upload using cakephp 3


#1

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'
                    ]);
         }                       

      }
}

#2

I would start with installing mixer and looking at the upload plugin.


#3

thanks… currently I’m using that plugin only it’s working fine, no issues but I want store uploaded images to amazon aws s3 storage. that’s my question.


#4

https://cakephp-upload.readthedocs.io/

It looks like you need to configure a custom writer and possibly create a writer using he same interface found here:

Maybe someone knows where there is a s3 writer?


#5

Looks like you can just specify an adapter using a callable in your settings.

filesystem.adapter: (default Local Flysystem Adapter) A Flysystem-compatible adapter. Can also be a callable that returns an adapter.

And then you just use the callable to return something like this:

https://flysystem.thephpleague.com/adapter/aws-s3-v3/