Created
August 26, 2015 05:52
-
-
Save romaninsh/6816f64f9869363575a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Controller_Storage_S3 extends Controller_Storage { | |
public $useSSL = true; | |
public $endpoint = 's3-eu-west-1.amazonaws.com'; | |
function connect(){ | |
return new S3( | |
$this->app->getConfig('amazon/s3/Access_Key_ID'), | |
$this->app->getConfig('amazon/s3/Secret_Access_Key'), | |
$this->useSSL, | |
$this->endpoint | |
); | |
} | |
function put($model, $source_file){ | |
// genetare unique filename | |
$uniq = uniqid().'.'.$model->ref('file_metadata')['extension']; | |
$s3 = $this->connect(); | |
$res = $s3 -> putObject( | |
S3::inputFile($source_file, false), | |
$bucket = $this->app->getConfig('amazon/s3/image-bucket').'/original', | |
$uniq, | |
S3::ACL_PUBLIC_READ, | |
[], | |
['Content-Type' => $model->ref('file_metadata')['mime']] | |
); | |
$model['storage']='s3'; | |
$model['location']='s3://'.$this->endpoint.'/'.$bucket.'/'.$uniq; | |
list($b1, $b2) = explode('/', $bucket, 2); | |
$model['url']='https://'.$b1.'.s3.amazonaws.com/'.$b2.'/'.$uniq; | |
return $res; | |
} | |
/** | |
* Break location such as s3://s3-eu-west-1.amazonaws.com/lf-image/original/55dc62824072f.png | |
* into bucket and filename. | |
* | |
* @return array(bucket, uri); | |
*/ | |
function splitLocation($location){ | |
// s3://s3-eu-west-1.amazonaws.com/lf-image/original/55dc62824072f.png | |
list(,$location) = explode('//', $location, 2); | |
// s3-eu-west-1.amazonaws.com/lf-image/original/55dc62824072f.png | |
list(,$location) = explode('/', $location, 2); | |
// lf-image/original/55dc62824072f.png | |
return [dirname($location), basename($location)]; | |
// lf-image/original | 55dc62824072f.png | |
} | |
/** | |
* Retrieve file from the store, store in a local file and | |
* return name of that file. Can also return URL to public | |
* file. | |
*/ | |
function get($model){ | |
$s3 = $this->connect(); | |
list($bucket,$file) = $this->splitLocation($model['location']); | |
$s = tempnam('/tmp','atk-file-'); | |
$s3->getObject($bucket, $file, $s); | |
return $s; | |
} | |
/** | |
* Deletes associtaed file from the store. | |
*/ | |
function delete($model){ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment