# AWS S3 file upload using cakephp 3

**URL:** <https://discourse.cakephp.org/t/aws-s3-file-upload-using-cakephp-3/2792>\
**Category:** Need Help\
**Created:** [June 28, 2017, 7:06am UTC](https://discourse.cakephp.org/t/aws-s3-file-upload-using-cakephp-3/2792 "2017-06-28T07:06:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashok](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@ashok](https://discourse.cakephp.org/u/ashok)\
**Post date:** [June 28, 2017, 7:06am UTC](https://discourse.cakephp.org/t/aws-s3-file-upload-using-cakephp-3/2792/1 "2017-06-28T07:06:47Z")

</div>

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

      }
}
```

---

<div class="post-metadata">

**Author:** ![xaphalanx](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/xaphalanx/32/575_2.png) [@xaphalanx](https://discourse.cakephp.org/u/xaphalanx)\
**Post date:** [June 28, 2017, 1:29pm UTC](https://discourse.cakephp.org/t/aws-s3-file-upload-using-cakephp-3/2792/2 "2017-06-28T13:29:32Z")

</div>

> **[FriendsOfCake/cakephp-upload](https://github.com/FriendsOfCake/cakephp-upload)**
>
> CakePHP: Handle file uploading sans ridiculous automagic - FriendsOfCake/cakephp-upload

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

---

<div class="post-metadata">

**Author:** ![ashok](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@ashok](https://discourse.cakephp.org/u/ashok)\
**Post date:** [June 28, 2017, 2:20pm UTC](https://discourse.cakephp.org/t/aws-s3-file-upload-using-cakephp-3/2792/3 "2017-06-28T14:20:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xaphalanx](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/xaphalanx/32/575_2.png) [@xaphalanx](https://discourse.cakephp.org/u/xaphalanx)\
**Post date:** [June 28, 2017, 3:46pm UTC](https://discourse.cakephp.org/t/aws-s3-file-upload-using-cakephp-3/2792/4 "2017-06-28T15:46:56Z")

</div>

[https://cakephp-upload.readthedocs.io/](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:

> **[FriendsOfCake/cakephp-upload](https://github.com/FriendsOfCake/cakephp-upload/tree/master/src/File/Writer)**
>
> CakePHP: Handle file uploading sans ridiculous automagic - FriendsOfCake/cakephp-upload

Maybe someone knows where there is a s3 writer?

---

<div class="post-metadata">

**Author:** ![xaphalanx](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/xaphalanx/32/575_2.png) [@xaphalanx](https://discourse.cakephp.org/u/xaphalanx)\
**Post date:** [June 28, 2017, 4:07pm UTC](https://discourse.cakephp.org/t/aws-s3-file-upload-using-cakephp-3/2792/5 "2017-06-28T16:07:27Z")

</div>

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/](https://flysystem.thephpleague.com/adapter/aws-s3-v3/)
