From 24bed5e6b6e31090cb61600a0bdea898eac65da1 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期六, 26 十二月 2020 17:20:23 +0800
Subject: [PATCH] cmq调整

---
 utils/src/main/java/org/yeshi/utils/tencentcloud/COSManager.java |  357 ++++++++++++++++++++++++++++++-----------------------------
 1 files changed, 182 insertions(+), 175 deletions(-)

diff --git a/utils/src/main/java/org/yeshi/utils/tencentcloud/COSManager.java b/utils/src/main/java/org/yeshi/utils/tencentcloud/COSManager.java
index e30f12c..2f2b5fa 100644
--- a/utils/src/main/java/org/yeshi/utils/tencentcloud/COSManager.java
+++ b/utils/src/main/java/org/yeshi/utils/tencentcloud/COSManager.java
@@ -24,201 +24,208 @@
 import com.qcloud.cos.region.Region;
 
 public class COSManager {
-	static long appId = 0L;
-	static String secretId = "";
-	static String secretKey = "";
-	// 璁剧疆瑕佹搷浣滅殑bucket
-	static String bucketName = "";
-	static COSClient cosClient;
-	static String totalBucketName = "";
+    static long appId = 0L;
+    static String secretId = "";
+    static String secretKey = "";
+    // 璁剧疆瑕佹搷浣滅殑bucket
+    static String bucketName = "";
+    static COSClient cosClient;
+    static String totalBucketName = "";
+    static String accessHost = "";
 
-	static COSManager instance;
+    static COSManager instance;
 
-	public static COSManager getInstance() {
-		if (instance == null)
-			instance = new COSManager();
-		return instance;
-	}
+    public static COSManager getInstance() {
+        if (instance == null)
+            instance = new COSManager();
+        return instance;
+    }
 
-	/**
-	 * 鍒濆鍖栵紝鍦ㄤ娇鐢ㄤ箣鍓嶅繀椤昏皟鐢ㄦ鏂规硶
-	 * 
-	 * @param params
-	 */
-	public void init(COSInitParams params) {
-		appId = params.getAppId();
-		secretId = params.getSecretId();
-		secretKey = params.getSecretKey();
-		bucketName = params.getBucketName();
-		COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
-		ClientConfig clientConfig = new ClientConfig(new Region(params.getRegion()));
-		cosClient = new COSClient(cred, clientConfig);
-		// bucket鐨勫懡鍚嶈鍒欎负{name}-{appid} 锛屾澶勫~鍐欑殑瀛樺偍妗跺悕绉板繀椤讳负姝ゆ牸寮�
-		totalBucketName = bucketName + "-" + appId;
-	}
+    /**
+     * 鍒濆鍖栵紝鍦ㄤ娇鐢ㄤ箣鍓嶅繀椤昏皟鐢ㄦ鏂规硶
+     *
+     * @param params
+     */
+    public void init(COSInitParams params) {
+        appId = params.getAppId();
+        secretId = params.getSecretId();
+        secretKey = params.getSecretKey();
+        bucketName = params.getBucketName();
+        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
+        ClientConfig clientConfig = new ClientConfig(new Region(params.getRegion()));
+        cosClient = new COSClient(cred, clientConfig);
+        // bucket鐨勫懡鍚嶈鍒欎负{name}-{appid} 锛屾澶勫~鍐欑殑瀛樺偍妗跺悕绉板繀椤讳负姝ゆ牸寮�
+        totalBucketName = bucketName + "-" + appId;
+        accessHost = params.getAccessHost();
+    }
 
-	public FileUploadResult uploadFile(File f, String key) {
-		if (key != null && !key.startsWith("/"))
-			key = "/" + key;
-		PutObjectRequest putObjectRequest = new PutObjectRequest(totalBucketName, key, f);
+    public FileUploadResult uploadFile(File f, String key) {
+        if (key != null && !key.startsWith("/"))
+            key = "/" + key;
+        PutObjectRequest putObjectRequest = new PutObjectRequest(totalBucketName, key, f);
 
-		try {
-			PutObjectResult result = cosClient.putObject(putObjectRequest);
+        try {
+            PutObjectResult result = cosClient.putObject(putObjectRequest);
 
-			return new FileUploadResult(String.format("http://%s.file.myqcloud.com%s", totalBucketName, key),
-					result.getETag());
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-		return null;
-	}
+            if (StringUtil.isNullOrEmpty(accessHost)) {
+                return new FileUploadResult(String.format("http://%s.file.myqcloud.com%s", totalBucketName, key),
+                        result.getETag());
+            } else {
+                return new FileUploadResult(String.format("%s%s", accessHost, key),
+                        result.getETag());
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 
-	public FileUploadResult uploadInputStream(InputStream inputStream, String key) {
-		int count = 0;
-		try {
-			count = inputStream.available();
-		} catch (IOException e1) {
-			e1.printStackTrace();
-		}
+    public FileUploadResult uploadInputStream(InputStream inputStream, String key) {
+        int count = 0;
+        try {
+            count = inputStream.available();
+        } catch (IOException e1) {
+            e1.printStackTrace();
+        }
 
-		ObjectMetadata metadata = new ObjectMetadata();
-		metadata.setContentLength(count);
+        ObjectMetadata metadata = new ObjectMetadata();
+        metadata.setContentLength(count);
 
-		if (key != null && !key.startsWith("/"))
-			key = "/" + key;
-		PutObjectRequest putObjectRequest = new PutObjectRequest(totalBucketName, key, inputStream, metadata);
-		try {
-			PutObjectResult result = cosClient.putObject(putObjectRequest);
+        if (key != null && !key.startsWith("/"))
+            key = "/" + key;
+        PutObjectRequest putObjectRequest = new PutObjectRequest(totalBucketName, key, inputStream, metadata);
+        try {
+            PutObjectResult result = cosClient.putObject(putObjectRequest);
 
-			return new FileUploadResult(String.format("http://%s.file.myqcloud.com%s", totalBucketName, key),
-					result.getETag());
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-		return null;
-	}
+            return new FileUploadResult(String.format("http://%s.file.myqcloud.com%s", totalBucketName, key),
+                    result.getETag());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 
-	public FileUploadResult uploadFile(InputStream inputStream, String key) {
-		if (key != null && !key.startsWith("/"))
-			key = "/" + key;
-		String dir = FileUtil.getCacheDir();
-		String tempPath = dir + "/temp_" + System.currentTimeMillis() + "_" + (long) (Math.random() * 100000000);
-		try {
+    public FileUploadResult uploadFile(InputStream inputStream, String key) {
+        if (key != null && !key.startsWith("/"))
+            key = "/" + key;
+        String dir = FileUtil.getCacheDir();
+        String tempPath = dir + "/temp_" + System.currentTimeMillis() + "_" + (long) (Math.random() * 100000000);
+        try {
 
-			try {
-				FileUtil.saveAsFile(inputStream, tempPath);
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-			if (new File(tempPath).exists()) {
-				return uploadFile(new File(tempPath), key);
-			}
-			return null;
-		} finally {
-			if (new File(tempPath).exists())
-				new File(tempPath).delete();
-		}
-	}
+            try {
+                FileUtil.saveAsFile(inputStream, tempPath);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            if (new File(tempPath).exists()) {
+                return uploadFile(new File(tempPath), key);
+            }
+            return null;
+        } finally {
+            if (new File(tempPath).exists())
+                new File(tempPath).delete();
+        }
+    }
 
-	/**
-	 * 鑾峰彇Object鍒楄〃
-	 * 
-	 * @param prefix-鍖归厤瀛楃
-	 * @param count-鏈�澶�1000
-	 * @param marker-璧风偣鏍囪
-	 * @return
-	 */
+    /**
+     * 鑾峰彇Object鍒楄〃
+     *
+     * @param prefix-鍖归厤瀛楃
+     * @param count-鏈�澶�1000
+     * @param marker-璧风偣鏍囪
+     * @return
+     */
 
-	public ObjectListing getObjectList(String prefix, String marker, int count) {
-		ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
-		listObjectsRequest.setBucketName(totalBucketName);
-		listObjectsRequest.setDelimiter("/");
-		listObjectsRequest.setPrefix(prefix);
-		listObjectsRequest.setMaxKeys(count);
-		listObjectsRequest.setMarker(marker);
-		ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
-		return objectListing;
-	}
+    public ObjectListing getObjectList(String prefix, String marker, int count) {
+        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
+        listObjectsRequest.setBucketName(totalBucketName);
+        listObjectsRequest.setDelimiter("/");
+        listObjectsRequest.setPrefix(prefix);
+        listObjectsRequest.setMaxKeys(count);
+        listObjectsRequest.setMarker(marker);
+        ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
+        return objectListing;
+    }
 
-	public COSObjectSummary getObjectDetail(String prefix) {
-		ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
-		listObjectsRequest.setBucketName(totalBucketName);
-		listObjectsRequest.setDelimiter("/");
-		listObjectsRequest.setPrefix(prefix);
-		listObjectsRequest.setMaxKeys(1);
-		listObjectsRequest.setMarker(null);
-		ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
-		if (objectListing.getObjectSummaries() != null && objectListing.getObjectSummaries().size() > 0) {
-			return objectListing.getObjectSummaries().get(0);
-		}
-		return null;
-	}
+    public COSObjectSummary getObjectDetail(String prefix) {
+        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
+        listObjectsRequest.setBucketName(totalBucketName);
+        listObjectsRequest.setDelimiter("/");
+        listObjectsRequest.setPrefix(prefix);
+        listObjectsRequest.setMaxKeys(1);
+        listObjectsRequest.setMarker(null);
+        ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
+        if (objectListing.getObjectSummaries() != null && objectListing.getObjectSummaries().size() > 0) {
+            return objectListing.getObjectSummaries().get(0);
+        }
+        return null;
+    }
 
-	// 鏂囦欢鏄惁瀛樺湪
-	public boolean isFileExist(String url) {
-		String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
-		try {
-			cosClient.getObjectMetadata(totalBucketName, key);
-			return true;
-		} catch (Exception e) {
-		}
-		return false;
-	}
+    // 鏂囦欢鏄惁瀛樺湪
+    public boolean isFileExist(String url) {
+        String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
+        try {
+            cosClient.getObjectMetadata(totalBucketName, key);
+            return true;
+        } catch (Exception e) {
+        }
+        return false;
+    }
 
-	public boolean deleteFile(String url) {
-		String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
-		try {
-			cosClient.deleteObject(totalBucketName, key);
-			return true;
-		} catch (Exception e) {
-		}
-		return false;
-	}
+    public boolean deleteFile(String url) {
+        String key = url.replace(String.format("https://%s.file.myqcloud.com", totalBucketName), "");
+        try {
+            cosClient.deleteObject(totalBucketName, key);
+            return true;
+        } catch (Exception e) {
+        }
+        return false;
+    }
 
-	public static String getUploadVideoUrl(String name) {
-		if (name != null && !name.startsWith("/"))
-			name = "/" + name;
-		return "https://gz.file.myqcloud.com/files/v2/" + appId + "/" + bucketName + name;
-	}
+    public static String getUploadVideoUrl(String name) {
+        if (name != null && !name.startsWith("/"))
+            name = "/" + name;
+        return "https://gz.file.myqcloud.com/files/v2/" + appId + "/" + bucketName + name;
+    }
 
-	public static String getUploadVideoFileId(String name) {
-		if (name != null && !name.startsWith("/"))
-			name = "/" + name;
-		return "/" + appId + "/" + bucketName + name;
-	}
+    public static String getUploadVideoFileId(String name) {
+        if (name != null && !name.startsWith("/"))
+            name = "/" + name;
+        return "/" + appId + "/" + bucketName + name;
+    }
 
-	public static String getUploadVideoAuthorization() {
-		long time = System.currentTimeMillis() / 1000;
-		long expireTime = time + 60 * 20;// 20鍒嗛挓鐨勪笂浼犳椂闂�
-		long random = (long) (1000000L * Math.random());
-		String original = String.format("a=%s&b=%s&k=%s&e=%s&t=%s&r=%s&f=", appId + "", bucketName, secretId,
-				(expireTime) + "", time + "", random + "");
-		byte[] so = HmacUtils.hmacSha1(secretKey, original);
-		byte[] or = original.getBytes();
-		byte[] bts = ArrayUtils.addAll(so, or);
-		String sign = StringUtil.getBase64FromByte(bts).replace("\n", "").replace("\r", "").trim();
-		return sign;
-	}
+    public static String getUploadVideoAuthorization() {
+        long time = System.currentTimeMillis() / 1000;
+        long expireTime = time + 60 * 20;// 20鍒嗛挓鐨勪笂浼犳椂闂�
+        long random = (long) (1000000L * Math.random());
+        String original = String.format("a=%s&b=%s&k=%s&e=%s&t=%s&r=%s&f=", appId + "", bucketName, secretId,
+                (expireTime) + "", time + "", random + "");
+        byte[] so = HmacUtils.hmacSha1(secretKey, original);
+        byte[] or = original.getBytes();
+        byte[] bts = ArrayUtils.addAll(so, or);
+        String sign = StringUtil.getBase64FromByte(bts).replace("\n", "").replace("\r", "").trim();
+        return sign;
+    }
 
-	public FileUploadResult uploadFileByte(byte[] b, String key) {
-		if (key != null && !key.startsWith("/"))
-			key = "/" + key;
-		String dir = FileUtil.getCacheDir();
-		String tempPath = dir + "/temp_" + System.currentTimeMillis() + "_" + (long) (Math.random() * 100000000);
-		try {
+    public FileUploadResult uploadFileByte(byte[] b, String key) {
+        if (key != null && !key.startsWith("/"))
+            key = "/" + key;
+        String dir = FileUtil.getCacheDir();
+        String tempPath = dir + "/temp_" + System.currentTimeMillis() + "_" + (long) (Math.random() * 100000000);
+        try {
 
-			try {
-				FileUtil.saveAsFileByte(b, tempPath);
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-			if (new File(tempPath).exists()) {
-				return uploadFile(new File(tempPath), key);
-			}
-			return null;
-		} finally {
-			if (new File(tempPath).exists())
-				new File(tempPath).delete();
-		}
-	}
+            try {
+                FileUtil.saveAsFileByte(b, tempPath);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            if (new File(tempPath).exists()) {
+                return uploadFile(new File(tempPath), key);
+            }
+            return null;
+        } finally {
+            if (new File(tempPath).exists())
+                new File(tempPath).delete();
+        }
+    }
 }

--
Gitblit v1.8.0