From 76b630351b2db4b3dbc5c427f5d8edb5d488872c Mon Sep 17 00:00:00 2001 From: ChenJiaHe <763432473@qq.com> Date: 星期五, 21 五月 2021 10:55:28 +0800 Subject: [PATCH] 月增加时间处理 --- src/main/java/com/hx/util/OBSUtil.java | 64 ++++++++++++++++++++++++++++---- 1 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/hx/util/OBSUtil.java b/src/main/java/com/hx/util/OBSUtil.java index 9a433ff..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 fileName 鏂囦欢鍚嶇О锛堝甫鍚庣紑锛� + * @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 fileName 鏂囦欢鍚嶇О锛堝甫鍚庣紑锛� + * @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