Welcome Guest, Not a member yet? Register   Sign In
Need help connecting to Amazon S3 storage
#1

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.
Reply
#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...
Reply
#3

(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)
Reply
#4

Not sure where that's coming from, or where the upload() method exists.

All the docs point to the PutObject() method to upload a file - https://docs.aws.amazon.com/code-samples...t.php.html
Reply
#5

Hi Craig, Tried the putObject() but I still get this memory error.

$aws->putObject(['Body' => $file,
'Bucket' => $bucketName,
'Key' => $uploadName,
'ACL' => 'public-read']);
Reply
#6

Can you try uploading a much smaller file to see if you still have the same problem?

You may need to increase the memory limit in your PHP configuration.
Reply
#7

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.
Reply
#8

That library looks really old.

With the limited error details you've provided so far, I'm still not sure why you're getting that error using the official SDK. I've used it on CI3 and CI4 projects and haven't come across that specific problem.
Reply
#9

(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)
   
Reply
#10

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB