Example #1
0
 /**
  * We only instantiate the client once per task call
  *
  * @return Services_Amazon_S3
  */
 public function getClient()
 {
     require_once "Services/Amazon/S3.php";
     if ($this->_client === null) {
         $this->_client = Services_Amazon_S3::getAccount($this->getKey(), $this->getSecret());
     }
     return $this->_client;
 }
 /**
  * Breaks a path "s3://mybucket/foo/bar.gif" into a bucket name "mybucket"
  * and a key "bar.gif". If $populateProperties is true (or omitted),
  * various properties are populated on the current instance.
  *
  * @param string $path               a path including the stream wrapper
  *                                   prefix for this wrapper, e.g.
  *                                   s3://foo/bar.txt
  * @param bool   $populateProperties populate various properties on $this
  *
  * @return array   tuple containing (bucketName, key) - key may be false
  */
 private function _parsePath($path, $populateProperties = true)
 {
     if (!preg_match("@^([^:]+)://([^/]*)(/(.*))?\$@", $path, $matches)) {
         return array(false, false);
     }
     $wrapper = $matches[1];
     // the string used in stream_wrapper_register()
     $bucketName = $matches[2];
     $key = isset($matches[4]) ? $matches[4] : false;
     if ($populateProperties) {
         $this->_prefix = $key ? rtrim($key, '/') . '/' : '';
         // If stream wrapper was invoked with a specific stream context,
         // this is set in $this->context
         if (!$this->context) {
             $this->context = stream_context_get_default();
         }
         // Array of options for all stram wrappers
         $options = stream_context_get_options($this->context);
         $this->_options = isset($options[$wrapper]) ? $options[$wrapper] : array();
         if (isset($this->_options['access_key_id']) && isset($this->_options['secret_access_key'])) {
             $this->_s3 = Services_Amazon_S3::getAccount($this->_options['access_key_id'], $this->_options['secret_access_key']);
         } else {
             $this->_s3 = Services_Amazon_S3::getAnonymousAccount();
         }
         // Various options
         if (isset($this->_options['http_options'])) {
             $this->_s3->httpOptions = array_merge($this->_s3->httpOptions, $this->_options['http_options']);
         }
         if (isset($this->_options['use_ssl'])) {
             $this->_s3->useSSL = (bool) $this->_options['use_ssl'];
         }
         if (isset($this->_options['request_style'])) {
             $this->_s3->requestStyle = $this->_options['request_style'];
         }
         if (isset($this->_options['endpoint'])) {
             $this->_s3->endpoint = $this->_options['endpoint'];
         }
         if (isset($this->_options['strict'])) {
             $this->_strict = $this->_options['strict'];
         }
         if ($bucketName) {
             $this->_bucket = $this->_s3->getBucket($bucketName);
             // If $path ends with "/", it is a signal that this is a
             // directory
             if ($key && substr($key, -1) != '/') {
                 $this->_object = $this->_bucket->getObject($key);
             }
         }
     }
     return array($bucketName, $key);
 }
 public static function uploadFileToS3($account, $secret, $bucket, File $file)
 {
     require_once 'Services/Amazon/S3.php';
     $s3 = Services_Amazon_S3::getAccount($account, $secret);
     $bucket = $s3->getBucket($bucket);
     $object = $bucket->getObject($file->original_filename);
     $object->contentType = $file->type;
     $object->acl = Services_Amazon_S3_AccessControlList::ACL_PUBLIC_READ;
     $object->data = $file->FileBin->bin;
     $object->save();
 }