提交 | 用户 | 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 |
*/ |
dada11
|
35 |
public static PutObjectResult uploadImg(MultipartFile localFile,String ak,String sk,String endPoint,String bucketName,String folderName,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 |
} |
dada11
|
44 |
if(StringUtils.noNull(folderName)){ |
C |
45 |
fileName = folderName+"/"+fileName; |
|
46 |
} |
8d87cf
|
47 |
|
C |
48 |
PutObjectResult putObjectResult = null; |
|
49 |
|
|
50 |
//初始化OBS客户端 |
|
51 |
ObsClient obsClient = new ObsClient(ak, sk, endPoint); |
|
52 |
try{ |
dada11
|
53 |
putObjectResult = obsClient.putObject(bucketName,fileName , localFile.getInputStream()); |
8d87cf
|
54 |
}catch (ObsException e){ |
C |
55 |
logger.error("华为云 OBS 上传文件报错 "); |
|
56 |
logger.error("HTTP Code: " + e.getResponseCode()); |
|
57 |
logger.error("Error Code:" + e.getErrorCode()); |
|
58 |
logger.error("Error Message: " + e.getErrorMessage()); |
|
59 |
|
|
60 |
logger.error("Request ID:" + e.getErrorRequestId()); |
|
61 |
logger.error("Host ID:" + e.getErrorHostId()); |
c7276f
|
62 |
e.printStackTrace(); |
8d87cf
|
63 |
}finally { |
C |
64 |
if(obsClient != null){ |
|
65 |
obsClient.close(); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
return putObjectResult; |
|
70 |
} |
|
71 |
|
|
72 |
|
|
73 |
/** 后端调用上传图片 |
|
74 |
* @param localFile 存储文件 |
|
75 |
* @param ak 访问秘钥 |
|
76 |
* @param sk 访问秘钥 |
c7276f
|
77 |
* @param endPoint 区域 |
C |
78 |
* @param bucketName 存储桶 |
dada11
|
79 |
* @param folderName 文件夹名称 |
58930c
|
80 |
* @param fileName 文件名称(带后缀),为空时自动生成 |
8d87cf
|
81 |
* @return |
C |
82 |
* @throws IOException |
|
83 |
*/ |
dada11
|
84 |
public static PutObjectResult uploadImg(File localFile,String ak,String sk,String endPoint,String bucketName,String folderName,String fileName) throws IOException { |
8d87cf
|
85 |
|
C |
86 |
//没有名次的时候自动生成 |
|
87 |
if(StringUtils.isNull(fileName)){ |
|
88 |
fileName = UUID.randomUUID().toString(); |
|
89 |
if (localFile.getName().lastIndexOf(".") >= 0){ |
|
90 |
fileName += localFile.getName().substring(localFile.getName().lastIndexOf(".")); |
|
91 |
} |
|
92 |
} |
dada11
|
93 |
if(StringUtils.noNull(folderName)){ |
C |
94 |
fileName = folderName+"/"+fileName; |
|
95 |
} |
8d87cf
|
96 |
|
C |
97 |
PutObjectResult putObjectResult = null; |
|
98 |
|
|
99 |
//初始化OBS客户端 |
|
100 |
ObsClient obsClient = new ObsClient(ak, sk, endPoint); |
|
101 |
try{ |
6e509d
|
102 |
putObjectResult = obsClient.putObject(bucketName, fileName, localFile); |
8d87cf
|
103 |
}catch (ObsException e){ |
C |
104 |
logger.error("华为云 OBS 上传文件报错 "); |
|
105 |
logger.error("HTTP Code: " + e.getResponseCode()); |
|
106 |
logger.error("Error Code:" + e.getErrorCode()); |
|
107 |
logger.error("Error Message: " + e.getErrorMessage()); |
|
108 |
|
|
109 |
logger.error("Request ID:" + e.getErrorRequestId()); |
|
110 |
logger.error("Host ID:" + e.getErrorHostId()); |
c7276f
|
111 |
e.printStackTrace(); |
8d87cf
|
112 |
}finally { |
C |
113 |
if(obsClient != null){ |
|
114 |
obsClient.close(); |
|
115 |
} |
|
116 |
} |
|
117 |
return putObjectResult; |
|
118 |
|
|
119 |
} |
|
120 |
|
c7276f
|
121 |
/** 创建桶 |
C |
122 |
* @param ak 访问秘钥 |
|
123 |
* @param sk 访问秘钥 |
|
124 |
* @param endPoint 区域 |
|
125 |
* @param bucketName 存储桶 |
|
126 |
* @return |
|
127 |
* @throws IOException |
|
128 |
*/ |
|
129 |
public static HeaderResponse createBucket(String ak,String sk,String endPoint,String bucketName) throws IOException { |
|
130 |
|
|
131 |
HeaderResponse response = null; |
|
132 |
|
|
133 |
//初始化OBS客户端 |
|
134 |
ObsClient obsClient = new ObsClient(ak, sk, endPoint); |
|
135 |
try{ |
|
136 |
response = obsClient.createBucket(bucketName); |
|
137 |
}catch (ObsException e){ |
|
138 |
|
|
139 |
logger.error("华为云 OBS 创建桶报错 "); |
|
140 |
logger.error("HTTP Code: " + e.getResponseCode()); |
|
141 |
logger.error("Error Code:" + e.getErrorCode()); |
|
142 |
logger.error("Error Message: " + e.getErrorMessage()); |
|
143 |
|
|
144 |
logger.error("Request ID:" + e.getErrorRequestId()); |
|
145 |
logger.error("Host ID:" + e.getErrorHostId()); |
|
146 |
e.printStackTrace(); |
|
147 |
}finally { |
|
148 |
if(obsClient != null){ |
|
149 |
obsClient.close(); |
|
150 |
} |
|
151 |
} |
|
152 |
return response; |
|
153 |
} |
|
154 |
|
|
155 |
|
8d87cf
|
156 |
} |