六、java操作swift对象存储(resultful方式实现)
系列导航
六、java操作swift对象存储(resultful方式实现)
上一节讲了如何通过swift官方的样例操作swift对象存储,官方样例中,种类还是有点少工作中不够用,但是swift集群的调用支持resultful这就给我们提供了另一种调用swift的方法。那么都支持那些操作类型呢请看下表。
一、 RESTful API 总结
资源类型 URL GET PUT POST DELETE HEAD
账户 /account/ 获取容器列表 - - - 获取账户元数据
容器 /account/container 获取对象列表 创建容器 更新容器元数据 删除容器 获取容器元数据
对象 /account/container/object 获取对象内容和元数据 创建、更新或拷贝对象 更新对象元数据 删除对象 获取对象元数据
了解了以上知识后下面进入代码部分java使用resultful方式操作swift对象存储。
二、pom.xml依赖
<dependencies> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.3.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency> <!--JSONArray--> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180130</version> </dependency>
三、方法类ResultFulBase.java
package swift.base; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.FileEntity; import org.apache.http.entity.InputStreamEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; //resultFul接口方式实现基础操作(tempauth认证方式) public class ResultFulBase { //访问swift的url地址 public static String X_Storage_Url = ""; //访问swift的令牌 public static String X_Auth_Token = ""; //分割符 private static final String separator = System.getProperty("file.separator"); public ResultFulBase(String url, String port, String user, String passwd) { get_AUTH(url, port, user, passwd); } //获取X-Storage-Url 和 X-Auth-Token begin //curl http://127.0.0.1:8080/auth/v1.0 -v -H 'X-Storage-User: admin:admin' -H 'X-Storage-Pass: admin' public void get_AUTH(String url, String port, String user, String passwd) { try { HttpGet req = new HttpGet("http://" + url + ":" + port + "/auth/v1.0"); req.addHeader("X-Storage-User", user); req.addHeader("X-Storage-Pass", passwd); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); Header h1 = rsp.getFirstHeader("X-Storage-Url"); Header h2 = rsp.getFirstHeader("X-Auth-Token"); X_Storage_Url = h1.getValue(); X_Auth_Token = h2.getValue(); System.out.println(h1.getValue() + "---------" + h2.getValue()); System.out.println("----------------------------------------"); //获取X-Storage-Url 和 X-Auth-Token end } catch (Exception e) { e.printStackTrace(); } } //查看账户信息 //curl http://127.0.0.1:8080/v1/AUTH_admin -v -H 'X-Auth-Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36' public void get_acount_info() { try { HttpGet req = new HttpGet(X_Storage_Url); req.addHeader("X-Auth-Token", X_Auth_Token); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //创建 container(container2为创建的容器名称) 按照默认策略创建容器 //curl http://127.0.0.1:8080/v1/AUTH_admin/container2 -X PUT -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36" public void create_container(String containerName) { try { HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName); req.addHeader("X-Auth-Token", X_Auth_Token); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //创建 container(containerpolicy2为创建的容器名称) 按照存储策略创建容器 //curl http://127.0.0.1:8080/v1/AUTH_admin/containerpolicy2 -X PUT -H "X-Auth_Token: AUTH_tk7c21eddae31d449db36a5e8596d0cb71" -H "X-Storage-Policy: silver" //Policy是存储策略的名称 public void create_container(String containerName, String Policy) { try { HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName); req.addHeader("X-Auth-Token", X_Auth_Token); req.addHeader("X-Storage-Policy", Policy); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //获取AUTH_admin账户下的容器container1的对象列表 //curl http://127.0.0.1:8080/v1/AUTH_admin/container1 -X GET -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36" public void get_container_object_list(String containerName) { try { HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName); req.addHeader("X-Auth-Token", X_Auth_Token); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); HttpEntity entity = rsp.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity); String[] objectList = result.split("\n"); for (int i = 0; i < objectList.length; i++) { System.out.println("--" + objectList[i]); } } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //获取容器下指定条数的对象列表(有的容器下对象太多,对象都拿出来服务器压力很大) //limit 整数n,指明返回结果只包含n个值 public void get_container_object_limit_list(String containerName, int limit) { try { HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName + "/?limit=" + limit); req.addHeader("X-Auth-Token", X_Auth_Token); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); HttpEntity entity = rsp.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity); String[] objectList = result.split("\n"); for (int i = 0; i < objectList.length; i++) { System.out.println("--" + objectList[i]); } } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //获取符合查询条件的对象列表 //marker 字符串x,返回名称比这个特定的字符串x大的container列表 >= //end_marker 字符串x,返回名称比这个特定字符串小的container列表 < // 注: 对于md5码 对比的是 第一个字母(经验观察,不是官网内容) public void get_container_object_marker_list(String containerName, String marker, String end_marker) { try { HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName + "/?marker=" + marker + "&end_marker=" + end_marker); req.addHeader("X-Auth-Token", X_Auth_Token); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); HttpEntity entity = rsp.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity); String[] objectList = result.split("\n"); for (int i = 0; i < objectList.length; i++) { System.out.println("--" + objectList[i]); } } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //创建、更新或拷贝对象 //curl http://127.0.0.1:8080/v1/AUTH_admin/container1/1.txt -X PUT -T 1.txt -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36" public void create_object(String containerName, String objectName, String filePath) { try { HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName); req.addHeader("X-Auth-Token", X_Auth_Token); File file = new File(filePath); FileEntity entity1 = new FileEntity(file, "text/plain; charset=\"UTF-8\""); req.addHeader("Content-Type", "image/jpeg"); req.addHeader("Content-Type", "text/plain; charset=\"UTF-8\""); req.setEntity(entity1); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //创建对象及Meta //curl http://127.0.0.1:8080/v1/AUTH_admin/container1/1.txt -X PUT -T 1.txt -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36" public void create_object_meta(String containerName, String objectName, String filePath) { try { HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName); req.addHeader("X-Auth-Token", X_Auth_Token); req.addHeader("X-Object-Meta-idNum", "123123123"); File file = new File(filePath); FileEntity entity1 = new FileEntity(file, "text/plain; charset=\"UTF-8\""); req.addHeader("Content-Type", "image/jpeg"); req.addHeader("Content-Type", "text/plain; charset=\"UTF-8\""); req.setEntity(entity1); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //获取对象内容和元数据 //curl http://127.0.0.1:8080/v1/AUTH_admin/container1/2.txt -X GET -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36" public void get_container_object(String containerName, String objectName, String path) { try { HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName + "/" + objectName); req.addHeader("X-Auth-Token", X_Auth_Token); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); if (HttpStatus.SC_OK == rsp.getStatusLine().getStatusCode()) { HttpEntity entity = rsp.getEntity(); if (entity != null) { InputStream input = entity.getContent(); write(input, path); } } Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //判断文件是否存在 //curl http://127.0.0.1:8080/v1/AUTH_admin/container1/2.txt -X GET -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36" public boolean exist_container_object(String containerName, String objectName) { boolean result = false; try { HttpGet req = new HttpGet(X_Storage_Url + "/" + containerName + "/" + objectName); req.addHeader("X-Auth-Token", X_Auth_Token); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); if (HttpStatus.SC_OK == rsp.getStatusLine().getStatusCode()) { HttpEntity entity = rsp.getEntity(); if (entity != null) { result = true; } } } catch (Exception e) { e.printStackTrace(); } return result; } //删除对象 // curl http://127.0.0.1:8080/v1/AUTH_admin/container1/2.txt -X DELETE -H "X-Auth_Token: AUTH_tkf44c48c200e34f14850f6a8e3bc63f36" public void delete_object(String containerName, String objectName) { try { HttpDelete req = new HttpDelete(X_Storage_Url + "/" + containerName + "/" + objectName); req.addHeader("X-Auth-Token", X_Auth_Token); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //写文件 private void write(InputStream is, String path) { try { OutputStream stream = new BufferedOutputStream(new FileOutputStream(path)); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int len = 0; while ((len = is.read(buffer)) != -1) { stream.write(buffer, 0, len); } stream.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } } //以文件地址方式向容器中放入数据,数据名称为文件的md5码 public void create_md5_object(String containerName, String filePath) { try { String objectName = " "; objectName = DigestUtils.md5Hex(new FileInputStream(filePath)); System.out.println(objectName); HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName); req.addHeader("X-Auth-Token", X_Auth_Token); File file = new File(filePath); FileInputStream fileInputStream = new FileInputStream(file); InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream, file.length()); req.addHeader("Content-Type", "application/octet-stream"); req.setEntity(reqEntity); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //以文件base64编码方式向容器中放入数据,数据名称为文件的md5码 public void create_base64_object(String containerName, String base64String) { try { String objectName = " "; objectName = DigestUtils.md5Hex(base64String); System.out.println(objectName); HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName); req.addHeader("X-Auth-Token", X_Auth_Token); byte[] content = Base64.decodeBase64(base64String); ByteArrayEntity reqEntity = new ByteArrayEntity(content); req.addHeader("Content-Type", "application/octet-stream"); req.setEntity(reqEntity); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //以文件base64编码方式向容器中放入数据,数据名称为文件的md5码 public void create_base64_object(String containerName, String base64String, String objectName) { try { HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName); req.addHeader("X-Auth-Token", X_Auth_Token); byte[] content = Base64.decodeBase64(base64String); ByteArrayEntity reqEntity = new ByteArrayEntity(content); req.addHeader("Content-Type", "application/octet-stream"); req.setEntity(reqEntity); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //以文件base64编码方式向容器中放入数据,数据名称为文件的md5码 public void create_base64_object(String containerName, String base64String, String objectName, String metaData) { try { HttpPut req = new HttpPut(X_Storage_Url + "/" + containerName + "/" + objectName); req.addHeader("X-Auth-Token", X_Auth_Token); req.addHeader("X-Object-Meta-idNum", metaData); byte[] content = Base64.decodeBase64(base64String); ByteArrayEntity reqEntity = new ByteArrayEntity(content); req.addHeader("Content-Type", "application/octet-stream"); req.setEntity(reqEntity); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } //写入Temp-URL-Key public void create_key(String key) { try { HttpPut req = new HttpPut(X_Storage_Url); req.addHeader("X-Auth-Token", X_Auth_Token); req.addHeader("Content-Type", "image/jpeg"); req.addHeader("X-Account-Meta-Temp-Url-Key", key); req.addHeader("Content-Type", "text/plain; charset=\"UTF-8\""); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse rsp = httpclient.execute(req); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); } catch (Exception e) { e.printStackTrace(); } } }
四、方法调用的主类ResultFulBaseMain.java
package swift.main; import swift.base.ResultFulBase; //resultFulBase.Java的调用测试 public class ResultFulBaseMain { /** * @param args */ public static void main(String[] args) { ResultFulBase resultFulBase = new ResultFulBase("192.168.0.1", "8080", "admin:admin", "admin"); //一、获取连接swift的连接和令牌 resultFulBase.get_AUTH("192.168.0.1","8080","admin:admin","admin"); //二、查看账户信息 resultFulBase.get_acount_info(); //三、按照默认存储策略创建容器 resultFulBase.create_container("containerName1"); //四、指定存储策略创建容器 resultFulBase.create_container("containerName2","silver"); //五、查看容器下对象列表 resultFulBase.get_container_object_list("containerName1"); //六、获取容器下指定条数的对象列表 resultFulBase.get_container_object_limit_list("containerName1",2); //七、获取符合查询条件的对象列表 resultFulBase.get_container_object_marker_list("containerName1","1","9"); //八、创建对象 resultFulBase.create_object("containerName2","1.txt","E://1.txt"); //九、创建对象及对象的属性Meta resultFulBase.create_object_meta("containerName2","2.txt","E://2.txt"); //十、获取对象内容和元数据 resultFulBase.get_container_object("containerName2","2.txt","E://99.txt"); //十一、判断文件是否存在 boolean flag = resultFulBase.exist_container_object("containerName2","2.txt" ); System.out.println(flag); flag = resultFulBase.exist_container_object("containerName2","4.txt" ); System.out.println(flag); //十二、删除对象 resultFulBase.delete_object("containerName2","1.txt" ) ; //十三、以文件地址方式向容器中放入数据,数据名称为文件的md5码 resultFulBase.create_md5_object("containerName2","E://1.txt"); //十四、以文件base64编码方式向容器中放入数据,数据名称为文件的md5码 resultFulBase.create_base64_object("containerName2","txbb01d60aaa174f26b20e5-005eec4823"); //十五、写入Temp-URL-Key resultFulBase.create_key("1233"); } }