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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM