HuaweiObsUtil.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package com.om.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.obs.services.ObsClient;
  4. import com.obs.services.model.HeaderResponse;
  5. import com.obs.services.model.HttpMethodEnum;
  6. import com.obs.services.model.TemporarySignatureRequest;
  7. import com.obs.services.model.TemporarySignatureResponse;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. @Slf4j
  19. @Component
  20. public class HuaweiObsUtil {
  21. //Access Key Id
  22. @Value("${obs.ak}")
  23. private String ak;
  24. //Secret Access Key
  25. @Value("${obs.sk}")
  26. private String sk;
  27. //桶名称
  28. @Value("${obs.bucketName}")
  29. private String bucketName;
  30. // 终端节点访问Endpoint
  31. @Value("${obs.endpoint}")
  32. private String endpoint;
  33. // 访问域名 在域名后面或文件目录前加“/”
  34. @Value("${obs.path}")
  35. private String path;
  36. /**
  37. * 文件上传
  38. *
  39. * @param file 要上传的文件
  40. * @return 文件在OBS中的URL
  41. * @throws IOException IO异常
  42. */
  43. public String upload(MultipartFile file,String prefix) throws IOException {
  44. ObsClient obsClient = null;
  45. try {
  46. String originalFilename = file.getOriginalFilename();
  47. // 拼接文件在OBS中的路径
  48. String objectName = prefix + "/"+ originalFilename;
  49. // 初始化OBS客户端
  50. obsClient = new ObsClient(ak, sk, endpoint);
  51. // 上传文件到OBS
  52. HeaderResponse response = obsClient.putObject(bucketName, objectName, file.getInputStream());
  53. log.info(JSONObject.toJSONString(response));
  54. // 判断上传是否成功
  55. int statusCode = response.getStatusCode();
  56. if (200 == statusCode) {
  57. // 拼接文件在OBS中的URL并返回
  58. String uploadUrl = getUploadUrl(originalFilename, prefix);
  59. //String objectUrl = "https://"+path + "/" + objectName;
  60. return uploadUrl;
  61. }
  62. // 关闭OBS客户端
  63. obsClient.close();
  64. } finally {
  65. }
  66. return null;
  67. }
  68. /**
  69. * 文件上传
  70. *
  71. * @param file 要上传的文件
  72. * @return 文件在OBS中的URL
  73. * @throws IOException IO异常
  74. */
  75. public String upload(File file, String prefix) throws IOException {
  76. ObsClient obsClient = null;
  77. try {
  78. String originalFilename = file.getName();
  79. // 拼接文件在OBS中的路径
  80. String objectName = prefix + "/"+ originalFilename;
  81. // 初始化OBS客户端
  82. obsClient = new ObsClient(ak, sk, endpoint);
  83. // 上传文件到OBS
  84. FileInputStream fileInputStream = new FileInputStream(file);
  85. HeaderResponse response = obsClient.putObject(bucketName, objectName, fileInputStream);
  86. log.info(JSONObject.toJSONString(response));
  87. // 判断上传是否成功
  88. int statusCode = response.getStatusCode();
  89. if (200 == statusCode) {
  90. // 拼接文件在OBS中的URL并返回
  91. String uploadUrl = getUploadUrl(originalFilename, prefix);
  92. //String objectUrl = "https://"+path + "/" + objectName;
  93. return uploadUrl;
  94. }
  95. // 关闭OBS客户端
  96. obsClient.close();
  97. } finally {
  98. }
  99. return null;
  100. }
  101. /**
  102. * 下载文件
  103. *
  104. * @param fileName 文件名称
  105. * @param fileType 文件路径
  106. * @return
  107. */
  108. public String getDownloadUrl(String fileName, FileType fileType) {
  109. ObsClient obsClient = null;
  110. obsClient = new ObsClient(ak, sk, endpoint);
  111. // URL有效期,3600秒.5分钟
  112. long expireSeconds = 3600L;
  113. String objectName = fileType.getType().concat("/").concat(fileName);
  114. TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
  115. request.setBucketName(bucketName);
  116. request.setObjectKey(objectName);
  117. TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
  118. return response.getSignedUrl();
  119. }
  120. /**
  121. * 下载文件
  122. *
  123. * @param fileName 文件名称
  124. * @param prefix 文件路径
  125. * @return
  126. */
  127. public String getDownloadUrl(String fileName, String prefix) {
  128. ObsClient obsClient = null;
  129. obsClient = new ObsClient(ak, sk, endpoint);
  130. // URL有效期,3600秒.5分钟
  131. long expireSeconds = 3600L;
  132. String objectName = prefix.concat("/").concat(fileName);
  133. TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
  134. request.setBucketName(bucketName);
  135. request.setObjectKey(objectName);
  136. TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
  137. return response.getSignedUrl();
  138. }
  139. /**
  140. * 获取上传地址
  141. *
  142. * @param fileName 文件名称
  143. * @param fileType 文件路径
  144. * @return
  145. */
  146. public String getUploadUrl(String fileName, FileType fileType) {
  147. try {
  148. // 创建ObsClient实例
  149. ObsClient obsClient = new ObsClient(ak, sk, endpoint);
  150. // URL有效期,3600秒
  151. long expireSeconds = 3600L;
  152. String objectName = fileType.getType().concat("/").concat(fileName);
  153. TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
  154. request.setBucketName(bucketName);
  155. request.setObjectKey(objectName);
  156. TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
  157. return response.getSignedUrl();
  158. } catch (Exception e) {
  159. log.error("获取上传地址异常:{}", e.getMessage(), e);
  160. }
  161. return null;
  162. }
  163. /**
  164. * 获取上传地址
  165. *
  166. * @param fileName 文件名称
  167. * @param prefix 文件路径
  168. * @return
  169. */
  170. public String getUploadUrl(String fileName, String prefix) {
  171. try {
  172. // 创建ObsClient实例
  173. ObsClient obsClient = new ObsClient(ak, sk, endpoint);
  174. // URL有效期,3600秒
  175. long expireSeconds = 3600L;
  176. String objectName = prefix.concat("/").concat(fileName);
  177. TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
  178. request.setBucketName(bucketName);
  179. request.setObjectKey(objectName);
  180. TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
  181. return response.getSignedUrl();
  182. } catch (Exception e) {
  183. log.error("获取上传地址异常:{}", e.getMessage(), e);
  184. }
  185. return null;
  186. }
  187. /**
  188. * 上传视频
  189. *
  190. * @param fileName 文件名称
  191. * @param fileType 文件路径
  192. * @return
  193. */
  194. public boolean uploadFileVideo(String fileName, FileType fileType, InputStream is) {
  195. try {
  196. String objectName = fileType.getType().concat("/").concat(fileName);
  197. ObsClient obsClient = new ObsClient(ak, sk, endpoint);
  198. // 添加 ContentType (添加后可在浏览器中直接浏览,而非下载链接)
  199. obsClient.putObject(bucketName, objectName, is);
  200. obsClient.close();
  201. return true;
  202. } catch (Exception e) {
  203. log.error("上传视频文件异常:{}", e.getMessage(), e);
  204. }
  205. return false;
  206. }
  207. public enum FileType {
  208. TEST("test", "测试");
  209. private String type;
  210. private String desc;
  211. FileType(String type, String desc) {
  212. this.type = type;
  213. this.desc = desc;
  214. }
  215. public String getType() {
  216. return type;
  217. }
  218. public String getDesc() {
  219. return desc;
  220. }
  221. }
  222. }