public function get_service($opts, $useservercerts = false, $disablesslverify = null) { # 'tenant', 'user', 'password', 'authurl', 'path', (optional) 'region' extract($opts); if (null === $disablesslverify) { $disablesslverify = UpdraftPlus_Options::get_updraft_option('updraft_ssl_disableverify'); } if (empty($user) || empty($password) || empty($authurl)) { throw new Exception(__('Authorisation failed (check your credentials)', 'updraftplus')); } require_once UPDRAFTPLUS_DIR . '/oc/autoload.php'; global $updraftplus; $updraftplus->log("OpenStack authentication URL: " . $authurl); $client = new OpenStack($authurl, array('username' => $user, 'password' => $password, 'tenantName' => $tenant)); $this->client = $client; if ($disablesslverify) { $client->setSslVerification(false); } else { if ($useservercerts) { $client->setConfig(array($client::SSL_CERT_AUTHORITY => false)); } else { $client->setSslVerification(UPDRAFTPLUS_DIR . '/includes/cacert.pem', true, 2); } } $client->authenticate(); if (empty($region)) { $catalog = $client->getCatalog(); if (!empty($catalog)) { $items = $catalog->getItems(); if (is_array($items)) { foreach ($items as $item) { $name = $item->getName(); $type = $item->getType(); if ('swift' != $name || 'object-store' != $type) { continue; } $eps = $item->getEndpoints(); if (!is_array($eps)) { continue; } foreach ($eps as $ep) { if (is_object($ep) && !empty($ep->region)) { $region = $ep->region; } } } } } } $this->region = $region; return $client->objectStoreService('swift', $region); }
if (!file_exists($configFile)) { echo "The configuration file '{$configFile}' does not exist.\n\n"; exit(2); } $ocParameters = json_decode(file_get_contents($configFile)); if (!$ocParameters) { echo "The configuration file '{$configFile}' content is not valid JSON.\n\n"; exit(3); } // Initialization $client = new OpenStack($ocParameters->authUrl, array("username" => $ocParameters->username, "password" => $ocParameters->password, "tenantName" => $ocParameters->tenant)); $swiftUrl = $ocParameters->swiftUrl; $serviceName = $ocParameters->serviceName; $region = $ocParameters->region; try { $client->authenticate(); $service = $client->objectStoreService($serviceName, $region); $container = $service->createContainer($containerName); if ($container === false) { echo "The container '{$containerName}' already exists.\n"; } else { echo "The container '{$containerName}' has been successfully created.\n"; } $container = $service->getContainer($containerName); foreach (glob($files) as $filename) { echo "Sending {$filename} (" . number_format(filesize($filename) / 1024 / 1024, 2) . " MB)...\n"; $fileData = fopen($filename, "r+"); $container->uploadObject(basename($filename), $fileData); echo "-> File sent.\n"; } } catch (Exception $e) {