五、java操作swift对象存储(官网样例)
系列导航
六、java操作swift对象存储(resultful方式实现)
swift的官方提供了一些java操作swift对象存储的操作方法,但是内容比较简单稍微复杂的没有找到合适的样例,后面我会使用http请求的方式实现
一、jar包
org-json.jar 需要使用maven的同学们自己找到依赖转换一下就可以
二、基础方法
package base; //官网的操作样例 import org.javaswift.joss.client.core.ContainerPaginationMap; import org.javaswift.joss.client.factory.AccountConfig; import org.javaswift.joss.client.factory.AccountFactory; import org.javaswift.joss.client.factory.AuthenticationMethod; import org.javaswift.joss.model.Account; import org.javaswift.joss.model.Container; import org.javaswift.joss.model.PaginationMap; import org.javaswift.joss.model.StoredObject; import java.io.File; import java.util.Collection; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; public class SwiftClientWork { private static final String module = SwiftClientWork.class.getName(); static String authurl = "http://192.168.0.1:8080/auth/v1.0"; static String username = "admin:admin"; static String password = "admin"; /** * 身份证存储容器 */ static Account account ; private static SwiftClientWork instance = null; public static synchronized SwiftClientWork getInstance(){ if(instance==null){ instance = new SwiftClientWork(); } return instance; } static { try{ AccountConfig config = new AccountConfig(); config.setUsername(username); config.setPassword(password); config.setAuthUrl(authurl); config.setAuthenticationMethod(AuthenticationMethod.BASIC); account = new AccountFactory(config).createAccount(); }catch (Exception e){ e.printStackTrace(); } } //给账户设置Temp-Url-Key public void setTempUrlKey(String key){ account.setAndSaveMetadata("Temp-Url-Key", key); System.out.println(account.getMetadata()); } /** * CREATE A CONTAINER * @param containerName */ public void createContainer(String containerName){ try{ Container container = account.getContainer(containerName); container.create(); }catch (Exception e){ e.printStackTrace(); } } /** * DELETE A CONTAINER * @param containerName */ public void deleteContainer(String containerName){ try{ Container container = account.getContainer(containerName); container.delete(); }catch (Exception e){ e.printStackTrace(); } } /** * CREATE AN OBJECT * @param containerName * @param objectName * @param inFilePath */ public StoredObject createObject(String containerName, String objectName, String inFilePath){ try{ /** StoredObject object = container.getObject("dog.png"); object.uploadObject(new File("/dog.png")); System.out.println("Public URL: "+object.getPublicURL()); */ Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); object.uploadObject(new File(inFilePath)); /*InputStream input = new FileInputStream(new File(inFilePath)); object.uploadObject(input); input.close();*/ /*System.out.println("Public URL: "+object.getPublicURL()); System.out.println("Last modified: "+object.getLastModified()); System.out.println("ETag: "+object.getEtag()); System.out.println("Content type: "+object.getContentType()); System.out.println("Content length: "+object.getContentLength());*/ return object; }catch (Exception e){ e.printStackTrace(); } return null; } /** * * @param containerName * @param objectName * @param inFile * @return */ public StoredObject createObject(String containerName, String objectName, File inFile){ try{ /** StoredObject object = container.getObject("dog.png"); object.uploadObject(new File("/dog.png")); System.out.println("Public URL: "+object.getPublicURL()); */ Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); object.uploadObject(inFile); /*InputStream input = new FileInputStream(new File(inFilePath)); object.uploadObject(input); input.close();*/ /*System.out.println("Public URL: "+object.getPublicURL()); System.out.println("Last modified: "+object.getLastModified()); System.out.println("ETag: "+object.getEtag()); System.out.println("Content type: "+object.getContentType()); System.out.println("Content length: "+object.getContentLength());*/ return object; }catch (Exception e){ e.printStackTrace(); } return null; } /** * ADD/UPDATE OBJECT METADATA * @param containerName * @param objectName * @param metadatas */ public void addOrUpdateObjectMetadata(String containerName, String objectName, Map<String, Object> metadatas){ try{ Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); Map<String, Object> metadata = new TreeMap<String, Object>(); for(String key: metadatas.keySet()){ String value = metadatas.get(key).toString(); System.out.println("key:"+key+" vlaue:"+value); metadata.put(key, value); } //Map<String, Object> metadata = new TreeMap<String, Object>(); //metadata.put("key", "value"); object.setMetadata(metadata); }catch (Exception e){ e.printStackTrace(); } } //上传一个带属性值的对象 public StoredObject createObjectWithMetadata(String containerName, String objectName, String inFilePath, Map<String, Object> metadatas){ try{ Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); object.uploadObject(new File(inFilePath)); Map<String, Object> metadata = new TreeMap<String, Object>(); for(String key: metadatas.keySet()){ String value = metadatas.get(key).toString(); System.out.println("key:"+key+" vlaue:"+value); metadata.put(key, value); } object.setMetadata(metadata); return object; }catch (Exception e){ e.printStackTrace(); } return null; } /** * LIST OWNED CONTAINERS */ public Collection<Container> containerList(){ try{ Collection<Container> containers = account.list(); for (Container currentContainer : containers) { System.out.println(currentContainer.getName()); } return containers; }catch (Exception e){ e.printStackTrace(); } return null; } /** * LIST A CONTAINER’S CONTENT * @param containerName * @return */ public Collection<StoredObject> containerObjects(String containerName){ try{ Container container = account.getContainer(containerName); Collection<StoredObject> objects = container.list(); for (StoredObject currentObject : objects) { System.out.println(currentObject.getName()); } return objects; }catch (Exception e){ e.printStackTrace(); } return null; } /** * LIST A CONTAINER’S CONTENT * @param containerName * @param prefix * @param pageSize * @return */ public void containerObjectsPage(String containerName,String prefix,int pageSize){ try{ Container container = account.getContainer(containerName); //ContainerPaginationMap(Container container, String prefix, Integer pageSize) PaginationMap paginationMap = new ContainerPaginationMap(container,prefix,pageSize); //list(PaginationMap paginationMap, int page) //Returns a number equal to pageSize of Container elements, starting with the first element after the Container named the same as marker. //后面这个参数经过测试不起任何作用 Collection<StoredObject> objects =container.list(paginationMap,50000); for (StoredObject currentObject : objects) { System.out.println(currentObject.getName()); } /* Iterator iter = container.list(paginationMap,1).iterator(); while(iter.hasNext()){ StoredObject object = (StoredObject) iter.next(); String fileName = object.getName(); System.out.println(fileName); } */ }catch (Exception e){ e.printStackTrace(); } } public Map getObjectMetadaTest(String containerName, String objectName) { try { Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); Map<String, Object> returnedMetadata = object.getMetadata(); // for (String name : returnedMetadata.keySet()) { // System.out.println("META / " + name + ": " + returnedMetadata.get(name)); // } return returnedMetadata; } catch (Exception e) { System.out.println("该对象没有元数据属性------------->"); //e.printStackTrace(); } return null; } /** * RETRIEVE AN OBJECT’S METADATA * @param containerName * @param objectName */ public void getObjectMetada(String containerName, String objectName){ try{ Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); Map<String, Object> returnedMetadata = object.getMetadata(); for (String name : returnedMetadata.keySet()) { System.out.println("META / "+name+": "+returnedMetadata.get(name)); } }catch (Exception e){ e.printStackTrace(); } } /** * RETRIEVE AN OBJECT * @param containerName * @param objectName * @param downPath */ public void getObject(String containerName, String objectName, String downPath){ try{ Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); object.downloadObject(new File(downPath)); /*System.out.println("Public URL: "+object.getPublicURL()); System.out.println("Last modified: "+object.getLastModified()); System.out.println("ETag: "+object.getEtag()); System.out.println("Content type: "+object.getContentType()); System.out.println("Content length: "+object.getContentLength());*/ }catch (Exception e){ e.printStackTrace(); } } /** * DELETE AN OBJECT * @param containerName * @param objectName */ public void deleteObject(String containerName, String objectName){ try{ Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); object.delete(); }catch (Exception e){ e.printStackTrace(); } } //获取get权限的tempurl //second 有效时间 单位秒 public void getObjectGetTempUrl(String containerName, String objectName,int second ){ try{ Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); String url=object.getTempGetUrl(second); url = url +"&inline"; System.out.println(url); }catch (Exception e){ e.printStackTrace(); } } //获取put权限的tempurl //second 有效时间 单位秒 public void getObjectPutTempUrl(String containerName, String objectName,int second ){ try{ Container container = account.getContainer(containerName); StoredObject object = container.getObject(objectName); String url=object.getTempPutUrl(second); url = url +"&inline"; System.out.println(url); }catch (Exception e){ e.printStackTrace(); } } }
三、调用的主方法
package mian; import java.io.File; import java.util.HashMap; import java.util.Map; import org.javaswift.joss.model.Container; import org.javaswift.joss.model.StoredObject; import base.SwiftClientWork; //官网操作swift 调用 public class SwiftClientWorkMain { /** * @param args */ public static void main(String[] args) { SwiftClientWork swiftClientWork = new SwiftClientWork(); //创建容器 swiftClientWork.createContainer("Container1"); //删除容器 swiftClientWork.deleteContainer("Container1"); //上传对象(本地文件路径上传) swiftClientWork.createObject("Container1", "1.zip", "E://1//1.zip"); //上传对象(本地文件路径上传) File file = new File("E://1//1.zip"); swiftClientWork.createObject("Container1", "2.zip", file); //给现有的对象增加后者更新属性 Map<String, Object> map = new HashMap<String, Object>(); map.put("id", "123123123"); swiftClientWork.addOrUpdateObjectMetadata( "Container1","2.zip", map); //上传一个带属性值的对象 Map<String, Object> map = new HashMap<String, Object>(); map.put("id", "123123123"); swiftClientWork.createObjectWithMetadata("Container1","3.zip", "E://1//1.zip" ,map ); //查看容器列表 swiftClientWork.containerList(); //查看容器中的对象列表 swiftClientWork.containerObjects("containerName1"); //对容器中的对象可以进行前缀和返回条数的限制 如下:对象名称前缀为3 且结果值返回5条记录 swiftClientWork.containerObjectsPage("containerName1","3",5); //查看对象的属性 swiftClientWork.getObjectMetada("Container1","3.zip"); //下载对象 swiftClientWork.getObject( "Container1","3.zip","E://8.zip"); //删除对象 swiftClientWork.deleteObject( "Container1","3.zip"); //获取tempurl测试 swiftClientWork.getObjectTempUrl("bigContainer", "1.jpg",600 ); } }
资源丰富的的网盘资源:网盘资源大全! 推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!