您好,登錄后才能下訂單哦!
這篇文章主要介紹了openstack中php sdk php-opencloud怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
openstack提供的是rest風格的api接口,其說明文檔見:
http://developer.openstack.org/api-ref-guides/bk-api-ref.pdf,為便于理解和簡化調用,這里使用開源的php sdk——php-opencoud。
vendor
---------composer
---------guzzle
---------psr
---------rackspace
--------------php-opencloud
---------------------docs
---------------------lib
---OpenCloud
---------------------samples
---------------------tests
composer:提供自動加載的功能
guzzle:處理http請求
psr:提供日志功能
rackspace/php-opencloud/lib/OpenCloud:php封裝庫
OpenCloud不僅支持openstack的api,還支持Rackspace的api。
由圖中看到,可以從OpenStack客戶端中根據需要獲取不同的服務對象。計算服務用來管理云主機,通過主機類型、鏡像、密鑰對和網絡等數據就可以創建一臺云主機,而且能夠對云主機做出暫停、恢復、停止、開始和重啟等多種操作,還可以得到基于web vnc的控制臺鏈接。對象存儲服務是以容器的形式來存放數據對象,可以給對象名稱設置路徑前綴來對對象進行分類,組織成類似windows文件夾的結構。
<?php require dirname(__DIR__) . '/vendor/autoload.php'; use OpenCloud\Openstack; use OpenCloud\Compute\Constants\ServerState; use Guzzle\Http\Exception\BadResponseException; error_reporting(0); $client = new Openstack('http://192.168.128.0:5000/v2.0', array( 'username' => 'admin', 'password' => 'ADMIN_PASS', 'tenantName' => 'admin' )); $computeService = $client->computeService('nova','regionOne'); //1.枚舉鏡像 $p_w_picpaths = $computeService->p_w_picpathList(); printf("Images-------------------------------------------------------------------------\n"); foreach($p_w_picpaths as $p_w_picpath) { printf("ID:%s,Name:%s\n",$p_w_picpath->id,$p_w_picpath->name); } //2.枚舉主機類型 $flavors = $computeService->flavorList(); printf("Flavors-------------------------------------------------------------------------\n"); foreach($flavors as $flavor) { printf("ID:%-36s,Name:%-36s,Vcpus:%d,Ram:%d\n",$flavor->id,$flavor->name,$flavor->vcpus,$flavor->ram); } //3.枚舉密鑰對(for windows:加密密碼傳輸 for linux:ssh登錄) $keypairs = $computeService->listKeypairs(); printf("Keypairs------------------------------------------------------------------------\n"); foreach($keypairs as $keypair) { printf("Name:%s\n",$keypair->getName()); } //4.創建云主機 $server = $computeService->server(); printf("Create Server-------------------------------------------------------------------\n"); $serverName = 'test123'; $p_w_picpathId = 'c36b0707-38ec-45e5-9d62-7e8798f1724f'; $flavorId = '53b1355a-c7c6-41a6-884f-7b1604002897'; $keypairName = 'testKey'; try { $response = $server->create(array( 'name' => $serverName, 'p_w_picpathId' => $p_w_picpathId, 'flavorId' => $flavorId, 'keypair' => $keypairName, 'metadata' => array( 'admin_pass' => '123456',//設置密碼 'appid' => '123456789' //設置自定義元數據 ))); }catch(BadResponseException $e) { echo $e->getResponse(); } printf("ID:%s,Name:%s,Status:%s\n",$server->id,$server->name,$server->status); function GetServerState($server) { printf("Status:%s\n",$server->status); } //wait for $server->waitFor(ServerState::ACTIVE,null,GetServerState,1); if (is_object($server->addresses)) { foreach ($server->addresses as $netkey => $netvalue) { printf("\tNetwork name:%s\n",$netkey); if (is_array($netvalue)) { foreach ($netvalue as $infokey => $infovalue) { printf("\t\tIndex:%s\n",$infokey); printf("\t\t\tMac: %s\n",$infovalue->{'OS-EXT-IPS-MAC:mac_addr'}); printf("\t\t\tVersion:%s\n",$infovalue->version); printf("\t\t\tAddr: %s\n",$infovalue->addr); printf("\t\t\tType: %s\n",$infovalue->{'OS-EXT-IPS:type'}); } } } } //5.獲取vnc訪問地址 printf("VNC-----------------------------------------------------------------------------\n"); $console = $server->console(); printf("Type:%s,URL:%s\n",$console->type,$console->url); //6.停止云主機 printf("Stop Server---------------------------------------------------------------------\n"); $server->stop(); $server->waitFor('SHUTOFF',null,GetServerState,1); //7.啟動云主機 printf("Start Server--------------------------------------------------------------------\n"); $server->start(); $server->waitFor(ServerState::ACTIVE,null,GetServerState,1); //8.重啟云主機 printf("ReBoot Server-------------------------------------------------------------------\n"); //$server->reboot(ServerState::REBOOT_STATE_SOFT);//REBOOT_STATE_SOFT:軟重啟 REBOOT_STATE_HARD:硬重啟 //$server->waitFor(ServerState::ACTIVE,null,GetServerState,1); //9.刪除云主機 printf("Delete Server-------------------------------------------------------------------\n"); $server->delete(); //10.獲取鏡像信息 printf("Image Info----------------------------------------------------------------------\n"); $p_w_picpath = $computeService->p_w_picpath($server->p_w_picpath->id); printf("ID:%s,Name:%s\n",$p_w_picpath->id,$p_w_picpath->name); //11.獲取主機類型信息 printf("Flavor Info---------------------------------------------------------------------\n"); $flavor = $computeService->flavor($server->flavor->id); printf("ID:%s,Name:%s,Vcpus:%d,Ram:%d\n",$flavor->id,$flavor->name,$flavor->vcpus,$flavor->ram);
<?php require dirname(__DIR__) . '/vendor/autoload.php'; use OpenCloud\Openstack; use OpenCloud\Common\Constants\Size; use OpenCloud\ObjectStore\Resource\DataObject; $client = new Openstack('http://192.168.128.0:5000/v2.0', array( 'username' => 'admin', 'password' => 'ADMIN_PASS', 'tenantName' => 'admin' )); //獲取對象存儲服務 $objectStoreService = $client->objectStoreService('swift', 'regionOne'); //1.創建容器(每個云盤使用者都有一個容器跟其對應) $containerName = 'testContainer'; printf("Create Container %s------------------------------------------------------\n",$containerName); //檢查名字合法性 $objectStoreService->checkContainerName($containerName); $container = $objectStoreService->createContainer($containerName); if($container == false) { $container = $objectStoreService->getContainer($containerName); } else { //設置配額 $container->setBytesQuota(5 * Size::GB); // 5GB } //2.枚舉容器 $containers = $objectStoreService->listContainers(); printf("Containers--------------------------------------------------------------------------\n"); foreach ($containers as $container) { $container = $objectStoreService->getContainer($container->name); printf("Name:%-60s,BytesUsed:%15.6fM,ObjectCount:%d",$container->name,$container->getBytesUsed() / 1024 / 1024,$container->getObjectCount()); $bytesQuota = $container->getBytesQuota(); if($bytesQuota) printf(",BytesQuota:%.6fM",$bytesQuota / 1024 / 1024); printf("\n"); } //3.容器添加對象 $container = $objectStoreService->getContainer($containerName); //添加文件 $customHeaders = array('Content-Type' => 'text/plain'); $fileData = fopen('D:/upload_small_object.txt', 'r'); $object = $container->uploadObject('upload_small_object.txt', $fileData,$customHeaders); //添加目錄(注意后面的斜杠) $dirName = 'dir1/'; $object = $container->uploadObject($dirName,''); //目錄添加文件(文件放到目錄中,必須在文件名前加上目錄,形成類似a/b/c/d/1.txt結構(目錄a/,目錄a/b/,目錄a/b/c/,目錄a/b/c/d/)) $fileData = fopen('D:/upload_small_object.txt', 'r'); $object = $container->uploadObject($dirName . 'upload_small_object.txt', $fileData,$customHeaders); //目錄下再添加目錄 $dirName = $dirName . 'dir1-1/'; $object = $container->uploadObject($dirName,''); //二級目錄下再添加文件 $fileData = fopen('D:/upload_small_object.txt', 'r'); $object = $container->uploadObject($dirName . 'upload_small_object.txt', $fileData,$customHeaders); //4.枚舉對象 printf("%s Objects-----------------------------------------------------------\n",$containerName); //是目錄的最后一個字符是斜杠 $objects = $container->objectList(); foreach ($objects as $object) { printf("Name:%-60s,Size:%15.6fM,LastModified:%s\n",$object->getName(),$object->getContentLength(),$object->getLastModified()); } //5.枚舉目錄下的對象(設定前綴) printf("%s Objects---------------------------------------------------------------\n",$dirName); $objects = $container->objectList(array('prefix' => $dirName)); foreach ($objects as $object) { printf("Name:%-60s,Size:%15.6fM,LastModified:%s\n",$object->getName(),$object->getContentLength(),$object->getLastModified()); } //6.下載文件 $object = $container->getObject($dirName . 'upload_small_object.txt'); $objectContent = $object->getContent(); $objectContent->rewind(); $stream = $objectContent->getStream(); file_put_contents('D:/123.txt', $stream); //7.刪除容器(刪除云盤使用者時刪除對應云盤) $objects = $container->objectList(); //循環刪除每個對象(不支持批刪) foreach ($objects as $object) { $object->delete(); } $container->delete();
感謝你能夠認真閱讀完這篇文章,希望小編分享的“openstack中php sdk php-opencloud怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。