提交 | 用户 | age
|
8d87cf
|
1 |
package com.hx.util; |
C |
2 |
|
|
3 |
import com.obs.services.ObsClient; |
|
4 |
import com.obs.services.exception.ObsException; |
|
5 |
import com.obs.services.model.PutObjectResult; |
|
6 |
import org.slf4j.Logger; |
|
7 |
import org.slf4j.LoggerFactory; |
|
8 |
import org.springframework.web.multipart.MultipartFile; |
|
9 |
|
|
10 |
import java.io.File; |
|
11 |
import java.io.IOException; |
|
12 |
import java.util.UUID; |
|
13 |
|
|
14 |
/**华为云 OBS(OSS) |
|
15 |
* @author ChenJiaHe |
|
16 |
* @date 2021-05-06 |
|
17 |
*/ |
|
18 |
public class OBSUtil { |
|
19 |
|
|
20 |
//log4j日志 |
|
21 |
private static Logger logger = LoggerFactory.getLogger(OBSUtil.class.getName()); |
|
22 |
|
|
23 |
/** 后端调用上传图片 |
|
24 |
* @param localFile 图片文件 |
|
25 |
* @param ak 访问秘钥 |
|
26 |
* @param sk 访问秘钥 |
|
27 |
* @param endPoint 存储桶 |
58930c
|
28 |
* @param fileName 文件名称(带后缀),为空时自动生成 |
8d87cf
|
29 |
* @return |
C |
30 |
* @throws IOException |
|
31 |
*/ |
|
32 |
public static PutObjectResult uploadImg(MultipartFile localFile,String ak,String sk,String endPoint,String fileName) throws IOException { |
|
33 |
|
|
34 |
//没有名次的时候自动生成 |
|
35 |
if(StringUtils.isNull(fileName)){ |
|
36 |
fileName = UUID.randomUUID().toString(); |
|
37 |
if (localFile.getOriginalFilename().lastIndexOf(".") >= 0){ |
|
38 |
fileName += localFile.getOriginalFilename().substring(localFile.getOriginalFilename().lastIndexOf(".")); |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
PutObjectResult putObjectResult = null; |
|
43 |
|
|
44 |
//初始化OBS客户端 |
|
45 |
ObsClient obsClient = new ObsClient(ak, sk, endPoint); |
|
46 |
try{ |
|
47 |
putObjectResult = obsClient.putObject(endPoint, fileName,localFile.getInputStream()); |
|
48 |
}catch (ObsException e){ |
|
49 |
logger.error("华为云 OBS 上传文件报错 "); |
|
50 |
logger.error("HTTP Code: " + e.getResponseCode()); |
|
51 |
logger.error("Error Code:" + e.getErrorCode()); |
|
52 |
logger.error("Error Message: " + e.getErrorMessage()); |
|
53 |
|
|
54 |
logger.error("Request ID:" + e.getErrorRequestId()); |
|
55 |
logger.error("Host ID:" + e.getErrorHostId()); |
|
56 |
}finally { |
|
57 |
if(obsClient != null){ |
|
58 |
obsClient.close(); |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
return putObjectResult; |
|
63 |
} |
|
64 |
|
|
65 |
|
|
66 |
/** 后端调用上传图片 |
|
67 |
* @param localFile 存储文件 |
|
68 |
* @param ak 访问秘钥 |
|
69 |
* @param sk 访问秘钥 |
|
70 |
* @param endPoint 存储桶 |
58930c
|
71 |
* @param fileName 文件名称(带后缀),为空时自动生成 |
8d87cf
|
72 |
* @return |
C |
73 |
* @throws IOException |
|
74 |
*/ |
|
75 |
public static PutObjectResult uploadImg(File localFile,String ak,String sk,String endPoint,String fileName) throws IOException { |
|
76 |
|
|
77 |
//没有名次的时候自动生成 |
|
78 |
if(StringUtils.isNull(fileName)){ |
|
79 |
fileName = UUID.randomUUID().toString(); |
|
80 |
if (localFile.getName().lastIndexOf(".") >= 0){ |
|
81 |
fileName += localFile.getName().substring(localFile.getName().lastIndexOf(".")); |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
PutObjectResult putObjectResult = null; |
|
86 |
|
|
87 |
//初始化OBS客户端 |
|
88 |
ObsClient obsClient = new ObsClient(ak, sk, endPoint); |
|
89 |
try{ |
|
90 |
putObjectResult = obsClient.putObject(endPoint, fileName,localFile); |
|
91 |
}catch (ObsException e){ |
|
92 |
logger.error("华为云 OBS 上传文件报错 "); |
|
93 |
logger.error("HTTP Code: " + e.getResponseCode()); |
|
94 |
logger.error("Error Code:" + e.getErrorCode()); |
|
95 |
logger.error("Error Message: " + e.getErrorMessage()); |
|
96 |
|
|
97 |
logger.error("Request ID:" + e.getErrorRequestId()); |
|
98 |
logger.error("Host ID:" + e.getErrorHostId()); |
|
99 |
}finally { |
|
100 |
if(obsClient != null){ |
|
101 |
obsClient.close(); |
|
102 |
} |
|
103 |
} |
|
104 |
return putObjectResult; |
|
105 |
|
|
106 |
} |
|
107 |
|
|
108 |
} |