Exemplo n.º 1
0
 public function init($myrole, $drivers)
 {
     $this->_out->logNotice(">>>init S3 driver as {$myrole}");
     $this->_asRemote = $myrole == Core_Engine::ROLE_REMOTE;
     // Amazon library SSL Connection Issues
     if (!defined('AWS_CERTIFICATE_AUTHORITY')) {
         define('AWS_CERTIFICATE_AUTHORITY', $this->_options['certificate_authority']);
     } else {
         $this->_out->logNotice("option 'certificate_authority' was already set, it can't be changed");
     }
     if ($this->_options['compatibility-test']) {
         // see lib/AWSSDKforPHP/_compatibility_test
         $this->_out->jobStart("executing Amazon SDK compatibility test");
         // their code shows notices
         error_reporting(E_ALL & ~E_NOTICE);
         include "lib/AWSSDKforPHP/_compatibility_test/sdk_compatibility_test_cli.php";
         $this->_out->stop("-- re-run without --");
     }
     // test parameters
     if (!isset($this->_options['key'], $this->_options['key']['access'])) {
         throw new Core_StopException("You have to define S3 option key.access.", "S3Init");
     }
     if (!isset($this->_options['key']['secret'])) {
         throw new Core_StopException("You have to define S3 option key.secret.", "S3Init");
     }
     if (!isset($this->_options['bucket'])) {
         throw new Core_StopException("You have to define S3 option bucket.", "S3Init");
     }
     if (!is_null($this->_options['multipart']['part-size']) && ($this->_options['multipart']['part-size'] < 5 || $this->_options['multipart']['part-size'] > 5120)) {
         throw new Core_StopException("multipart.part-size has to be in range from 5MB to 500MB. It is Amazon S3 restriction. Current value is {$this->_options['multipart']['part-size']}MB.", "S3Init");
     }
     $job = $this->_out->jobStart("handshaking with Amazon S3");
     // TODO we need better AmazonS3 error handling
     $this->_s3 = new AmazonS3(array('key' => $this->_options['key']['access'], 'secret' => $this->_options['key']['secret']));
     if (false == $this->_s3->if_bucket_exists($this->getBucket())) {
         $this->_out->jobEnd($job, "failed");
         throw new Core_StopException("S3 bucket not found: '{$this->getBucket()}' for access key '" . substr($this->_options['key']['access'], 0, 5) . "...'", "S3Init");
     }
     $this->_out->jobEnd($job, "authorized");
     // find out if versioning is enabled
     $versioning = $this->_s3->get_versioning_status($this->getBucket());
     if (!$versioning->isOK()) {
         throw new Core_StopException("Not possible to get versioning status of S3 bucket. (" . (string) $versioning->body->Code . ": " . (string) $versioning->body->Message . ")", "S3Init");
     }
     $this->_versioningEnabled = $versioning->body->Status == "Enabled";
     if (!$this->_versioningEnabled) {
         $priority = $this->_options['warn-versioning'] ? Output_Stack::WARNING : Output_Stack::DEBUG;
         $this->_out->log($priority, "Versioning not enabled for this S3 bucket, you will not be able to restore older versions of files.");
     }
     if (array_key_exists('defaultRedundancyStorage', $this->_options)) {
         if (is_string($this->_options['defaultRedundancyStorage'])) {
             $this->_defaultRedundancyStorage = constant("AmazonS3::" . $this->_options['defaultRedundancyStorage']);
         }
     }
     return true;
 }