From 6b479c5774ed3d6e2dc92693ddeda3e7ad1494db Mon Sep 17 00:00:00 2001 From: Andru <769883346@qq.com> Date: 星期一, 23 十月 2023 17:34:08 +0800 Subject: [PATCH] 添加获客方法 --- src/main/java/com/hx/util/COSUtil.java | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 172 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/hx/util/COSUtil.java b/src/main/java/com/hx/util/COSUtil.java index 0137cac..92efaba 100644 --- a/src/main/java/com/hx/util/COSUtil.java +++ b/src/main/java/com/hx/util/COSUtil.java @@ -1,22 +1,26 @@ package com.hx.util; -import com.aliyun.oss.OSSClient; -import com.aliyun.oss.model.OSSObject; +import com.hx.exception.TipsException; import com.qcloud.cos.COSClient; import com.qcloud.cos.ClientConfig; import com.qcloud.cos.auth.BasicCOSCredentials; import com.qcloud.cos.auth.COSCredentials; +import com.qcloud.cos.exception.CosClientException; +import com.qcloud.cos.exception.CosServiceException; +import com.qcloud.cos.http.HttpProtocol; +import com.qcloud.cos.model.GetObjectRequest; import com.qcloud.cos.model.ObjectMetadata; import com.qcloud.cos.model.PutObjectRequest; import com.qcloud.cos.model.PutObjectResult; import com.qcloud.cos.region.Region; +import com.qcloud.cos.transfer.Download; +import com.qcloud.cos.transfer.TransferManager; import org.springframework.web.multipart.MultipartFile; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.util.Date; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /**鑵捐浜� COS * @author ChenJiaHe @@ -63,8 +67,171 @@ }else{ imgUrl.append("/"+key); } + cosClient.shutdown(); return imgUrl.toString(); } + + /** 鍚庣璋冪敤涓婁紶鍥剧墖 + * @param key 涓婁紶璺緞锛堝寘鎷浘鐗囧悕绉板拰鍜屽悗缂�锛夛紝鎸囧畾瑕佷笂浼犲埌 COS 涓婂璞¢敭 + * @param localFile + * @param secretId 鐢ㄦ埛id + * @param secretKey 鐢ㄦ埛绉橀挜 + * @param regionName 瀛樺湪鍩燂紝鍙傝�冭吘璁簯 + * @param bucketName 鎸囧畾瑕佷笂浼犲埌鐨勫瓨鍌ㄦ《 + * @return + * @throws IOException + */ + public static String uploadImg(String key,File localFile,String secretId, String secretKey,String regionName,String bucketName) throws IOException { + + // 1 鍒濆鍖栫敤鎴疯韩浠戒俊鎭紙secretId, secretKey锛夈�� + COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); + // 2 璁剧疆 bucket 鐨勫尯鍩�, COS 鍦板煙鐨勭畝绉拌鍙傜収 https://cloud.tencent.com/document/product/436/6224 + // clientConfig 涓寘鍚簡璁剧疆 region, https(榛樿 http), 瓒呮椂, 浠g悊绛� set 鏂规硶, 浣跨敤鍙弬瑙佹簮鐮佹垨鑰呭父瑙侀棶棰� Java SDK 閮ㄥ垎銆� + Region region = new Region(regionName); + ClientConfig clientConfig = new ClientConfig(region); + // 3 鐢熸垚 cos 瀹㈡埛绔�� + COSClient cosClient = new COSClient(cred, clientConfig); + + // 璁剧疆 Content type, 榛樿鏄� application/octet-stream,瀵逛簬鏈湴鏂囦欢涓婁紶锛岄粯璁ゆ牴鎹湰鍦版枃浠剁殑鍚庣紑杩涜鏄犲皠 + // 锛屼緥濡� jpg 鏂囦欢鏄犲皠 涓篿mage/jpeg瀵逛簬娴佸紡涓婁紶 榛樿鏄� application/octet-stream + //objectMetadata.setContentType("application/pdf"); + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile); + PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); + + //鎷兼帴璺緞 + StringBuilder imgUrl = new StringBuilder(); + imgUrl.append("https://"+bucketName+".cos."+regionName+".myqcloud.com"); + if(key.startsWith("/")){ + imgUrl.append(key); + }else{ + imgUrl.append("/"+key); + } + cosClient.shutdown(); + return imgUrl.toString(); + + } + + /**涓嬭浇鏂囦欢 + * @param key 涓婁紶璺緞锛堝寘鎷浘鐗囧悕绉板拰鍜屽悗缂�锛夛紝鎸囧畾瑕佷笂浼犲埌 COS 涓婂璞¢敭 + * @param secretId 鐢ㄦ埛id + * @param secretKey 鐢ㄦ埛绉橀挜 + * @param regionName 瀛樺湪鍩燂紝鍙傝�冭吘璁簯 + * @param bucketName 鎸囧畾瑕佷笂浼犲埌鐨勫瓨鍌ㄦ《 + */ + public static File download(String key,String secretId, String secretKey,String regionName,String bucketName){ + + // 浣跨敤楂樼骇鎺ュ彛蹇呴』鍏堜繚璇佹湰杩涚▼瀛樺湪涓�涓� TransferManager 瀹炰緥锛屽鏋滄病鏈夊垯鍒涘缓 + // 璇︾粏浠g爜鍙傝鏈〉锛氶珮绾ф帴鍙� -> 鍒涘缓 TransferManager + TransferManager transferManager = createTransferManager( secretId, secretKey,regionName); + // 鏈湴鏂囦欢璺緞 + File downloadFile = null; + try { + GetObjectRequest getObjectRequest; + //鎴彇鏂囦欢鍚嶇О + String[] datas = key.split(regionName+".myqcloud.com/"); + if(datas.length ==1){ + getObjectRequest = new GetObjectRequest(bucketName, datas[0]); + }else if(datas.length ==2){ + getObjectRequest = new GetObjectRequest(bucketName, datas[1]); + }else{ + throw new TipsException("鏂囦欢璺緞閿欒銆恔ey銆�"); + } + + ////鐢熸垚涓存椂鏂囦欢 + //鑾峰彇鍚庣紑鍚嶇О + String suffix = key.substring(key.lastIndexOf(".")); + downloadFile = File.createTempFile("temp", suffix); + + // 杩斿洖涓�涓紓姝ョ粨鏋� Donload, 鍙悓姝ョ殑璋冪敤 waitForCompletion 绛夊緟涓嬭浇缁撴潫, 鎴愬姛杩斿洖 void, 澶辫触鎶涘嚭寮傚父 + Download download = transferManager.download(getObjectRequest, downloadFile); + download.waitForCompletion(); + } catch (CosServiceException e) { + e.printStackTrace(); + } catch (CosClientException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + }catch (IOException e){ + e.printStackTrace(); + }finally { + // 纭畾鏈繘绋嬩笉鍐嶄娇鐢� transferManager 瀹炰緥涔嬪悗锛屽叧闂箣 + // 璇︾粏浠g爜鍙傝鏈〉锛氶珮绾ф帴鍙� -> 鍏抽棴 TransferManager + shutdownTransferManager(transferManager); + } + + return downloadFile; + } + + /** + * 鍒涘缓 TransferManager 瀹炰緥锛岃繖涓疄渚嬬敤鏉ュ悗缁皟鐢ㄩ珮绾ф帴鍙� + * @param secretId + * @param secretKey + * @return + */ + public static TransferManager createTransferManager(String secretId, String secretKey,String regionName) { + // 鍒涘缓涓�涓� COSClient 瀹炰緥锛岃繖鏄闂� COS 鏈嶅姟鐨勫熀纭�瀹炰緥銆� + // 璇︾粏浠g爜鍙傝鏈〉: 绠�鍗曟搷浣� -> 鍒涘缓 COSClient + COSClient cosClient = createCOSClient( secretId, secretKey,regionName); + + // 鑷畾涔夌嚎绋嬫睜澶у皬锛屽缓璁湪瀹㈡埛绔笌 COS 缃戠粶鍏呰冻锛堜緥濡備娇鐢ㄨ吘璁簯鐨� CVM锛屽悓鍦板煙涓婁紶 COS锛夌殑鎯呭喌涓嬶紝璁剧疆鎴�16鎴�32鍗冲彲锛屽彲杈冨厖鍒嗙殑鍒╃敤缃戠粶璧勬簮 + // 瀵逛簬浣跨敤鍏綉浼犺緭涓旂綉缁滃甫瀹借川閲忎笉楂樼殑鎯呭喌锛屽缓璁噺灏忚鍊硷紝閬垮厤鍥犵綉閫熻繃鎱紝閫犳垚璇锋眰瓒呮椂銆� + ExecutorService threadPool = Executors.newFixedThreadPool(32); + + // 浼犲叆涓�涓� threadpool, 鑻ヤ笉浼犲叆绾跨▼姹狅紝榛樿 TransferManager 涓細鐢熸垚涓�涓崟绾跨▼鐨勭嚎绋嬫睜銆� + TransferManager transferManager = new TransferManager(cosClient, threadPool); + + return transferManager; + } + + /** + * 鍏抽棴绠$悊 + * @param transferManager + */ + public static void shutdownTransferManager(TransferManager transferManager) { + // 鎸囧畾鍙傛暟涓� true, 鍒欏悓鏃朵細鍏抽棴 transferManager 鍐呴儴鐨� COSClient 瀹炰緥銆� + // 鎸囧畾鍙傛暟涓� false, 鍒欎笉浼氬叧闂� transferManager 鍐呴儴鐨� COSClient 瀹炰緥銆� + transferManager.shutdownNow(true); + } + + /** + * 鍒涘缓 COSClient 瀹炰緥锛岃繖涓疄渚嬬敤鏉ュ悗缁皟鐢ㄨ姹� + * @param secretId + * @param secretKey + * @return + */ + public static COSClient createCOSClient(String secretId, String secretKey,String regionName) { + // 璁剧疆鐢ㄦ埛韬唤淇℃伅銆� + // SECRETID 鍜� SECRETKEY 璇风櫥褰曡闂鐞嗘帶鍒跺彴 https://console.cloud.tencent.com/cam/capi 杩涜鏌ョ湅鍜岀鐞� + COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); + + // ClientConfig 涓寘鍚簡鍚庣画璇锋眰 COS 鐨勫鎴风璁剧疆锛� + ClientConfig clientConfig = new ClientConfig(); + + // 璁剧疆 bucket 鐨勫湴鍩� + // COS_REGION 璇峰弬鐓� https://cloud.tencent.com/document/product/436/6224 + clientConfig.setRegion(new Region(regionName)); + + // 璁剧疆璇锋眰鍗忚, http 鎴栬�� https + // 5.6.53 鍙婃洿浣庣殑鐗堟湰锛屽缓璁缃娇鐢� https 鍗忚 + // 5.6.54 鍙婃洿楂樼増鏈紝榛樿浣跨敤浜� https + clientConfig.setHttpProtocol(HttpProtocol.https); + + // 浠ヤ笅鐨勮缃紝鏄彲閫夌殑锛� + + // 璁剧疆 socket 璇诲彇瓒呮椂锛岄粯璁� 30s + clientConfig.setSocketTimeout(300*1000); + // 璁剧疆寤虹珛杩炴帴瓒呮椂锛岄粯璁� 30s + clientConfig.setConnectionTimeout(30*1000); + + // 濡傛灉闇�瑕佺殑璇濓紝璁剧疆 http 浠g悊锛宨p 浠ュ強 port + //clientConfig.setHttpProxyIp("httpProxyIp"); + //clientConfig.setHttpProxyPort(80); + + // 鐢熸垚 cos 瀹㈡埛绔�� + return new COSClient(cred, clientConfig); + } + + } -- Gitblit v1.8.0