提交 | 用户 | age
|
826b66
|
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.HeaderResponse; |
|
6 |
import com.obs.services.model.ObsBucket; |
|
7 |
import com.obs.services.model.PutObjectResult; |
|
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 访问秘钥 |
|
29 |
* @param endPoint 区域 |
|
30 |
* @param bucketName 存储桶 |
|
31 |
* @param fileName 文件名称(带后缀),为空时自动生成 |
|
32 |
* @return |
|
33 |
* @throws IOException |
|
34 |
*/ |
|
35 |
public static PutObjectResult uploadImg(MultipartFile localFile,String ak,String sk,String endPoint,String bucketName,String folderName,String fileName) throws IOException { |
|
36 |
|
|
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 |
if(StringUtils.noNull(folderName)){ |
|
45 |
fileName = folderName+"/"+fileName; |
|
46 |
} |
|
47 |
|
|
48 |
PutObjectResult putObjectResult = null; |
|
49 |
|
|
50 |
//初始化OBS客户端 |
|
51 |
ObsClient obsClient = new ObsClient(ak, sk, endPoint); |
|
52 |
try{ |
|
53 |
putObjectResult = obsClient.putObject(bucketName,fileName , localFile.getInputStream()); |
|
54 |
}catch (ObsException e){ |
|
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()); |
|
62 |
e.printStackTrace(); |
|
63 |
}finally { |
|
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 访问秘钥 |
|
77 |
* @param endPoint 区域 |
|
78 |
* @param bucketName 存储桶 |
|
79 |
* @param folderName 文件夹名称 |
|
80 |
* @param fileName 文件名称(带后缀),为空时自动生成 |
|
81 |
* @return |
|
82 |
* @throws IOException |
|
83 |
*/ |
|
84 |
public static PutObjectResult uploadImg(File localFile,String ak,String sk,String endPoint,String bucketName,String folderName,String fileName) throws IOException { |
|
85 |
|
|
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 |
} |
|
93 |
if(StringUtils.noNull(folderName)){ |
|
94 |
fileName = folderName+"/"+fileName; |
|
95 |
} |
|
96 |
|
|
97 |
PutObjectResult putObjectResult = null; |
|
98 |
|
|
99 |
//初始化OBS客户端 |
|
100 |
ObsClient obsClient = new ObsClient(ak, sk, endPoint); |
|
101 |
try{ |
|
102 |
putObjectResult = obsClient.putObject(bucketName, fileName, localFile); |
|
103 |
}catch (ObsException e){ |
|
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()); |
|
111 |
e.printStackTrace(); |
|
112 |
}finally { |
|
113 |
if(obsClient != null){ |
|
114 |
obsClient.close(); |
|
115 |
} |
|
116 |
} |
|
117 |
return putObjectResult; |
|
118 |
|
|
119 |
} |
|
120 |
|
|
121 |
/** 创建桶 |
|
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 |
|
|
156 |
} |