一、騰訊雲COS
騰訊雲對象存儲 COS 是一種存儲海量數據的分布式存儲服務。COS 提供了多種對象的存儲類型:標准存儲、低頻存儲、歸檔存儲。
二、為什么要使用TA
- 便宜;
- 個人用戶有6個月的免費使用額度50G;
- 有客戶端COSBrowser ,提供圖形化操作;
- 可以設置跨域、防盜鏈;
- 可以進行靜態網站托管;
- 支持https訪問;
- CDN加速,下載速度大幅提高。
以上就是我使用它的原因。
三、開始對接
1、添加SDK
<!-- 騰訊雲cos_api -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.4.10</version>
</dependency>
<!-- java開發工具包 https://www.hutool.club/docs/#/ -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.13</version>
</dependency>
2、編寫工具類
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.thread.ThreadUtil;
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.model.*;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.Copy;
import com.qcloud.cos.transfer.Download;
import com.qcloud.cos.transfer.TransferManager;
import com.qcloud.cos.transfer.Upload;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.ExecutorService;
/**
* 騰訊雲對象存儲cos幫助類
*
* @author lixingwu
*/
public class TxCosUtils {
/*配置參數自行填寫*/
private static final String APP_ID = "";
private static final String SECRET_ID = "";
private static final String SECRET_KEY = "";
private static final String REGION_NAME = "";
private TxCosUtils() {
}
/**
* 方法描述:獲取cos存儲桶客戶端.
* 創建時間:2019-02-27 18:09:46
*
* @author "lixingwu"
*/
private static COSClient getCOSClient() {
// 1 初始化用戶身份信息(secretId, secretKey)。
COSCredentials cred = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
// 2 設置bucket的區域, COS地域的簡稱請參照 https://cloud.tencent.com/document/product/436/6224
// clientConfig中包含了設置 region, https(默認 http), 超時, 代理等 set 方法, 使用可參見源碼或者接口文檔 FAQ 中說明。
ClientConfig clientConfig = new ClientConfig(new Region(REGION_NAME));
// 3 生成 cos 客戶端。
return new COSClient(cred, clientConfig);
}
/**
* 方法描述:bucket 的命名規則為{name}-{appid} ,
* name 僅支持小寫字母、數字和 - 的組合,不能超過40字符.
* 創建時間:2019-02-27 18:09:46
*
* @author "lixingwu"
*/
private static String getBucketName(String bucketName) {
Validator.validateNotEmpty(bucketName, "bucketName不能為空");
Validator.validateMatchRegex("^[a-z!-@0-9]+$", bucketName,
"bucketName僅支持小寫字母、數字和 - 的組合");
if (bucketName.length() > 40) {
throw new ValidateException("bucketName不能超過40字符");
}
return (bucketName + "-" + APP_ID);
}
/**
* 方法描述:創建一個存儲桶.
* 創建時間:2019-02-27 18:09:46
*
* @author "lixingwu"
*/
public static Bucket createBucket(String bucketName) throws CosClientException {
Bucket bucket = null;
COSClient client = getCOSClient();
bucket = createBucket(getBucketName(bucketName));
//設置存儲桶的權限為 公有讀私有寫
if (null != bucket) {
client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
}
return bucket;
}
/**
* 方法描述:獲取存儲桶列表.
* 創建時間:2019-02-27 18:09:46
*
* @author "lixingwu"
*/
public static List<Bucket> listBuckets() throws CosClientException {
return getCOSClient().listBuckets();
}
/**
* 方法描述:檢測存儲桶是否存在.
* 創建時間:2019-02-27 18:09:46
*
* @author "lixingwu"
*/
public static boolean doesBucketExist(String bucketName) throws CosClientException {
return getCOSClient().doesBucketExist(getBucketName(bucketName));
}
/**
* 方法描述:刪除文件.
* 創建時間:2019-03-05 14:25:08
* 創建作者:李興武
*
* @param bucketName bucketName
* @param fileName 文件地址
* @throws CosClientException the cos client exception
* @author "lixingwu"
*/
public static void deleteObject(String bucketName, String fileName)
throws CosClientException {
getCOSClient().deleteObject(bucketName, fileName);
}
/**
* 方法描述:獲取文件的信息.
* 創建時間:2019-03-05 14:54:09
* 創建作者:李興武
*
* @param bucketName bucketName
* @param fileName 文件地址
* @return the object metadata
* @throws CosClientException the cos client exception
* @author "lixingwu"
*/
public static ObjectMetadata findObjectMetadata(String bucketName, String fileName)
throws CosClientException {
return getCOSClient().getObjectMetadata(getBucketName(bucketName), fileName);
}
/**
* 方法描述:獲取一個高級的API TransferManager,項目中應該使用這些接口.
* 創建時間:2019-03-05 15:35:12
* 創建作者:李興武
*
* @return the transfer manager
* @author "lixingwu"
*/
public static TransferManager cosTransferManager() {
ExecutorService threadPool = ThreadUtil.newExecutor();
return new TransferManager(getCOSClient(), threadPool);
}
/**
* 方法描述:上傳文件
* 創建時間:2019-03-05 15:53:54
* 創建作者:李興武
*
* @param bucketName 存儲桶名稱
* @param filePath 文件存儲地址
* @param stream 文件流
* @return the upload
* @author "lixingwu"
*/
public static Upload upload(String bucketName, String filePath, InputStream stream) {
TransferManager manager = cosTransferManager();
Upload result = null;
try {
result = manager.upload(getBucketName(bucketName), filePath, stream, new ObjectMetadata());
result.waitForUploadResult();
} catch (CosClientException | InterruptedException e) {
e.printStackTrace();
} finally {
manager.shutdownNow();
}
return result;
}
/**
* 方法描述:下載文件.
* 創建時間:2019-03-05 16:20:16
* 創建作者:李興武
*
* @param bucketName 存儲桶名稱
* @param filePath 文件存儲地址
* @param destFile 存儲到的本地目標文件
* @return the download
* @author "lixingwu"
*/
public static Download download(String bucketName, String filePath, File destFile) {
TransferManager manager = cosTransferManager();
Download download = null;
try {
download = manager.download(getBucketName(bucketName), filePath, destFile);
download.waitForCompletion();
} catch (CosClientException | InterruptedException e) {
e.printStackTrace();
} finally {
manager.shutdownNow();
}
return download;
}
/**
* 方法描述:復制文件-不同存儲桶.
* 創建時間:2019-03-05 16:28:11
* 創建作者:李興武
*
* @param srcBucketName 源存儲桶名稱
* @param srcKey 源文件地址
* @param destBucketName 目標存儲桶名稱
* @param destKey 目標件地址
* @return the copy
* @author "lixingwu"
*/
public static Copy copy(String srcBucketName, String srcKey, String destBucketName, String destKey) {
TransferManager manager = cosTransferManager();
Copy copy = null;
try {
copy = manager.copy(getBucketName(srcBucketName), srcKey, getBucketName(destBucketName), destKey);
copy.waitForCompletion();
} catch (CosClientException | InterruptedException e) {
e.printStackTrace();
} finally {
manager.shutdownNow();
}
return copy;
}
/**
* 方法描述:復制文件-同存儲桶.
* 創建時間:2019-03-05 16:28:11
* 創建作者:李興武
*
* @param bucketName 源存儲桶名稱
* @param srcKey 源文件地址
* @param destKey 目標件地址
* @return the copy
* @author "lixingwu"
*/
public static Copy copy(String bucketName, String srcKey, String destKey) {
return copy(bucketName, srcKey, bucketName, destKey);
}
/*測試*/
public static void main(String[] args) throws IOException {
// Bucket bucket = TxCosUtils.createBucket("test");
// List<Bucket> listBuckets = TxCosUtils.listBuckets();
// boolean test = TxCosUtils.doesBucketExist("web-js-css01");
// File file = new File("E:\\codeList01.html");
// BufferedInputStream stream = FileUtil.getInputStream(file);
// PutObjectResult result = TxCosUtils.putObject("test", "html/codeList01.html", stream);
// COSObject test = downObject("test", "html/codeList.html");
// COSObjectInputStream inputStream = test.getObjectContent();
// ObjectMetadata test = downObject("test", "html/codeList.html", new File("E:\\codeList.xml"));
// ObjectMetadata metadata = findObjectMetadata("test", "html/codeList.html");
// CopyObjectResult result = TxCosUtils.copyObject("web-js-css", "css/ch233.min.css", "test", "css/ch233.min.css");
// CopyObjectResult result = TxCosUtils.copyObject("test", "css/ch233.min.css", "css/ch233.min.css.bak");
// deleteObject(getBucketName("test"), "html/codeList01.html");
// 高級API
// 上傳文件
// File file = new File("E:\\templet.html");
// BufferedInputStream stream = FileUtil.getInputStream(file);
// Upload test = TxCosUtils.upload("test", "html/templet.html", stream);
// Console.log(test);
// 下載文件
// Download test = TxCosUtils.download("test", "css/ch233.min.css.bak", new File("E:\\ch233.min.css"));
// Console.log(test);
// 復制文件
// Copy copy = TxCosUtils.copy("web-js-css", "layui/layui.js", "test", "layui/layui.js");
// Copy copy = TxCosUtils.copy("test", "layui/layui.js", "layui/layui.js.bak");
// Console.log(copy);
}
}