Two examples has been added to the repositories.
The controller S3 has been created with 3 actions:
- Index, that shows the links to create and get objects.
- createObject, that creates the object in the bucket with the content “Empty”. (Really, that is the contento of the file).
- getObject, that downloads the file of the bucket to a temp dire on the server. This is really useful for caching.
Actions:
public function actionGetObject() {
Yii::import('application.vendors.*');
require_once('AWS/sdk.class.php');
$s3 = new AmazonS3();
$bucketname = "yiiaws";
$filename = "MyFileName";
$where = sys_get_temp_dir() . "/" . $filename;
$response = $s3->get_object($bucketname, $filename, array(
'fileDownload' => $where
));
if ($response->status == 200) {
Yii::app()->user->setFlash('success', "$filename was downloaded from $bucketname to <b><i>$where</b></i>");
} else {
Yii::app()->user->setFlash('error', "Error : ".$response->status);
}
$this->render('getObject', array(
));
}
public function actionCreateObject() {
Yii::import('application.vendors.*');
require_once('AWS/sdk.class.php');
$s3 = new AmazonS3();
$bucketname = "yiiaws";
$filename = "MyFileName";
$response = $s3->create_object($bucketname, $filename, array(
'body' => "EMPTY",
'contentType' => 'text/plain',
'acl' => $s3::ACL_PUBLIC
));
/*
* If the status is 200 is because everything went right.
*/
if ($response->status == 200) {
Yii::app()->user->setFlash('success', "$filename was created in $bucketname");
$url = $response->header['x-aws-request-url'];
$this->render('createObject', array(
'url' => $url
));
} else {
/*
* There are a lot of possible errors. The idea is to get them later.
*/
Yii::app()->user->setFlash('error', $response->status . " : " . $response->body->Message);
$this->render('createObject', array(
));
}
//print_r($response);
}
Recent Comments