From 8d87cf8d33815bdd6e7d0c968d105a09c5f5eac5 Mon Sep 17 00:00:00 2001
From: ChenJiaHe <763432473@qq.com>
Date: 星期四, 06 五月 2021 12:03:25 +0800
Subject: [PATCH] 新增华为云OBS存储

---
 src/main/java/com/hx/util/OBSUtil.java |  108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 pom.xml                                |    5 ++
 2 files changed, 113 insertions(+), 0 deletions(-)

diff --git a/pom.xml b/pom.xml
index ba62efd..852c3fe 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,6 +56,11 @@
             <artifactId>aliyun-sdk-oss</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.huaweicloud</groupId>
+            <artifactId>esdk-obs-java</artifactId>
+        </dependency>
+
         <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
         <dependency>
             <groupId>org.bouncycastle</groupId>
diff --git a/src/main/java/com/hx/util/OBSUtil.java b/src/main/java/com/hx/util/OBSUtil.java
new file mode 100644
index 0000000..9a433ff
--- /dev/null
+++ b/src/main/java/com/hx/util/OBSUtil.java
@@ -0,0 +1,108 @@
+package com.hx.util;
+
+import com.obs.services.ObsClient;
+import com.obs.services.exception.ObsException;
+import com.obs.services.model.PutObjectResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.UUID;
+
+/**鍗庝负浜� OBS(OSS)
+ * @author ChenJiaHe
+ * @date 2021-05-06
+ */
+public class OBSUtil {
+
+	//log4j鏃ュ織
+	private static Logger logger = LoggerFactory.getLogger(OBSUtil.class.getName());
+
+	/** 鍚庣璋冪敤涓婁紶鍥剧墖
+	 * @param localFile 鍥剧墖鏂囦欢
+	 * @param ak 璁块棶绉橀挜
+	 * @param sk 璁块棶绉橀挜
+	 * @param endPoint 瀛樺偍妗�
+	 * @param fileName 鏂囦欢鍚嶇О锛堝甫鍚庣紑锛�
+	 * @return
+	 * @throws IOException
+	 */
+	public static PutObjectResult uploadImg(MultipartFile localFile,String ak,String sk,String endPoint,String fileName) throws IOException {
+
+		//娌℃湁鍚嶆鐨勬椂鍊欒嚜鍔ㄧ敓鎴�
+		if(StringUtils.isNull(fileName)){
+			fileName = UUID.randomUUID().toString();
+			if (localFile.getOriginalFilename().lastIndexOf(".") >= 0){
+				fileName += localFile.getOriginalFilename().substring(localFile.getOriginalFilename().lastIndexOf("."));
+			}
+		}
+
+		PutObjectResult putObjectResult = null;
+
+		//鍒濆鍖朞BS瀹㈡埛绔�
+		ObsClient obsClient = new ObsClient(ak, sk, endPoint);
+		try{
+			putObjectResult = obsClient.putObject(endPoint, fileName,localFile.getInputStream());
+		}catch (ObsException e){
+			logger.error("鍗庝负浜� OBS 涓婁紶鏂囦欢鎶ラ敊 ");
+			logger.error("HTTP Code: " + e.getResponseCode());
+			logger.error("Error Code:" + e.getErrorCode());
+			logger.error("Error Message: " + e.getErrorMessage());
+
+			logger.error("Request ID:" + e.getErrorRequestId());
+			logger.error("Host ID:" + e.getErrorHostId());
+		}finally {
+			if(obsClient != null){
+				obsClient.close();
+			}
+		}
+
+		return putObjectResult;
+	}
+
+
+	/** 鍚庣璋冪敤涓婁紶鍥剧墖
+	 * @param localFile 瀛樺偍鏂囦欢
+	 * @param ak 璁块棶绉橀挜
+	 * @param sk 璁块棶绉橀挜
+	 * @param endPoint 瀛樺偍妗�
+	 * @param fileName 鏂囦欢鍚嶇О锛堝甫鍚庣紑锛�
+	 * @return
+	 * @throws IOException
+	 */
+	public static PutObjectResult uploadImg(File localFile,String ak,String sk,String endPoint,String fileName) throws IOException {
+
+		//娌℃湁鍚嶆鐨勬椂鍊欒嚜鍔ㄧ敓鎴�
+		if(StringUtils.isNull(fileName)){
+			fileName = UUID.randomUUID().toString();
+			if (localFile.getName().lastIndexOf(".") >= 0){
+				fileName += localFile.getName().substring(localFile.getName().lastIndexOf("."));
+			}
+		}
+
+		PutObjectResult putObjectResult = null;
+
+		//鍒濆鍖朞BS瀹㈡埛绔�
+		ObsClient obsClient = new ObsClient(ak, sk, endPoint);
+		try{
+			putObjectResult = obsClient.putObject(endPoint, fileName,localFile);
+		}catch (ObsException e){
+			logger.error("鍗庝负浜� OBS 涓婁紶鏂囦欢鎶ラ敊 ");
+			logger.error("HTTP Code: " + e.getResponseCode());
+			logger.error("Error Code:" + e.getErrorCode());
+			logger.error("Error Message: " + e.getErrorMessage());
+
+			logger.error("Request ID:" + e.getErrorRequestId());
+			logger.error("Host ID:" + e.getErrorHostId());
+		}finally {
+			if(obsClient != null){
+				obsClient.close();
+			}
+		}
+		return putObjectResult;
+
+	}
+
+}

--
Gitblit v1.8.0