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