Esempio n. 1
0
 protected function init()
 {
     if ($this->container) {
         return;
     }
     // the OpenCloud client library will default to 'cloudFiles' if $serviceName is null
     $serviceName = null;
     if (isset($this->params['serviceName'])) {
         $serviceName = $this->params['serviceName'];
     }
     // the OpenCloud client library will default to 'publicURL' if $urlType is null
     $urlType = null;
     if (isset($this->params['urlType'])) {
         $urlType = $this->params['urlType'];
     }
     $this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType);
     try {
         $this->container = $this->objectStoreService->getContainer($this->params['container']);
     } catch (ClientErrorResponseException $ex) {
         // if the container does not exist and autocreate is true try to create the container on the fly
         if (isset($this->params['autocreate']) && $this->params['autocreate'] === true) {
             $this->container = $this->objectStoreService->createContainer($this->params['container']);
         } else {
             throw $ex;
         }
     }
 }
Esempio n. 2
0
 /**
  * @param string $key
  *
  * @return \OpenCloud\ObjectStore\Resource\DataObject|false
  */
 protected function tryGetObject($key)
 {
     try {
         return $this->service->getContainer()->getObject($key);
     } catch (ObjectNotFoundException $objFetchError) {
         return false;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function createResourceFromPath($path)
 {
     $container = $this->objectStore->getContainer($this->containerName);
     $resource = new RackspaceCloudFilesResource($path);
     //create_object but no problem if already exists
     $objectData = array('name' => $resource->getResourceName());
     if ($mimeType = $this->guessFileType($path)) {
         $objectData['content_type'] = $mimeType;
     }
     if (!($obj = $this->getObjectByContainer($container, $objectData))) {
         return false;
     }
     $resource->setObject($obj);
     $resource->setContainer($container);
     return $resource;
 }
 /**
  * @param string $container
  * @param string $name
  * @param array  $params
  *
  * @throws DfException
  * @throws \Exception
  */
 public function streamBlob($container, $name, $params = [])
 {
     $this->checkConnection();
     try {
         /** @var Container $container */
         $container = $this->blobConn->Container($container);
         if (empty($container)) {
             throw new \Exception("No container named '{$container}'");
         }
         $obj = $container->DataObject($name);
         header('Last-Modified: ' . $obj->last_modified);
         header('Content-Type: ' . $obj->content_type);
         header('Content-Length:' . $obj->content_length);
         $disposition = isset($params['disposition']) && !empty($params['disposition']) ? $params['disposition'] : 'inline';
         header('Content-Disposition: ' . $disposition . '; filename="' . $name . '";');
         echo $obj->SaveToString();
     } catch (\Exception $ex) {
         if ('Resource could not be accessed.' == $ex->getMessage()) {
             $status_header = "HTTP/1.1 404 The specified file '{$name}' does not exist.";
             header($status_header);
             header('Content-Type: text/html');
         } else {
             throw new DfException('Failed to stream blob: ' . $ex->getMessage());
         }
     }
 }
Esempio n. 5
0
 /**
  * Returns an initialized container
  *
  * @throws \RuntimeException
  * @return Container
  */
 protected function getContainer()
 {
     if ($this->container) {
         return $this->container;
     }
     try {
         return $this->container = $this->objectStore->getContainer($this->containerName);
     } catch (BadResponseException $e) {
         //OpenCloud lib does not wrap this exception
         if (!$this->createContainer) {
             throw new \RuntimeException(sprintf('Container "%s" does not exist.', $this->containerName));
         }
     }
     if (!($container = $this->objectStore->createContainer($this->containerName))) {
         throw new \RuntimeException(sprintf('Container "%s" could not be created.', $this->containerName));
     }
     return $this->container = $container;
 }
Esempio n. 6
0
 /**
  * Returns an initialized container
  *
  * @param $containerName
  * @param bool $createContainer
  * @return Container
  * @throws \RuntimeException
  */
 protected function getContainer($containerName, $createContainer = false)
 {
     if ($this->container) {
         return $this->container;
     }
     try {
         $container = $this->objectStore->getContainer($containerName);
     } catch (ClientErrorResponseException $e) {
         if (!$createContainer) {
             throw new \RuntimeException(sprintf('Container "%s" does not exist.', $containerName));
         } else {
             if (!($container = $this->objectStore->createContainer($containerName))) {
                 throw new \RuntimeException(sprintf('Container "%s" could not be created.', $containerName));
             }
         }
     }
     return $this->container = $container;
 }
Esempio n. 7
0
 public function __construct($params)
 {
     if (empty($params['key']) and empty($params['password']) or empty($params['user']) or empty($params['bucket']) or empty($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 (empty($params['url'])) {
         $params['url'] = 'https://identity.api.rackspacecloud.com/v2.0/';
     }
     if (empty($params['service_name'])) {
         $params['service_name'] = 'cloudFiles';
     }
     $settings = array('username' => $params['user']);
     if (!empty($params['password'])) {
         $settings['password'] = $params['password'];
     } else {
         if (!empty($params['key'])) {
             $settings['apiKey'] = $params['key'];
         }
     }
     if (!empty($params['tenant'])) {
         $settings['tenantName'] = $params['tenant'];
     }
     if (!empty($params['timeout'])) {
         $settings['timeout'] = $params['timeout'];
     }
     if (isset($settings['apiKey'])) {
         $this->anchor = new Rackspace($params['url'], $settings);
     } else {
         $this->anchor = new OpenStack($params['url'], $settings);
     }
     $this->connection = $this->anchor->objectStoreService($params['service_name'], $params['region']);
     try {
         $this->container = $this->connection->getContainer($this->bucket);
     } catch (ClientErrorResponseException $e) {
         $this->container = $this->connection->createContainer($this->bucket);
     }
     if (!$this->file_exists('.')) {
         $this->mkdir('.');
     }
 }