Dive into Scalable File Storage with CodeIgniter and Amazon S3
Amazon S3, the most popular object storage service from Amazon Web Services, is the go-to solution for many developers when it comes to storing and serving files at scale. CodeIgniter, a powerful PHP framework, makes it relatively straightforward to work with Amazon S3 through its vast ecosystem of libraries. In this blog post, we’ll dive into how you can use CodeIgniter to store and retrieve files on Amazon S3.
Prerequisites:
– CodeIgniter installation
– AWS account
– AWS SDK for PHP
1. Installing AWS SDK for PHP
First, you need to have the AWS SDK for PHP installed. The easiest way to do this is using Composer.
```bash composer require aws/aws-sdk-php ```
2. Configuring Credentials
You need to provide AWS credentials (API key and secret) to interact with S3. Update `application/config/config.php` and add the following:
```php $config['aws_access_key'] = 'YOUR_AWS_ACCESS_KEY'; $config['aws_secret_key'] = 'YOUR_AWS_SECRET_KEY'; ```
3. Storing Files to S3
Let’s start by creating a function to upload files to S3.
3.1. Controller
In `application/controllers/Files.php`, add:
```php public function upload() { $this->load->library('s3'); if($_FILES) { $file_name = $_FILES['userfile']['name']; $file_tmp_name = $_FILES['userfile']['tmp_name']; if ($this->s3->putObject($file_tmp_name, 'YOUR_S3_BUCKET_NAME', $file_name, 'public-read')) { echo "File uploaded!"; } else { echo "File upload failed!"; } } else { $this->load->view('upload_form'); } } ```
3.2. Library
Create a file `application/libraries/S3.php`:
```php <?php require 'vendor/autoload.php'; use Aws\S3\S3Client; class S3 { private $s3; public function __construct() { $CI =& get_instance(); $this->s3 = new S3Client([ 'version' => 'latest', 'region' => 'us-west-2', 'credentials' => [ 'key' => $CI->config->item('aws_access_key'), 'secret' => $CI->config->item('aws_secret_key'), ], ]); } public function putObject($file_tmp_name, $bucket, $file_name, $acl = 'public-read') { try { $result = $this->s3->putObject([ 'Bucket' => $bucket, 'Key' => $file_name, 'Body' => fopen($file_tmp_name, 'r'), 'ACL' => $acl, ]); return $result; } catch (Exception $e) { return false; } } } ```
3.3. View
Create a file `application/views/upload_form.php`:
```html <form method="post" enctype="multipart/form-data"> <input type="file" name="userfile" /> <input type="submit" value="Upload" /> </form> ```
Visit `/files/upload` to see your file upload form and test the functionality.
4. Retrieving Files from S3
4.1. Controller
Add the following function to `application/controllers/Files.php`:
```php public function download($file_name) { $this->load->library('s3'); $file_url = $this->s3->getObjectUrl('YOUR_S3_BUCKET_NAME', $file_name); redirect($file_url); } ```
4.2. Library
Update `application/libraries/S3.php` and add:
```php public function getObjectUrl($bucket, $file_name) { return $this->s3->getObjectUrl($bucket, $file_name); } ```
Now, you can access `/files/download/FILENAME` to retrieve and download the file from S3.
Conclusion
Integrating Amazon S3 with CodeIgniter is a seamless process that combines the power of the AWS ecosystem with the simplicity of the CodeIgniter framework. With this integration, developers can easily build scalable web applications without worrying about file storage issues. Always ensure you handle AWS credentials with care, avoiding hardcoding them, and consider using IAM roles or environment variables for enhanced security.
Table of Contents