Ceph的Python接口

参考文章

ceph的python_api文档: http://docs.ceph.com/docs/master/rados/api/python/

连接ceph集群

import rados
cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')
cluster.connect()

创建与删除池

# 列出可用的池
pools = cluster.list_pools()
for pool in pools:
    print pool
# 创建池test
cluster.create_pool('test')
# 删除池
cluster.delete_pool('test')
# 判断是否存在一个池
cluster.pool_exists('test')

列出池中所有的文件名

ioctx = cluster.open_ioctx('test')
# 列出test池中的所有文件名
object_iterator = ioctx.list_objects()
while True :
    try :
        rados_object = object_iterator.next()
        print "Object contents = " + rados_object.key
    except StopIteration :
        break
ioctx.close()

上传文件

# 连接到test池
ioctx = cluster.open_ioctx('test')
file_name = "yy.mp3"
f = open("yy.mp3", "r")
file_content = f.read()
f.close()
# 将文件写入池
ioctx.write_full(file_name, file_content)
ioctx.close()

下载文件

# 连接到test池
ioctx = cluster.open_ioctx('test')
f = open("yy.mp3", "w")
# 将文件下载(写入)到本地
f.write(ioctx.read("yy.mp3"))
f.close()
ioctx.close()

删除文件

ioctx = cluster.open_ioctx('test')
# 删除test池中的yy.mp3文件
ioctx.remove_object("yy.mp3")
ioctx.close()

断开ceph集群连接

cluster.shutdown()