// 腾讯云COS高级(大文件)上传,不用考虑分块
public static URL fileCOS(File file, String key) throws Exception {
String SecretKey = "SecretKey";
String SecretId = "SecretId";
COSCredentials cred = new BasicCOSCredentials(SecretKey, SecretId);
// 2 设置bucket的区域, COS地域的简称请参照
// https://cloud.tencent.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("bucket的区域"));
// 3 生成cos客户端
COSClient cosClient = new COSClient(cred, clientConfig);
// bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
String bucketName = {name}-{appid};
//创建线程池
ExecutorService threadPool = Executors.newFixedThreadPool(32);
// 传入一个 threadpool, 若不传入线程池, 默认 TransferManager 中会生成一个单线程的线程池。
TransferManager transferManager = new TransferManager(cosClient, threadPool);
//分装与上传
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
Upload upload = transferManager.upload(putObjectRequest);
// 等待传输结束(如果想同步的等待上传结束,则调用 waitForCompletion)
upload.waitForUploadResult();
// 关闭 TransferManger
transferManager.shutdownNow();
Date expiration = new Date(new Date().getTime() + 5 * 60 * 10000);
URL url = cosClient.generatePresignedUrl(bucketName, key, expiration);
return url;
}
本文是对上篇(腾讯云 对象储存java实现COS文件上传与返回的URL)补充