Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * ES3 class file.
- *
- * ES3 is a wrapper for the excellent S3.php class provided by Donovan Sch�nknecht (@link http://undesigned.org.za/2007/10/22/amazon-s3-php-class)
- * This wrapper contains minimal functionality as there is only so much I want to allow access to from the Yii public end
- *
- * @version 0.1
- *
- * @uses CFile
- * @author Dana Luther (dana.luther@gmail.com)
- * @copyright Copyright © 2010 Dana Luther
- */
- class ES3 extends CApplicationComponent
- {
- private $_s3;
- public $aKey; // AWS Access key
- public $sKey; // AWS Secret key
- public $bucket;
- public $lastError='';
- public $cacheControl = 'max-age=2592000, public'; //30 dias + publico
- private function getInstance(){
- if ($this->_s3 === NULL)
- $this->connect();
- return $this->_s3;
- }
- /**
- * Instance the S3 object
- */
- public function connect()
- {
- if ( $this->aKey === NULL || $this->sKey === NULL )
- throw new CException('S3 Keys are not set.');
- $this->_s3 = new S3($this->aKey,$this->sKey);
- }
- /**
- * @param string $original File to upload - can be any valid CFile filename
- * @param string $uploaded Name of the file on destination -- can include directory separators
- */
- public function upload( $original, $uploaded='', $contentType=null, $bucket='' )
- {
- $s3 = $this->getInstance();
- if( $bucket == '' )
- {
- $bucket = $this->bucket;
- }
- if ($bucket === NULL || trim($bucket) == '')
- {
- throw new CException('Bucket param cannot be empty');
- }
- $file = Yii::app()->file->set($original);
- if(!$file->exists)
- throw new CException('Origin file not found');
- $fs1 = $file->size;
- if ( !$fs1 )
- {
- $this->lastError = "Attempted to upload empty file.";
- return false;
- }
- if (trim($uploaded) == ''){
- $uploaded = $original;
- }
- $requestHeaders = array();
- if(is_string($contentType))
- $requestHeaders['Content-Type'] = $contentType;
- if($this->cacheControl)
- $requestHeaders['Cache-Control'] = $this->cacheControl;
- if(is_array($contentType))
- $requestHeaders = array_merge($requestHeaders, $contentType);
- //if (!$s3->putObject($s3->inputResource(fopen($file->getRealPath(), 'r'), $fs1), $bucket, $uploaded, S3::ACL_PUBLIC_READ))
- //echo $file->getRealPath();
- //if (!$s3->putObject($s3->inputResource( fopen($file->getRealPath(), 'rb'), $fs1), $bucket, $uploaded, S3::ACL_PUBLIC_READ))
- if (!$s3->putObjectFile( $original, $bucket, $uploaded, S3::ACL_PUBLIC_READ, array(), $requestHeaders))
- {
- $this->lastError = "Unable to upload file.";
- return false;
- }
- return true;
- }
- // Testing connection :p
- public function buckets()
- {
- $s3 = $this->getInstance();
- return $this->_s3->listBuckets();
- }
- // Passthru function for basic functions
- public function call( $func )
- {
- $s3 = $this->getInstance();
- return $s3->$func();
- }
- public function createUrl( $uri, $bucket=null ) {
- if(!$bucket) {
- $bucket = $this->bucket;
- }
- $schema = Yii::app()->request->getIsSecureConnection() ? 'https' : 'http';
- return "{$schema}://s3-eu-west-1.amazonaws.com/{$bucket}/{$uri}";
- }
- public function delete( $uri, $bucket='' ) {
- if( $bucket == '' ) {
- $bucket = $this->bucket;
- }
- $s3 = $this->getInstance();
- if(!$s3->deleteObject( $bucket, $uri )) {
- $this->lastError = "Unable do delete object '$uri'";
- return false;
- }
- return true;
- }
- public function copy($srcUri, $destUri, $metaHeaders = array(), $requestHeaders = array(), $acl = S3::ACL_PRIVATE, $srcBucket='', $destBucket='') {
- if( $srcBucket == '' )
- $srcBucket = $this->bucket;
- if( $destBucket == '' )
- $destBucket = $this->bucket;
- $s3 = $this->getInstance();
- return $s3->copyObject($srcBucket, $srcUri, $destBucket, $destUri, $acl, $metaHeaders, $requestHeaders);
- //return $s3->copyObject($srcBucket, $srcUri, $destBucket, $destUri) !== false;
- }
- public function contents() {
- $s3 = $this->getInstance();
- return $s3->getBucket($this->bucket);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement