package com.jeeplus.common.bos; import com.baidubce.auth.DefaultBceCredentials; import com.baidubce.services.bos.BosClient; import com.baidubce.services.bos.BosClientConfiguration; import com.baidubce.services.bos.model.BosObject; import com.baidubce.services.bos.model.PutObjectResponse; import com.jeeplus.common.config.Global; import org.junit.Test; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; public class BOSClientUtil { private final String accessKey= Global.getConfig("bos_access_key"); private final String secretKey= Global.getConfig("bos_secret_key"); private final String endpoint= Global.getConfig("bos_endpoint"); private final String bucketName= Global.getConfig("bos_buck_name"); private final String urlTem = "https://BUCKNAME.su.bcebos.comKEY"; private BosClient bosClient; public BOSClientUtil() { BosClientConfiguration config = new BosClientConfiguration(); config.setMaxConnections(10); config.setCredentials(new DefaultBceCredentials(accessKey, secretKey)); config.setEndpoint(endpoint); bosClient = new BosClient(config); } /** * 获取BosClient对象 * @return */ public BosClient getBosClient() { BosClientConfiguration config = new BosClientConfiguration(); config.setMaxConnections(10); config.setCredentials(new DefaultBceCredentials(accessKey, secretKey)); config.setEndpoint(endpoint); return new BosClient(config); } /** *百度bos以file形式上传文件(不超过5GB) * @param file * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹) * @return */ public PutObjectResponse uploadFileToBos(File file, String objectKey) { return bosClient.putObject(bucketName, objectKey, file); } /** *以数据流形式上传Object(不超过5GB) * @param inputStream * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹) * @return */ public PutObjectResponse uploadInputStreamToBos(InputStream inputStream, String objectKey) { return bosClient.putObject(bucketName, objectKey, inputStream); } /** * 删除已经上传的Object * * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹) * @return 上传成功后的tag */ public void deleteObject(String objectKey) { bosClient.deleteObject(bucketName, objectKey); } /** * 获取文件下载URL * @param objectKey 文件夹和文件名 * @return 目标文件的下载url */ public String generatePresignedUrl(String objectKey) { URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1); return url.toString(); } public InputStream getObject(String objectKey) throws IOException { // 获取Object,返回结果为BosObject对象 BosObject object = bosClient.getObject(bucketName, objectKey); // 获取Object的输入流 InputStream inputStream = object.getObjectContent(); return inputStream; } public String upload(String path,File file){ String objectKey = path+file.getName(); bosClient.putObject(bucketName, objectKey, file); String url = urlTem.replace("BUCKNAME",bucketName).replace("KEY",objectKey); // URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1); return url; } public String upload(String path,InputStream inputStream){ bosClient.putObject(bucketName, path, inputStream); String url = urlTem.replace("BUCKNAME",bucketName).replace("KEY",path); // URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1); return url; } // @Test // public void testPut(){ // File file = new File("E:/bk1.png"); // String name = file.getName(); // BOSClientUtil.uploadFileToBos(file,"/test/"+name); // } // // // @Test // public void testDel(){ // BOSClientUtil.deleteObject("/test/1.txt"); // } // // @Test // public void testUrl(){ // File file = new File("E:/bk1.png"); // String upload = BOSClientUtil.upload("/text1/", file); // System.out.println(upload); // } @Test public void uploadAll(){ ArrayList listFileName = new ArrayList(); String filePath = "D:/IDEA workspace/total_process/src/main/webapp/static/"; getAllFileName(filePath,listFileName); int count = 1; int total = listFileName.size(); System.out.println(total); for(String name : listFileName){ if(name != null) { int statice = name.indexOf("static"); String name1 = name.substring(statice + 6, name.length()); File file = new File(name); // System.out.println("/static"+name1); bosClient.putObject(bucketName, "/static"+name1, file); // BOSClientUtil.upload("/static"+name1,file); System.out.println("---------正在上传 "+file.getName()+"-------"); System.out.println("上传第"+count+"个资源,共"+total+"个资源"); count++; // try { // bosClient.deleteObject(bucketName, " /static" + name1); // }catch (Exception e){ // e.printStackTrace(); // } } } } public void getAllFileName(String path, ArrayList listFileName){ File file = new File(path); File [] files = file.listFiles(); String [] names = file.list(); if(names != null){ String [] completNames = new String[names.length]; for(int i=0;i