/** * Cleaning up the test container so we can reuse it for other tests. */ public static function tearDownAfterClass() { // First we get an identity $user = self::conf('hpcloud.identity.username'); $pass = self::conf('hpcloud.identity.password'); $tenantId = self::conf('hpcloud.identity.tenantId'); $url = self::conf('hpcloud.identity.url'); $ident = new \HPCloud\Services\IdentityServices($url); $token = $ident->authenticateAsUser($user, $pass, $tenantId); $region = self::conf('hpcloud.swift.region'); // Then we need to get an instance of storage $store = \HPCloud\Storage\ObjectStorage::newFromIdentity($ident, $region); // Delete the container and all the contents. $cname = self::$settings['hpcloud.swift.container']; try { $container = $store->container($cname); } catch (\HPCloud\Transport\FileNotFoundException $e) { return; } foreach ($container as $object) { try { $container->delete($object->name()); } catch (\Exception $e) { } } $store->deleteContainer($cname); }
// tests is ill advised. define('TEST_CONTAINER', 'mycontainer'); $base = __DIR__ . '/../src'; require_once $base . '/HPCloud/Bootstrap.php'; \HPCloud\Bootstrap::useAutoloader(); $inifile = __DIR__ . '/settings.ini'; if (!is_readable($inifile)) { die('Could not find ' . $inifile); } $ini = parse_ini_file($inifile, FALSE); \HPCloud\Bootstrap::setConfiguration($ini); \HPCloud\Bootstrap::useStreamWrappers(); $id = new \HPCloud\Services\IdentityServices($ini['hpcloud.identity.url']); //$token = $id->authenticateAsAccount($ini['hpcloud.identity.account'], $ini['hpcloud.identity.secret'], $ini['hpcloud.identity.tenantId']); $token = $id->authenticateAsUser($ini['hpcloud.identity.username'], $ini['hpcloud.identity.password'], $ini['hpcloud.identity.tenantId']); $objstore = \HPCloud\Storage\ObjectStorage::newFromServiceCatalog($id->serviceCatalog(), $token); $cdn = \HPCloud\Storage\CDN::newFromServiceCatalog($id->serviceCatalog(), $token); $objstore->useCDN($cdn); //var_dump($cdn->containers()); // Check that the container has CDN. $cname = TEST_CONTAINER; //$ini['hpcloud.swift.container']; $isEnabled = FALSE; $cdnData = $cdn->container($cname); print "***** TESTING CDN ENABLED" . PHP_EOL; if ($cdnData['cdn_enabled'] != 1) { die('Cannot test CDN: You must enable CDN on ' . $cname); } $container = $objstore->container($cname); print "***** TESTING CDN URL" . PHP_EOL; $cdnSsl = $objstore->cdnUrl($cname);
/** * Based on the context, initialize the ObjectStorage. * * The following parameters may be set either in the stream context * or through HPCloud::Bootstrap::setConfiguration(): * * - token: An auth token. If this is supplied, authentication is skipped and * this token is used. NOTE: You MUST set swift_endpoint if using this * option. * - swift_endpoint: The URL to the swift instance. This is only necessary if * 'token' is set. Otherwise it is ignored. * - username: A username. MUST be accompanied by 'password' and 'tenantid'. * - password: A password. MUST be accompanied by 'username' and 'tenantid'. * - account: An account ID. MUST be accompanied by a 'key' and 'tenantid'. * - key: A secret key. MUST be accompanied by an 'account' and 'tenantid'. * - endpoint: The URL to the authentication endpoint. Necessary if you are not * using a 'token' and 'swift_endpoint'. * - use_swift_auth: If this is set to TRUE, it will force the app to use * the deprecated swiftAuth instead of IdentityServices authentication. * In general, you should avoid using this. * * To find these params, the method first checks the supplied context. If the * key is not found there, it checks the Bootstrap::conf(). * * @fixme This should be rewritten to use ObjectStorage::newFromServiceCatalog(). */ protected function initializeObjectStorage() { $token = $this->cxt('token'); if (empty($token) && isset($_SESSION["HPCLOUD_TOKEN"])) { $token = $_SESSION["HPCLOUD_TOKEN"]; } $account = $this->cxt('account'); $key = $this->cxt('key'); $tenantId = $this->cxt('tenantid'); $authUrl = $this->cxt('endpoint'); $endpoint = $this->cxt('swift_endpoint'); $serviceCatalog = NULL; if (!empty($token) && (isset(self::$serviceCatalogCache[$token]) || isset($_SESSION["HPCLOUD_CATALOG"]))) { if (isset($_SESSION["HPCLOUD_CATALOG"])) { $serviceCatalog = $_SESSION["HPCLOUD_CATALOG"]; } else { $serviceCatalog = self::$serviceCatalogCache[$token]; } if (empty($endpoint)) { for ($i = 0; $i < count($serviceCatalog); $i++) { if ($serviceCatalog[$i]['type'] == "object-store") { $endpoint = $serviceCatalog[$i]['endpoints'][0]['publicURL']; break; } } } } // FIXME: If a token is invalidated, we should try to re-authenticate. // If context has the info we need, start from there. if (!empty($token) && !empty($endpoint)) { $this->store = new \HPCloud\Storage\ObjectStorage($token, $endpoint); } elseif ($this->cxt('use_swift_auth', FALSE)) { if (empty($authUrl) || empty($account) || empty($key)) { throw new \HPCloud\Exception('account, endpoint, key are required stream parameters.'); } $this->store = \HPCloud\Storage\ObjectStorage::newFromSwiftAuth($account, $key, $authUrl); } elseif (empty($tenantId) || empty($authUrl)) { throw new \HPCloud\Exception('Tenant ID (tenantid) and endpoint are required.'); } else { \AJXP_Logger::debug("Authenticating"); $ident = $this->authenticate(); // Update token and service catalog. The old pair may be out of date. $token = $ident->token(); $serviceCatalog = $ident->serviceCatalog(); self::$serviceCatalogCache[$token] = $serviceCatalog; $this->store = ObjectStorage::newFromServiceCatalog($serviceCatalog, $token); $_SESSION["HPCLOUD_TOKEN"] = $token; $_SESSION["HPCLOUD_CATALOG"] = $serviceCatalog; /* $catalog = $ident->serviceCatalog(ObjectStorage::SERVICE_TYPE); if (empty($catalog) || empty($catalog[0]['endpoints'][0]['publicURL'])) { //throw new \HPCloud\Exception('No object storage services could be found for this tenant ID.' . print_r($catalog, TRUE)); throw new \HPCloud\Exception('No object storage services could be found for this tenant ID.'); } $serviceURL = $catalog[0]['endpoints'][0]['publicURL']; $this->store = new ObjectStorage($token, $serviceURL); */ } try { $this->initializeCDN($token, $serviceCatalog); } catch (\HPCloud\Exception $e) { //fwrite(STDOUT, $e); throw new \HPCloud\Exception('CDN could not be initialized', 1, $e); } return !empty($this->store); }
protected function objectStore($reset = FALSE) { if ($reset || empty(self::$ostore)) { $region = self::conf('hpcloud.swift.region'); $ident = $this->identity($reset); $objStore = \HPCloud\Storage\ObjectStorage::newFromIdentity($ident, $region); self::$ostore = $objStore; } return self::$ostore; }
<?php require_once __DIR__ . '/../src/HPCloud/Bootstrap.php'; use HPCloud\Bootstrap; use HPCloud\Services\IdentityServices; use HPCloud\Storage\ObjectStorage; use HPCloud\Storage\ObjectStorage\Object; Bootstrap::useAutoloader(); // Load these from an ini file. $ini = parse_ini_file(getenv('HOME') . '/.hpcloud.ini'); $username = $ini['username']; $password = $ini['password']; $tenantId = $ini['tenantId']; $endpoint = $ini['url']; $idService = new IdentityServices($endpoint); $token = $idService->authenticateAsAccount($username, $password, $tenantId); $catalog = $idService->serviceCatalog(); $store = ObjectStorage::newFromServiceCatalog($catalog, $token); $store->createContainer('Example'); $container = $store->container('Example'); $name = 'hello.txt'; $content = 'Hello World'; $mime = 'text/plain'; $localObject = new Object($name, $content, $mime); $container->save($localObject); $object = $container->object('hello.txt'); printf("Name: %s \n", $object->name()); printf("Size: %d \n", $object->contentLength()); printf("Type: %s \n", $object->contentType()); print $object->content() . PHP_EOL;
public function testNewFromIdentity() { $ident = $this->identity(); $region = self::conf('hpcloud.swift.region'); $ostore = \HPCloud\Storage\ObjectStorage::newFromIdentity($ident, $region); $this->assertInstanceOf('\\HPCloud\\Storage\\ObjectStorage', $ostore); $this->assertTrue(strlen($ostore->token()) > 0); }
/** * Based on the context, initialize the ObjectStorage. * * The following parameters may be set either in the stream context * or through HPCloud::Bootstrap::setConfiguration(): * * - token: An auth token. If this is supplied, authentication is skipped and * this token is used. NOTE: You MUST set swift_endpoint if using this * option. * - swift_endpoint: The URL to the swift instance. This is only necessary if * 'token' is set. Otherwise it is ignored. * - username: A username. MUST be accompanied by 'password' and 'tenantname'. * - password: A password. MUST be accompanied by 'username' and 'tenantname'. * - account: An account ID. MUST be accompanied by a 'secret' and 'tenantname'. * - secret: A secret key. MUST be accompanied by an 'account' and 'tenantname'. * - endpoint: The URL to the authentication endpoint. Necessary if you are not * using a 'token' and 'swift_endpoint'. * - use_swift_auth: If this is set to TRUE, it will force the app to use * the deprecated swiftAuth instead of IdentityServices authentication. * In general, you should avoid using this. * * To find these params, the method first checks the supplied context. If the * key is not found there, it checks the Bootstrap::conf(). * * @param string $region * The region being used for this service. * * @fixme This should be rewritten to use ObjectStorage::newFromServiceCatalog(). */ protected function initializeObjectStorage() { $token = $this->cxt('token'); $account = $this->cxt('account'); // Legacy support for old 'key' param. $key = $this->cxt('key', $this->cxt('secret')); $tenantId = $this->cxt('tenantid'); $tenantName = $this->cxt('tenantname'); $authUrl = $this->cxt('endpoint'); $endpoint = $this->cxt('swift_endpoint'); $region = $this->cxt('region'); $serviceCatalog = NULL; if (!empty($token) && isset(self::$serviceCatalogCache[$token])) { $serviceCatalog = self::$serviceCatalogCache[$token]; } // FIXME: If a token is invalidated, we should try to re-authenticate. // If context has the info we need, start from there. if (!empty($token) && !empty($endpoint)) { $this->store = new \HPCloud\Storage\ObjectStorage($token, $endpoint); } elseif ($this->cxt('use_swift_auth', FALSE)) { if (empty($authUrl) || empty($account) || empty($key)) { throw new \HPCloud\Exception('account, endpoint, key are required stream parameters.'); } $this->store = \HPCloud\Storage\ObjectStorage::newFromSwiftAuth($account, $key, $authUrl); } elseif (empty($tenantId) && empty($tenantName)) { throw new \HPCloud\Exception('Either Tenant ID (tenantid) or Tenant Name (tenantname) is required.'); } elseif (empty($authUrl)) { throw new \HPCloud\Exception('An Identity Service Endpoint (endpoint) is required.'); } else { $ident = $this->authenticate(); // Update token and service catalog. The old pair may be out of date. $token = $ident->token(); $serviceCatalog = $ident->serviceCatalog(); self::$serviceCatalogCache[$token] = $serviceCatalog; $this->store = ObjectStorage::newFromServiceCatalog($serviceCatalog, $token, $region); /* $catalog = $ident->serviceCatalog(ObjectStorage::SERVICE_TYPE); if (empty($catalog) || empty($catalog[0]['endpoints'][0]['publicURL'])) { //throw new \HPCloud\Exception('No object storage services could be found for this tenant ID.' . print_r($catalog, TRUE)); throw new \HPCloud\Exception('No object storage services could be found for this tenant ID.'); } $serviceURL = $catalog[0]['endpoints'][0]['publicURL']; $this->store = new ObjectStorage($token, $serviceURL); */ } try { $this->initializeCDN($token, $serviceCatalog); } catch (\HPCloud\Exception $e) { //fwrite(STDOUT, $e); throw new \HPCloud\Exception('CDN could not be initialized', 1, $e); } return !empty($this->store); }