Amazon S3 Integration with YII Framework 07Apr, 2014

Amazon S3 is an online file storage repository offered by Amazon Web Services. It provides extensive storage capabilities in the cloud through a number of web services interfaces. Launched in 2006, Amazon S3 serves thousands of customers and stores billions of objects.

Using the extension I have described below, you can interact with Amazon S3 using the class created by Donovan Schönknecht. This wrapper class enhances the S3 class and creates an extension for YII.

The important function of this wrapper class is that it can be used easily with one line of code in your YII controller. Apart from that, you can also extend it to create your own functions as required.

Here are the simple step-by-step instructions to use this extension.

  • Download the extension from here YII-S3-Extension
  • Extract your zipped source files intothe directory at protected/extension/aws
  • Add the following code to your app configuration file found at protected/config/main.php

's3' => array (
'class' => 'ext.aws.ES3',
'aKey' => 'ADD YOUR AKEY HERE',
'sKey' => 'ADD YOUR SKEY HERE,
'bucket' => 'DEFAULT BUCKET HERE (OPTIONAL)',
),

Use the S3 functions in your YII controllers as shown below:

/* List existed bucket */

$buckets = Yii::app()->s3->buckets();

/* Create a bucket */

Yii::app()->s3->createBucket("your_bucket_name");

/* Delete a bucket */

Yii::app()->s3->deleteBucket("your_bucket_name");

/* Upload a file */

$success = Yii::app()->s3->upload( 'originalfile' , 'uploadedfile', 'your_bucket_name');

/* Delete a file */

Yii::app()->s3->deleteObject();

/* Copy a file */

Yii::app()->s3->copyFile($sourceBucket,$fileUrifilename,$targetBucket,$fileUriNewname);

This extension assumes that you have CFile Extension configured to use ‘file’ as the application component name. If you don’t have the required extension, you can download it from the following links:
http://www.yiiframework.com/extension/cfile
https://github.com/idlesign/ist-yii-cfile

Posted by: DineshSaini / In: YII and Tagged , , ,
Cam