Ejemplo n.º 1
0
 public function __construct($params)
 {
     if (!isset($params['key']) and !isset($params['password']) or !isset($params['user']) or !isset($params['bucket']) or !isset($params['region'])) {
         throw new \Exception("API Key or password, Username, Bucket and Region have to be configured.");
     }
     $this->id = 'swift::' . $params['user'] . md5($params['bucket']);
     $this->bucket = $params['bucket'];
     if (!isset($params['url'])) {
         $params['url'] = 'https://identity.api.rackspacecloud.com/v2.0/';
     }
     if (!isset($params['service_name'])) {
         $params['service_name'] = 'cloudFiles';
     }
     $settings = array('username' => $params['user']);
     if (isset($params['password'])) {
         $settings['password'] = $params['password'];
     } else {
         if (isset($params['key'])) {
             $settings['apiKey'] = $params['key'];
         }
     }
     if (isset($params['tenant'])) {
         $settings['tenantName'] = $params['tenant'];
     }
     $this->anchor = new \OpenCloud\OpenStack($params['url'], $settings);
     if (isset($params['timeout'])) {
         $this->anchor->setHttpTimeout($params['timeout']);
     }
     $this->connection = $this->anchor->ObjectStore($params['service_name'], $params['region'], 'publicURL');
     try {
         $this->container = $this->connection->Container($this->bucket);
     } catch (Exceptions\ContainerNotFoundError $e) {
         $this->container = $this->connection->Container();
         $this->container->Create(array('name' => $this->bucket));
     }
     if (!$this->file_exists('.')) {
         $this->mkdir('.');
     }
 }
 /**
  * @param array $config
  *
  * @throws InvalidArgumentException
  * @throws DfException
  */
 public function __construct($config)
 {
     $storageType = strtolower(ArrayUtils::get($config, 'storage_type'));
     $credentials = $config;
     $this->container = ArrayUtils::get($config, 'container');
     Session::replaceLookups($credentials, true);
     switch ($storageType) {
         case 'rackspace cloudfiles':
             $authUrl = ArrayUtils::get($credentials, 'url', 'https://identity.api.rackspacecloud.com/');
             $region = ArrayUtils::get($credentials, 'region', 'DFW');
             break;
         default:
             $authUrl = ArrayUtils::get($credentials, 'url');
             $region = ArrayUtils::get($credentials, 'region');
             break;
     }
     $username = ArrayUtils::get($credentials, 'username');
     $password = ArrayUtils::get($credentials, 'password');
     $apiKey = ArrayUtils::get($credentials, 'api_key');
     $tenantName = ArrayUtils::get($credentials, 'tenant_name');
     if (empty($authUrl)) {
         throw new InvalidArgumentException('Object Store authentication URL can not be empty.');
     }
     if (empty($username)) {
         throw new InvalidArgumentException('Object Store username can not be empty.');
     }
     $secret = ['username' => $username];
     if (empty($apiKey)) {
         if (empty($password)) {
             throw new InvalidArgumentException('Object Store credentials must contain an API key or a password.');
         }
         $secret['password'] = $password;
     } else {
         $secret['apiKey'] = $apiKey;
     }
     if (!empty($tenantName)) {
         $secret['tenantName'] = $tenantName;
     }
     if (empty($region)) {
         throw new InvalidArgumentException('Object Store region can not be empty.');
     }
     try {
         switch ($storageType) {
             case 'rackspace cloudfiles':
                 $pos = stripos($authUrl, '/v');
                 if (false !== $pos) {
                     $authUrl = substr($authUrl, 0, $pos);
                 }
                 $authUrl = FileUtilities::fixFolderPath($authUrl) . 'v2.0';
                 $os = new Rackspace($authUrl, $secret);
                 break;
             default:
                 $os = new OpenStack($authUrl, $secret);
                 break;
         }
         $this->blobConn = $os->ObjectStore('cloudFiles', $region);
         if (!$this->containerExists($this->container)) {
             $this->createContainer(['name' => $this->container]);
         }
     } catch (\Exception $ex) {
         throw new DfException('Failed to launch OpenStack service: ' . $ex->getMessage());
     }
 }