|  | @@ -0,0 +1,121 @@
 | 
	
		
			
				|  |  | +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.PutObjectResponse;
 | 
	
		
			
				|  |  | +import com.jeeplus.common.config.Global;
 | 
	
		
			
				|  |  | +import org.junit.Test;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.io.File;
 | 
	
		
			
				|  |  | +import java.io.InputStream;
 | 
	
		
			
				|  |  | +import java.net.URL;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +public class BOSClientUtil {
 | 
	
		
			
				|  |  | +    private final static String accessKey= Global.getConfig("bos_access_key");
 | 
	
		
			
				|  |  | +    private final static String secretKey= Global.getConfig("bos_secret_key");
 | 
	
		
			
				|  |  | +    private final static String endpoint= Global.getConfig("bos_endpoint");
 | 
	
		
			
				|  |  | +    private final static String bucketName= Global.getConfig("bos_buck_name");
 | 
	
		
			
				|  |  | +    private static 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 static 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 static PutObjectResponse uploadFileToBos(File file,
 | 
	
		
			
				|  |  | +                                                    String objectKey) {
 | 
	
		
			
				|  |  | +        return bosClient.putObject(bucketName, objectKey, file);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     *以数据流形式上传Object(不超过5GB)
 | 
	
		
			
				|  |  | +     * @param inputStream
 | 
	
		
			
				|  |  | +     * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static PutObjectResponse uploadInputStreamToBos(InputStream inputStream,
 | 
	
		
			
				|  |  | +                                                           String objectKey) {
 | 
	
		
			
				|  |  | +        return bosClient.putObject(bucketName, objectKey, inputStream);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 删除已经上传的Object
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param objectKey  文件路径/文件名(可以用“/”来创建多层文件夹)
 | 
	
		
			
				|  |  | +     * @return 上传成功后的tag
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static void deleteObject(String objectKey) {
 | 
	
		
			
				|  |  | +        bosClient.deleteObject(bucketName, objectKey);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 获取文件下载URL
 | 
	
		
			
				|  |  | +     * @param objectKey           文件夹和文件名
 | 
	
		
			
				|  |  | +     * @return 目标文件的下载url
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String generatePresignedUrl(String objectKey) {
 | 
	
		
			
				|  |  | +        URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
 | 
	
		
			
				|  |  | +        return url.toString();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static String upload(String path,File file){
 | 
	
		
			
				|  |  | +        String objectKey = path+file.getName();
 | 
	
		
			
				|  |  | +        bosClient.putObject(bucketName, objectKey, file);
 | 
	
		
			
				|  |  | +        URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
 | 
	
		
			
				|  |  | +        return url.toString();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static String upload(String path,InputStream inputStream){
 | 
	
		
			
				|  |  | +        String objectKey = path;
 | 
	
		
			
				|  |  | +        bosClient.putObject(bucketName, objectKey, inputStream);
 | 
	
		
			
				|  |  | +        URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
 | 
	
		
			
				|  |  | +        return url.toString();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @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);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 |