Welcome Guest, Not a member yet? Register   Sign In
Need help connecting to Amazon S3 storage
#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


Messages In This Thread
RE: Need help connecting to Amazon S3 storage - by chakycool - 06-27-2021, 03:22 AM



Theme © iAndrew 2016 - Forum software by © MyBB