How to use Amazon PHP SDK to upload photos to S3 with Codeigniter 4 |
I have spent ages trying to figure out how to integrate Amazon Web Services (AWS) PHP SDK with CodeIgniter 4 but after a lot of trial and error, I think I've stumbled across some information that I hope might help others with the same problem. I use Amazon S3 as a storage mechanism for lots of images and in my last web service using Codeigniter 3, everything was working fine because I used a third party interface to AWS. This time, I've attempted to integrate the AWS PHP SDK and I managed to get it working OK until I used the S3 putObject function. I got all sorts of memory issues and I just couldn't understand why until I stumbled across this page:-
https://docs.aws.amazon.com/sdk-for-php/...apper.html It appears that using putObject creates all sorts of memory buffering and flushing issues which can be overcome by using the stream wrapper instead. I've included a simple example below on how to upload a file to an AWS S3 bucket easily through Codeigniter 4. Firstly, install the AWS PHP SDK so that the 'aws' root folder is nested in the httpdocs (or equivalent) folder:- [app] [aws] <---- [public] [system] [writeable] Then, make sure you've set your credentials. Because I'm using a localhost and XAMPP with Windows, mine are set through the .aws credentials file as per the AWS documentation. To connect to S3, make sure you add the line PHP Code: use Aws\S3\S3Client; to the top of the controller, then you can set the S3 client when you need it and are ready to put a file into your bucket. For example:- PHP Code: public function upload_file() { This seems to work well and I hope it might help those that, like me, have been struggling to sift through all the AWS documentation. |
Welcome Guest, Not a member yet? Register Sign In |