From a1bdc450eabe8d9a342dc4a578e157b73fdabf29 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期日, 08 十月 2023 11:41:27 +0800 Subject: [PATCH] 新版excel优化 --- src/main/java/com/hx/util/OBSUtil.java | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/hx/util/OBSUtil.java b/src/main/java/com/hx/util/OBSUtil.java index 26ce457..87b0ea8 100644 --- a/src/main/java/com/hx/util/OBSUtil.java +++ b/src/main/java/com/hx/util/OBSUtil.java @@ -2,6 +2,8 @@ import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ObsBucket; import com.obs.services.model.PutObjectResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,12 +26,13 @@ * @param localFile 鍥剧墖鏂囦欢 * @param ak 璁块棶绉橀挜 * @param sk 璁块棶绉橀挜 - * @param endPoint 瀛樺偍妗� + * @param endPoint 鍖哄煙 + * @param bucketName 瀛樺偍妗� * @param fileName 鏂囦欢鍚嶇О锛堝甫鍚庣紑锛夛紝涓虹┖鏃惰嚜鍔ㄧ敓鎴� * @return * @throws IOException */ - public static PutObjectResult uploadImg(MultipartFile localFile,String ak,String sk,String endPoint,String fileName) throws IOException { + public static PutObjectResult uploadImg(MultipartFile localFile,String ak,String sk,String endPoint,String bucketName,String folderName,String fileName) throws IOException { //娌℃湁鍚嶆鐨勬椂鍊欒嚜鍔ㄧ敓鎴� if(StringUtils.isNull(fileName)){ @@ -38,13 +41,16 @@ fileName += localFile.getOriginalFilename().substring(localFile.getOriginalFilename().lastIndexOf(".")); } } + if(StringUtils.noNull(folderName)){ + fileName = folderName+"/"+fileName; + } PutObjectResult putObjectResult = null; //鍒濆鍖朞BS瀹㈡埛绔� ObsClient obsClient = new ObsClient(ak, sk, endPoint); try{ - putObjectResult = obsClient.putObject(endPoint, fileName,localFile.getInputStream()); + putObjectResult = obsClient.putObject(bucketName,fileName , localFile.getInputStream()); }catch (ObsException e){ logger.error("鍗庝负浜� OBS 涓婁紶鏂囦欢鎶ラ敊 "); logger.error("HTTP Code: " + e.getResponseCode()); @@ -53,6 +59,7 @@ logger.error("Request ID:" + e.getErrorRequestId()); logger.error("Host ID:" + e.getErrorHostId()); + e.printStackTrace(); }finally { if(obsClient != null){ obsClient.close(); @@ -67,12 +74,14 @@ * @param localFile 瀛樺偍鏂囦欢 * @param ak 璁块棶绉橀挜 * @param sk 璁块棶绉橀挜 - * @param endPoint 瀛樺偍妗� + * @param endPoint 鍖哄煙 + * @param bucketName 瀛樺偍妗� + * @param folderName 鏂囦欢澶瑰悕绉� * @param fileName 鏂囦欢鍚嶇О锛堝甫鍚庣紑锛夛紝涓虹┖鏃惰嚜鍔ㄧ敓鎴� * @return * @throws IOException */ - public static PutObjectResult uploadImg(File localFile,String ak,String sk,String endPoint,String fileName) throws IOException { + public static PutObjectResult uploadImg(File localFile,String ak,String sk,String endPoint,String bucketName,String folderName,String fileName) throws IOException { //娌℃湁鍚嶆鐨勬椂鍊欒嚜鍔ㄧ敓鎴� if(StringUtils.isNull(fileName)){ @@ -81,13 +90,16 @@ fileName += localFile.getName().substring(localFile.getName().lastIndexOf(".")); } } + if(StringUtils.noNull(folderName)){ + fileName = folderName+"/"+fileName; + } PutObjectResult putObjectResult = null; //鍒濆鍖朞BS瀹㈡埛绔� ObsClient obsClient = new ObsClient(ak, sk, endPoint); try{ - putObjectResult = obsClient.putObject(endPoint, fileName,localFile); + putObjectResult = obsClient.putObject(bucketName, fileName, localFile); }catch (ObsException e){ logger.error("鍗庝负浜� OBS 涓婁紶鏂囦欢鎶ラ敊 "); logger.error("HTTP Code: " + e.getResponseCode()); @@ -96,6 +108,7 @@ logger.error("Request ID:" + e.getErrorRequestId()); logger.error("Host ID:" + e.getErrorHostId()); + e.printStackTrace(); }finally { if(obsClient != null){ obsClient.close(); @@ -105,4 +118,39 @@ } + /** 鍒涘缓妗� + * @param ak 璁块棶绉橀挜 + * @param sk 璁块棶绉橀挜 + * @param endPoint 鍖哄煙 + * @param bucketName 瀛樺偍妗� + * @return + * @throws IOException + */ + public static HeaderResponse createBucket(String ak,String sk,String endPoint,String bucketName) throws IOException { + + HeaderResponse response = null; + + //鍒濆鍖朞BS瀹㈡埛绔� + ObsClient obsClient = new ObsClient(ak, sk, endPoint); + try{ + response = obsClient.createBucket(bucketName); + }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()); + e.printStackTrace(); + }finally { + if(obsClient != null){ + obsClient.close(); + } + } + return response; + } + + } -- Gitblit v1.8.0