public function testNewFromIdentity()
 {
     $ident = $this->identity();
     $region = self::$settings['openstack.swift.region'];
     $client = $this->getTransportClient();
     $ostore = \OpenStack\ObjectStore\v1\ObjectStorage::newFromIdentity($ident, $region, $client);
     $this->assertInstanceOf('\\OpenStack\\ObjectStore\\v1\\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 \OpenStack\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'.
  * - endpoint: The URL to the authentication endpoint. Necessary if you are not
  *     using a 'token' and 'swift_endpoint'.
  * - transport_client: A transport client for the HTTP requests.
  *
  * To find these params, the method first checks the supplied context. If the
  * key is not found there, it checks the Bootstrap::conf().
  */
 protected function initializeObjectStorage()
 {
     $token = $this->cxt('token');
     $tenantId = $this->cxt('tenantid');
     $tenantName = $this->cxt('tenantname');
     $authUrl = $this->cxt('endpoint');
     $endpoint = $this->cxt('swift_endpoint');
     $client = $this->cxt('transport_client', null);
     $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 \OpenStack\ObjectStore\v1\ObjectStorage($token, $endpoint, $client);
     } elseif (empty($tenantId) && empty($tenantName)) {
         throw new \OpenStack\Common\Exception('Either Tenant ID (tenantid) or Tenant Name (tenantname) is required.');
     } elseif (empty($authUrl)) {
         throw new \OpenStack\Common\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;
         $region = $this->cxt('openstack.swift.region');
         $this->store = ObjectStorage::newFromServiceCatalog($serviceCatalog, $token, $region, $client);
     }
     return !empty($this->store);
 }
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use OpenStack\Identity\v2\IdentityService;
use OpenStack\ObjectStore\v1\ObjectStorage;
use OpenStack\ObjectStore\v1\ObjectStorage\Object;
// Load these from an ini file.
$ini = parse_ini_file(getenv('HOME') . '/.OpenStack.ini');
$username = $ini['username'];
$password = $ini['password'];
$tenantId = $ini['tenantId'];
$endpoint = $ini['url'];
$idService = new IdentityService($endpoint);
$token = $idService->authenticateAsUser($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;
Beispiel #4
0
 protected static function createObjectStoreService()
 {
     return ObjectStorage::newFromIdentity(self::createIdentityService(), self::$settings['openstack.swift.region'], self::getTransportClient());
 }