Home  >  Article  >  Backend Development  >  【PHP SDK for OpenStack/Rackspace APIs】使用对象储存服务

【PHP SDK for OpenStack/Rackspace APIs】使用对象储存服务

WBOY
WBOYOriginal
2016-06-13 11:34:10736browse

【PHP SDK for OpenStack/Rackspace APIs】使用对象存储服务

Swift是OpenStack的对象存储服务。在php-opencloud库中,通过connection对象创建的ObjectStore类(OpenStack或Rackspace)来存取。

例如:

$cloud = new \OpenCloud\OpenStack(array(    'username'=>'{username}','password'=>'{password}'));$swift = $cloud->ObjectStore('cloudFiles','DFW');
使用新创建的$swift,你可以使用不同的对象存储组件。

最高级别的对象存储组件实例是Container,Container是对象的集合名称,与文件系统中的目录和文件夹类似(实际上并不等同)。

所有的对象都保存在Container中。

在一个对象存储实例中列举所有Container

ContainerList对象是Container对象的集合。列举对象存储实例中的所有Container:

$containers = $swift->ContainerList();while($container = $containers->Next())    printf("%s\n", $container->name);
就像其他的对象集合,这也支持First(), Next()和Size()方法。

创建一个新Container

使用上面新创建的$swift对象的Container()方法创建一个新的(空的)的container。

$mycontainer = $swift->Container();
将该Container保存到对象存储实例中,使用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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn