使用 OpenStack 对象存储

在使用对象存储服务之前,您需要通过遵循 连接 用户指南来创建与您的 OpenStack 云的连接。这将为您提供在以下示例中使用的 conn 变量。

对象存储服务的主要资源是容器和对象。

使用容器

列出容器

要列出现有的容器,请使用 containers() 方法。

>>> for cont in conn.object_store.containers():
...     print cont
...
openstack.object_store.v1.container.Container: {u'count': 5,
u'bytes': 500, u'name': u'my container'}
openstack.object_store.v1.container.Container: {u'count': 0,
u'bytes': 0, u'name': u'empty container'}
openstack.object_store.v1.container.Container: {u'count': 100,
u'bytes': 1000000, u'name': u'another container'}

containers 方法返回一个生成器,该生成器产生 Container 对象。它会为您处理分页,可以通过 limit 参数进行调整。默认情况下,containers 方法将产生服务返回的尽可能多的容器,并且将继续请求直到不再接收到更多容器为止。

>>> for cont in conn.object_store.containers(limit=500):
...     print(cont)
...
<500 Containers>
... another request transparently made to the Object Store service
<500 more Containers>
...

创建容器

要创建容器,请使用 create_container() 方法。

>>> cont = conn.object_store.create_container(name="new container")
>>> cont
openstack.object_store.v1.container.Container: {'name': u'new container'}

使用容器元数据

要获取容器的元数据,请使用 get_container_metadata() 方法。此方法接受容器的名称或 Container 对象,并返回一个带有所有元数据属性设置的 Container 对象。

>>> cont = conn.object_store.get_container_metadata("new container")
openstack.object_store.v1.container.Container: {'content-length': '0',
'x-container-object-count': '0', 'name': u'new container',
'accept-ranges': 'bytes',
'x-trans-id': 'tx22c5de63466e4c05bb104-0054740c39',
'date': 'Tue, 25 Nov 2014 04:57:29 GMT',
'x-timestamp': '1416889793.23520', 'x-container-read': '.r:mysite.com',
'x-container-bytes-used': '0', 'content-type': 'text/plain; charset=utf-8'}

要设置容器的元数据,请使用 set_container_metadata() 方法。此方法接受一个 Container 对象。例如,要授予另一个用户对该容器的写入访问权限,您可以调用 set_container_metadata,传递要更新的 Container 以及表示元数据名称和值的关键字参数对。

>>> acl = "big_project:another_user"
>>> conn.object_store.set_container_metadata(cont, write_ACL=acl)
openstack.object_store.v1.container.Container: {'content-length': '0',
'x-container-object-count': '0',
'name': u'my new container', 'accept-ranges': 'bytes',
'x-trans-id': 'txc3ee751f971d41de9e9f4-0054740ec1',
'date': 'Tue, 25 Nov 2014 05:08:17 GMT',
'x-timestamp': '1416889793.23520', 'x-container-read': '.r:mysite.com',
'x-container-bytes-used': '0', 'content-type': 'text/plain; charset=utf-8',
'x-container-write': 'big_project:another_user'}

使用对象

对象存储在容器中。从 API 的角度来看,您使用类似命名的方法来处理它们,通常会添加一个额外的参数来指定它们的容器。

列出对象

要列出容器中存在的对象,请使用 objects() 方法。

如果您有一个 Container 对象,您可以将其传递给 objects

>>> print cont.name
pictures
>>> for obj in conn.object_store.objects(cont):
...     print obj
...
openstack.object_store.v1.container.Object:
{u'hash': u'0522d4ccdf9956badcb15c4087a0c4cb',
u'name': u'pictures/selfie.jpg', u'bytes': 15744,
'last-modified': u'2014-10-31T06:33:36.618640',
u'last_modified': u'2014-10-31T06:33:36.618640',
u'content_type': u'image/jpeg', 'container': u'pictures',
'content-type': u'image/jpeg'}
...

containers() 方法类似,objects 返回一个生成器,该生成器产生存储在容器中的 Object 对象。它还会为您处理分页,您可以使用 limit 参数进行调整,否则每次请求 Object Store 将返回的最大值。

如果您有容器的名称而不是对象,也可以将其传递给 objects 方法。

>>> for obj in conn.object_store.objects("pictures".decode("utf8"),
                                         limit=100):
...     print obj
...
<100 Objects>
... another request transparently made to the Object Store service
<100 more Objects>

获取对象数据

一旦您拥有一个 Object,您可以使用 get_object_data() 方法获取其中存储的数据。

>>> print ob.name
message.txt
>>> data = conn.object_store.get_object_data(ob)
>>> print data
Hello, world!

此外,如果您想将对象保存到磁盘,download_object() 实用方法接受一个 Object 和一个 path 来写入内容。

>>> conn.object_store.download_object(ob, "the_message.txt")

上传对象

一旦您拥有想要存储在对象存储服务中的数据,您可以使用 upload_object() 方法。此方法接受要存储的 data,以及至少一个对象 name 和要存储在其中的 container

>>> hello = conn.object_store.upload_object(container="messages",
                                            name="helloworld.txt",
                                            data="Hello, world!")
>>> print hello
openstack.object_store.v1.container.Object: {'content-length': '0',
'container': u'messages', 'name': u'helloworld.txt',
'last-modified': 'Tue, 25 Nov 2014 17:39:29 GMT',
'etag': '5eb63bbbe01eeed093cb22bb8f5acdc3',
'x-trans-id': 'tx3035d41b03334aeaaf3dd-005474bed0',
'date': 'Tue, 25 Nov 2014 17:39:28 GMT',
'content-type': 'text/html; charset=UTF-8'}

使用对象元数据

使用对象上的元数据与使用容器上的元数据相同。您可以使用 get_object_metadata()set_object_metadata() 方法。

要在设置的元数据属性可以在 Object 对象上找到。

>>> secret.delete_after = 300
>>> secret = conn.object_store.set_object_metadata(secret)

我们将 delete_after 值设置为 500 秒,导致对象在 300 秒后或五分钟后被删除。该属性对应于 X-Delete-After 标头值,您可以看到在检索更新后的元数据时返回该值。

>>> conn.object_store.get_object_metadata(ob)
openstack.object_store.v1.container.Object: {'content-length': '11',
'container': u'Secret Container',
'name': u'selfdestruct.txt', 'x-delete-after': 300,
'accept-ranges': 'bytes', 'last-modified': 'Tue, 25 Nov 2014 17:50:45 GMT',
'etag': '5eb63bbbe01eeed093cb22bb8f5acdc3',
'x-timestamp': '1416937844.36805',
'x-trans-id': 'tx5c3fd94adf7c4e1b8f334-005474c17b',
'date': 'Tue, 25 Nov 2014 17:50:51 GMT', 'content-type': 'text/plain'}