ceph常用的接口有librados和s3api,s3api与ceph对象网关RGW连接,往ceph集群存储数据,librados是对象网关RGW的下层,运行效率更高,性能更好。
使用librados可以操作ceph集群,它提供了存储,读取,删除,列出所有对象等功能。
在存储前,需要连接集群: Rados cluster = new Rados("admin"); File f = new File("/etc/ceph/ceph.conf"); cluster.confReadFile(f); cluster.connect(); 创建IO上下文,使用cluster.ioCtxCreate()方法,参数是要使用的存储池pool IoCTX io = cluster.ioCtxCreate("{yourpool}"); 最重要的存储方法是 io.write(),参数分别是:对象名,对象内容 io.write(objectname, objectcontent); //如果要存储文件,需要把文件变成输入流传进去。如 File ff = new File(path); length = (int) ff.length(); byte[] data = new byte[length]; new FileInputStream(ff).read(data); io.write(oid,data,offset); Java-rados demo: import com.ceph.rados.IoCTX; import com.ceph.rados.Rados; import com.ceph.rados.exceptions.RadosException; import java.io.File; public class CephClient { public static void main (String args[]){ try { //创建集群句柄 Rados cluster = new Rados("admin"); System.out.println("Created cluster handle."); //读取配置文件 File f = new File("/etc/ceph/ceph.conf"); cluster.confReadFile(f); System.out.println("Read the configuration file."); //连接集群 cluster.connect(); System.out.println("Connected to the cluster."); //创建上下文 IoCTX io = cluster.ioCtxCreate("data"); //存一个字符串对象 String oidone = "kyle-say"; String contentone = "Hello World!"; //存储使用write方法,参数为对象名和对象内容 io.write(oidone, contentone); //打印之前存进去的对象名字 String[] objects = io.listObjects(); for (String object: objects){ System.out.println("Put " + object); } //删除数据用的remove方法,参数为对象名 io.remove(oidone); //存文件 int length; File ff = new File("{file path}"); length = (int) ff.length(); byte[] data = new byte[length]; new FileInputStream(ff).read(data); ioCTX.write("{文件名}",data); //关闭上下文 cluster.ioCtxDestroy(io); } catch (RadosException e) { System.out.println(e.getMessage() + ": " + e.getReturnValue()); } } } 官方文档: http://docs.ceph.org.cn/rados/api/librados-intro/Ceph 支持 REST 风格的 API ,它与亚马逊 S3 API 的基本数据访问模型兼容。S3 API的pythondemo如下:
import boto import boto.s3.connection from boto.s3.key import Key #使用s3cfg文件里的access_key,secret_key,hostname确定连接那个机器 access_key = 'S2WEB1BIYXW35EX1YANG' secret_key = 'bgp5OvRJ640LJ46x5m6UANoEMQ5WSyImou8AFJPv' #建立连接 conn = boto.connect_s3( aws_access_key_id = access_key, aws_secret_access_key = secret_key, host = 'ceph', port = 7480, is_secure=False, calling_format = boto.s3.connection.OrdinaryCallingFormat(), ) #列出你的 bucket 的列表,也会打印出每个bucket的 bucket 名和创建时间 for bucket in conn.get_all_buckets(): print "{name}\t{created}".format( name = bucket.name, created = bucket.creation_date, ) #下面的代码会输出 bucket 内的所有对象列表。 这也会打印出每一个对象的名字、文件尺寸和最近修改时间 for key in bucket.list(): print "{name}\t{size}\t{modified}".format( name = key.name, size = key.size, modified = key.last_modified, ) #删除bucket conn.delete_bucket(bucket.name) #下面的代码会新建一个内容是字符串“Hello World!” 的文件 hello.txt。 key = bucket.new_key('hello.txt') key.set_contents_from_string('Hello World!') #下面的代码会下载对象 perl_poetry.pdf 并将它存到指定路径,如/home/larry/documents/perl_poetry.pdf key = bucket.get_key('perl_poetry.pdf') key.get_contents_to_filename('/home/larry/documents/perl_poetry.pdf') #下面的代码会删除对象 goodbye.txt bucket.delete_key('goodbye.txt') '''下面的代码会为 hello.txt 生成一个无签名为下载URL。 这个操作是生效是因为前面我们已经设置 hello.txt 的 ACL 为公开可读。 下面的代码同时会为 secret_plans.txt 生成一个有效时间是一个小时的带签名的下载 URL。 带签名的下载 URL 在这个时间内是可用的,即使对象的权限是私有(当时间到期后 URL 将不可用)。''' hello_key = bucket.get_key('hello.txt') hello_url = hello_key.generate_url(0, query_auth=False, force_http=True) print hello_url plans_key = bucket.get_key('secret_plans.txt') plans_url = plans_key.generate_url(3600, query_auth=True, force_http=True) print plans_url 官方文档: http://docs.ceph.org.cn/radosgw/s3/python/ http://docs.ceph.org.cn/radosgw/s3/