Ejemplo n.º 1
0
 /**
  * Gets s3 object
  *
  * @param  boolean   $debug return error message instead of script stop
  * @return \AmazonS3 s3 object
  */
 public function s3($debug = false)
 {
     // This is workaround to composer autoloader
     if (!class_exists('CFLoader')) {
         throw new ClassNotFoundException('Amazon: autoload failed');
     }
     if (empty($this->_s3)) {
         \CFCredentials::set(array('@default' => array('key' => $this->getOption('key'), 'secret' => $this->getOption('secret'))));
         $this->_s3 = new \AmazonS3();
         $this->_s3->use_ssl = false;
         $this->_buckets = fn_array_combine($this->_s3->get_bucket_list(), true);
     }
     $message = '';
     $bucket = $this->getOption('bucket');
     if (empty($this->_buckets[$bucket])) {
         $res = $this->_s3->create_bucket($bucket, $this->getOption('region'));
         if ($res->isOK()) {
             $res = $this->_s3->create_cors_config($bucket, array('cors_rule' => array(array('allowed_origin' => '*', 'allowed_method' => 'GET'))));
             if ($res->isOK()) {
                 $this->_buckets[$bucket] = true;
             } else {
                 $message = (string) $res->body->Message;
             }
         } else {
             $message = (string) $res->body->Message;
         }
     }
     if (!empty($message)) {
         if ($debug == true) {
             return $message;
         }
         throw new ExternalException('Amazon: ' . $message);
     }
     return $this->_s3;
 }