init
This commit is contained in:
79
bashi-dk/src/main/java/com/bashi/dk/oss/ali/AliOssKit.java
Normal file
79
bashi-dk/src/main/java/com/bashi/dk/oss/ali/AliOssKit.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package com.bashi.dk.oss.ali;
|
||||
|
||||
import com.aliyun.oss.ClientConfiguration;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.common.auth.CredentialsProvider;
|
||||
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* <p>created on 2021/3/3</p>
|
||||
*
|
||||
* @author zhangliang
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AliOssKit {
|
||||
|
||||
@Autowired
|
||||
@Setter
|
||||
@Getter
|
||||
private OssConfig ossConfig;
|
||||
|
||||
@Getter
|
||||
public OSSClient ossClient = null;
|
||||
|
||||
public final static String OSS_KEY_COMMON = "dk/common/";
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
if(!ossConfig.isEnable()){
|
||||
log.error("未开启阿里云OSS配置");
|
||||
return;
|
||||
}
|
||||
// 创建ClientConfiguration。ClientConfiguration是OSSClient的配置类,可配置代理、连接超时、最大连接数等参数。
|
||||
ClientConfiguration conf = new ClientConfiguration();
|
||||
// 设置OSSClient允许打开的最大HTTP连接数,默认为1024个。
|
||||
conf.setMaxConnections(1024);
|
||||
// 设置Socket层传输数据的超时时间,默认为50000毫秒。
|
||||
conf.setSocketTimeout(50000);
|
||||
// 设置建立连接的超时时间,默认为50000毫秒。
|
||||
conf.setConnectionTimeout(50000);
|
||||
// 设置从连接池中获取连接的超时时间(单位:毫秒),默认不超时。
|
||||
conf.setConnectionRequestTimeout(1000);
|
||||
// 设置连接空闲超时时间。超时则关闭连接,默认为60000毫秒。
|
||||
conf.setIdleConnectionTime(60000);
|
||||
// 设置失败请求重试次数,默认为3次。
|
||||
conf.setMaxErrorRetry(5);
|
||||
CredentialsProvider credentialsProvider = new DefaultCredentialProvider(ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
||||
// 创建客户端
|
||||
ossClient = new OSSClient(ossConfig.getEndpoint(), credentialsProvider, conf);
|
||||
}
|
||||
|
||||
public String upload(InputStream inputStream,String fileName){
|
||||
ossClient.putObject(ossConfig.getBucketName(), fileName, inputStream);
|
||||
return getUrl(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传单个文件 公开文件
|
||||
* @param inputStream
|
||||
* @param key
|
||||
*/
|
||||
public String uploadByKey(InputStream inputStream,String key){
|
||||
ossClient.putObject(ossConfig.getBucketName(), key, inputStream);
|
||||
return getUrl(key);
|
||||
}
|
||||
|
||||
public String getUrl(String key){
|
||||
return ossConfig.getCdnDomain()+"/"+key;
|
||||
}
|
||||
|
||||
}
|
||||
23
bashi-dk/src/main/java/com/bashi/dk/oss/ali/CosConfig.java
Normal file
23
bashi-dk/src/main/java/com/bashi/dk/oss/ali/CosConfig.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.bashi.dk.oss.ali;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Data
|
||||
@ToString(exclude={"secretId","secretKey"})
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "tencent.cos")
|
||||
public class CosConfig {
|
||||
|
||||
private boolean enable = false;
|
||||
private String appId;
|
||||
private String region; // 连接区域地址
|
||||
private String cdnDomain; // cdn 域名
|
||||
private String secretId; // 连接keyId
|
||||
private String secretKey; // 连接秘钥
|
||||
private String bucketName; // 需要存储的bucketName
|
||||
|
||||
}
|
||||
105
bashi-dk/src/main/java/com/bashi/dk/oss/ali/CosKit.java
Normal file
105
bashi-dk/src/main/java/com/bashi/dk/oss/ali/CosKit.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package com.bashi.dk.oss.ali;
|
||||
|
||||
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.ObjectMetadata;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.qcloud.cos.model.UploadResult;
|
||||
import com.qcloud.cos.region.Region;
|
||||
import com.qcloud.cos.transfer.TransferManager;
|
||||
import com.qcloud.cos.transfer.TransferManagerConfiguration;
|
||||
import com.qcloud.cos.transfer.Upload;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CosKit {
|
||||
|
||||
@Autowired
|
||||
private CosConfig cosConfig;
|
||||
private COSClient cosClient = null;
|
||||
|
||||
private TransferManager transferManager = null;
|
||||
|
||||
|
||||
public TransferManager createTransferManager(COSClient cosClient){
|
||||
// 自定义线程池大小,建议在客户端与 COS 网络充足(例如使用腾讯云的 CVM,同地域上传 COS)的情况下,设置成16或32即可,可较充分的利用网络资源
|
||||
// 对于使用公网传输且网络带宽质量不高的情况,建议减小该值,避免因网速过慢,造成请求超时。
|
||||
ExecutorService threadPool = Executors.newFixedThreadPool(4);
|
||||
// 传入一个 threadpool, 若不传入线程池,默认 TransferManager 中会生成一个单线程的线程池。
|
||||
TransferManager transferManager = new TransferManager(cosClient, threadPool);
|
||||
// 设置高级接口的配置项
|
||||
// 分块上传阈值和分块大小分别为 5MB 和 1MB
|
||||
TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
|
||||
transferManagerConfiguration.setMultipartUploadThreshold(5*1024*1024);
|
||||
transferManagerConfiguration.setMinimumUploadPartSize(1*1024*1024);
|
||||
transferManager.setConfiguration(transferManagerConfiguration);
|
||||
return transferManager;
|
||||
}
|
||||
|
||||
public COSClient createCosClient(){
|
||||
// SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
|
||||
String secretId = cosConfig.getSecretId();
|
||||
String secretKey = cosConfig.getSecretKey();
|
||||
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(cosConfig.getRegion()));
|
||||
// 设置请求协议, 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);
|
||||
}
|
||||
|
||||
public boolean isEnable(){
|
||||
return cosConfig.isEnable();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
if(!cosConfig.isEnable()){
|
||||
log.error("未开启腾讯云COS配置");
|
||||
return;
|
||||
}
|
||||
cosClient = createCosClient();
|
||||
transferManager = createTransferManager(cosClient);
|
||||
}
|
||||
|
||||
public String upload(MultipartFile file, String fileName) throws Exception {
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentLength(file.getSize());
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(cosConfig.getBucketName(), fileName, file.getInputStream(),metadata);
|
||||
Upload upload = transferManager.upload(putObjectRequest);
|
||||
UploadResult uploadResult = upload.waitForUploadResult();
|
||||
return getUrl(uploadResult.getKey());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getUrl(String key){
|
||||
return cosConfig.getCdnDomain()+"/"+key;
|
||||
}
|
||||
|
||||
}
|
||||
24
bashi-dk/src/main/java/com/bashi/dk/oss/ali/OssConfig.java
Normal file
24
bashi-dk/src/main/java/com/bashi/dk/oss/ali/OssConfig.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.bashi.dk.oss.ali;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* <p>created on 2021/3/3</p>
|
||||
*
|
||||
* @author zhangliang
|
||||
*/
|
||||
@Data
|
||||
@ToString(exclude={"accessKeyId","accessKeySecret"})
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "ali.oss")
|
||||
public class OssConfig {
|
||||
private boolean enable = false;
|
||||
private String endpoint; // 连接区域地址
|
||||
private String cdnDomain; // cdn 域名
|
||||
private String accessKeyId; // 连接keyId
|
||||
private String accessKeySecret; // 连接秘钥
|
||||
private String bucketName; // 需要存储的bucketName
|
||||
}
|
||||
Reference in New Issue
Block a user