【PHP SDK for OpenStack/Rackspace APIs】使用Object Storage Service服務
Swift是OpenStack的Object Storage Service服務。在php-opencloud庫中,通過connection對象建立的ObjectStore類(OpenStack或Rackspace)來存取。
例如:
$cloud = new \OpenCloud\OpenStack(array( 'username'=>'{username}','password'=>'{password}'));$swift = $cloud->ObjectStore('cloudFiles','DFW');
使用新建立的$swift,你可以使用不同的Object Storage Service組件。
最進階別的Object Storage Service組件執行個體是Container,Container是對象的集合名稱,與檔案系統中的目錄和檔案夾類似(實際上並不等同)。
所有的對象都儲存在Container中。
在一個Object Storage Service執行個體中列舉所有Container
ContainerList對象是Container對象的集合。列舉Object Storage Service執行個體中的所有Container:
$containers = $swift->ContainerList();while($container = $containers->Next()) printf("%s\n", $container->name);
就像其他的對象集合,這也支援First(), Next()和Size()方法。
建立一個新Container
使用上面新建立的$swift對象的Container()方法建立一個新的(空的)的container。
$mycontainer = $swift->Container();
將該Container儲存到Object Storage Service執行個體中,使用Create()方法:
$mycontainer->Create('MyContainerName');
name不是必須在Create()方法中,如果name已經被設定的話。直接在方法中指定名稱也是很方便的。
$mycontainer->name = 'MyContainerName';$mycontainer->Create();
檢索已存在的Container
如果你傳遞一個參數到ObjectStore對象的Container()方法中,可以檢索一個已存在的Container:
$oldcontainer = $swift->Container('SomeOldContainer');
在這種情況下,關於SomeOldContainer的資訊將被檢索。這包含Container的metadata資訊。
printf("Container %s has %d object(s) consuming %d bytes\n", $oldcontainer->name, $oldcontainer->count, $oldcontainer->bytes);
刪除Container
Delete()方法刪除Container
$oldcontainer->Delete();
請注意,Container被刪除時必須是空的,也就是說必須沒有對象與它相關聯。
更新Container
在後台,容器建立和更新方式完全相同。你可以使用Create()方法來更新Container;然而,Update()方法也被作為Create()方法的別名而存在,因為這在語意學上可能不同(在你的程式中):
$oldcontainer->metadata->update_time = time();$oldcontainer->Update();
翻譯自:https://github.com/rackspace/php-opencloud/blob/master/docs/userguide/objectstore.md