chakycool Member
Posts: 74
Threads: 21
Joined: Apr 2018
Reputation:
0
Hi,
I'm trying to connect with Amazon S3 and the libraries that are available works only with CI3.
Can some one please direct me to CI4 library or any support.
craig CI Veteran
Posts: 60
Threads: 0
Joined: Nov 2020
Reputation:
2
The official AWS SDK for PHP, installed via Composer, works fine with CI 4.
It can be registered and used with CI4 services:
Code:
/* In Config/Services.php: */
public static function aws($getShared = true)
{
if ($getShared) {
return static::getSharedInstance('aws');
}
$config = config('AWS');
$credentials = new \Aws\Credentials\Credentials($config->accessKey, $config->secretKey);
$sdk = new \Aws\Sdk([
'credentials' => $credentials,
'region' => $config->region,
]);
return $sdk;
}
Code:
<?php
/* Config/AWS.php */
namespace Config;
use CodeIgniter\Config\BaseConfig;
class AWS extends BaseConfig
{
public $region = 'eu-west-2';
public $accessKey = '';
public $secretKey = '';
public $inputS3bucket = '';
public $outputS3bucket = '';
}
Code:
# in your .env
aws.accessKey = ABC123...
aws.secretKey = xyz789...
chakycool Member
Posts: 74
Threads: 21
Joined: Apr 2018
Reputation:
0
06-23-2021, 12:59 AM
(This post was last modified: 06-23-2021, 01:57 AM by chakycool .)
Thanks Craig, got the SDK setup.
When I'm using the upload method I'm getting memory error. Can you please point out what I'm doing wrong.
Code
$aws = service('aws')->createClient('S3',['version' => '2006-03-01']);
$aws->upload($bucketName, $uploadFileName, $file);
Error:
ErrorException #1
Allowed memory size of 134217728 bytes exhausted (tried to allocate 65015808 bytes)
craig CI Veteran
Posts: 60
Threads: 0
Joined: Nov 2020
Reputation:
2
chakycool Member
Posts: 74
Threads: 21
Joined: Apr 2018
Reputation:
0
Hi Craig, Tried the putObject() but I still get this memory error.
$aws->putObject(['Body' => $file,
'Bucket' => $bucketName,
'Key' => $uploadName,
'ACL' => 'public-read']);
chakycool Member
Posts: 74
Threads: 21
Joined: Apr 2018
Reputation:
0
Hi Craig,
I actually got this S3 library "https://github.com/tpyo/amazon-s3-php-class" to work on CI4 when I couldn't find anything specific to CI4.
When you shared the info on how to use this via the official SDK you got me interested.
Now the file tested to upload is less that 50Kb and get upload properly via the custom S3 library. The same file gives me a error when I try it via the SDK method.
It would nice we I can get it to work via the SDK.
chakycool Member
Posts: 74
Threads: 21
Joined: Apr 2018
Reputation:
0
06-27-2021, 03:22 AM
(This post was last modified: 06-27-2021, 03:28 AM by chakycool .)
Hi Craig,
I created the service file as you have mentioned above and then I loaded it to the controller.
Here is the code in the controller. (Error screenshot is attached.)
PHP Code:
class Gift extends \ App \ Controllers \ BaseController { public function reward_edit (){ if( $this -> request -> getMethod ()=== 'post' ){ $gift -> fill ( $this -> request -> getPost ()); $file = $this -> request -> getFile ( 'img' ); if( $file -> isValid ()){ $size = $file -> getSizeByUnit ( 'mb' ); if( $size > 1 ){ return redirect ()-> back ()-> with ( 'warning' , 'Must be below 1MB' ); } $type = $file -> getMimeType (); if(! in_array ( $type ,[ 'image/png' , 'image/jpeg' ])){ return redirect ()-> back ()-> with ( 'warning' , 'Invalid file type' ); } //-----S3--------- $newFileName = date ( "YmdHisz" ). '.' . $file -> getClientExtension (); $s3ImagePath = "images/" ; $uploadName = $s3ImagePath . $newFileName ; $bucketName = "xxxxxx" ; $aws = service ( 'aws' )-> createClient ( 's3' ,[ 'version' => 'latest' , 'region' => 'eu-west-1' ]); $aws -> putObject ([ 'SourceFile' => $file , 'Bucket' => $bucketName , 'Key' => $uploadName , 'ACL' => 'public-read' ]); $gift -> addFileName ( $newFileName ); } if( $gift -> hasChanged ()){ if( $giftData_db -> save ( $gift )){ return redirect ()-> to ( '/reward_edit/' . $this -> uri -> getSegment ( 4 )); } else{ return redirect ()-> back () -> with ( 'errors' , $giftData_db -> errors ()) -> with ( 'warning' , 'Invalid data' ) -> withInput (); } }else{ return redirect ()-> back () -> with ( 'errors' , $giftData_db -> errors ()) -> with ( 'warning' , 'Nothing updated' ) -> withInput (); } } return view ( 'rewards_edit' , $data ); }
Attached Files
Thumbnail(s)
superior Coding is Art
Posts: 175
Threads: 30
Joined: Oct 2016
Reputation:
3
(06-27-2021, 03:22 AM) chakycool Wrote: Hi Craig,
I created the service file as you have mentioned above and then I loaded it to the controller.
Here is the code in the controller. (Error screenshot is attached.)
PHP Code:
class Gift extends \ App \ Controllers \ BaseController { public function reward_edit (){ if( $this -> request -> getMethod ()=== 'post' ){ $gift -> fill ( $this -> request -> getPost ()); $file = $this -> request -> getFile ( 'img' ); if( $file -> isValid ()){ $size = $file -> getSizeByUnit ( 'mb' ); if( $size > 1 ){ return redirect ()-> back ()-> with ( 'warning' , 'Must be below 1MB' ); } $type = $file -> getMimeType (); if(! in_array ( $type ,[ 'image/png' , 'image/jpeg' ])){ return redirect ()-> back ()-> with ( 'warning' , 'Invalid file type' ); } //-----S3--------- $newFileName = date ( "YmdHisz" ). '.' . $file -> getClientExtension (); $s3ImagePath = "images/" ; $uploadName = $s3ImagePath . $newFileName ; $bucketName = "xxxxxx" ; $aws = service ( 'aws' )-> createClient ( 's3' ,[ 'version' => 'latest' , 'region' => 'eu-west-1' ]); $aws -> putObject ([ 'SourceFile' => $file , 'Bucket' => $bucketName , 'Key' => $uploadName , 'ACL' => 'public-read' ]); $gift -> addFileName ( $newFileName ); } if( $gift -> hasChanged ()){ if( $giftData_db -> save ( $gift )){ return redirect ()-> to ( '/reward_edit/' . $this -> uri -> getSegment ( 4 )); } else{ return redirect ()-> back () -> with ( 'errors' , $giftData_db -> errors ()) -> with ( 'warning' , 'Invalid data' ) -> withInput (); } }else{ return redirect ()-> back () -> with ( 'errors' , $giftData_db -> errors ()) -> with ( 'warning' , 'Nothing updated' ) -> withInput (); } } return view ( 'rewards_edit' , $data ); }
Try sending it to a .txt file to see what output it gives, most likely it's to big to print on your screen.
Encountered this myself a few times, learned to put it in an empty .txt so i can read it when the request is done.