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; } } }
/** * @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; }
/** * 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; }
/** * 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; }
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('.'); } }