5 changed files with 251 additions and 0 deletions
@ -0,0 +1,103 @@ |
|||
package com.ccsens.util; |
|||
|
|||
import cn.hutool.core.util.StrUtil; |
|||
import com.ccsens.util.exception.BaseException; |
|||
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.http.HttpProtocol; |
|||
import com.qcloud.cos.model.*; |
|||
import com.qcloud.cos.region.Region; |
|||
import lombok.Data; |
|||
|
|||
import java.io.*; |
|||
|
|||
/** |
|||
* @author 逗 |
|||
*/ |
|||
public class TxCosUtil { |
|||
|
|||
@Data |
|||
public static class FileInfo{ |
|||
private InputStream inputStream; |
|||
private String fileName; |
|||
private Long fileSize; |
|||
|
|||
public FileInfo(InputStream inputStream, String fileName, Long fileSize) { |
|||
this.inputStream = inputStream; |
|||
this.fileName = fileName; |
|||
this.fileSize = fileSize; |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 上传文件 |
|||
*/ |
|||
public static String upLoadCos(String filePath, FileInfo fileInfo, String secretId, String secretKey, String bucketName, String region) throws Exception { |
|||
String key = getFileKey(filePath,fileInfo.getFileName()); |
|||
try { |
|||
ObjectMetadata objectMetadata = new ObjectMetadata(); |
|||
// 上传的流如果能够获取准确的流长度,则推荐一定填写 content-length
|
|||
// 如果确实没办法获取到,则下面这行可以省略,但同时高级接口也没办法使用分块上传了
|
|||
objectMetadata.setContentLength(fileInfo.getFileSize()); |
|||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, fileInfo.inputStream, objectMetadata); |
|||
|
|||
COSClient cosClient = createCosClient(secretId, secretKey, region); |
|||
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); |
|||
} catch (Exception e) { |
|||
throw new BaseException("上传文件至COS失败"); |
|||
} |
|||
return key; |
|||
} |
|||
|
|||
/** |
|||
* 生成cos客户端 |
|||
*/ |
|||
public static COSClient createCosClient(String secretId, String secretKey,String region) { |
|||
// 设置用户身份信息。
|
|||
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(region)); |
|||
|
|||
// 设置请求协议, http 或者 https
|
|||
// 5.6.53 及更低的版本,建议设置使用 https 协议
|
|||
// 5.6.54 及更高版本,默认使用了 https
|
|||
clientConfig.setHttpProtocol(HttpProtocol.https); |
|||
|
|||
// 以下的设置,是可选的:
|
|||
// // 设置 socket 读取超时,默认 30s
|
|||
// clientConfig.setSocketTimeout(30*1000);
|
|||
// // 设置建立连接超时,默认 30s
|
|||
// clientConfig.setConnectionTimeout(30*1000);
|
|||
//
|
|||
// // 如果需要的话,设置 http 代理,ip 以及 port
|
|||
// clientConfig.setHttpProxyIp("httpProxyIp");
|
|||
// clientConfig.setHttpProxyPort(80);
|
|||
// 生成 cos 客户端。
|
|||
return new COSClient(cred, clientConfig); |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 生成文件路径 |
|||
*/ |
|||
private static String getFileKey(String filePath, String fileName) { |
|||
//1.获取后缀名 2.去除文件后缀 替换所有特殊字符
|
|||
String fileType; |
|||
try { |
|||
fileType = fileName.substring(fileName.lastIndexOf(".")); |
|||
}catch (Exception e){ |
|||
fileType = ""; |
|||
} |
|||
String fileStr = StrUtil.removeSuffix(fileName, fileType).replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "_"); |
|||
filePath += System.currentTimeMillis() + "_" + fileStr + fileType; |
|||
return filePath; |
|||
} |
|||
} |
Loading…
Reference in new issue