一、参考项
腾讯云 COS(官网): 对象存储数据处理_COS数据处理_数据处理方案-腾讯云
COS SDK for Java(官网): 对象存储 快速入门 - SDK 文档 - 文档中心 - 腾讯云
二、引入Pom文件
<!-- https://mvnrepository.com/artifact/com.qcloud/cos_api -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.5.5</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
三、定义抽象类
public abstract class BaseObjectStorage {
/**
* 上传文件
*
* @param pathAndName
* @param file
*/
public abstract void upload(String pathAndName, File file);
/**
* 授权
*
* @param pathAndName
* @param time
* @return
*/
public abstract String authorize(String pathAndName, long time);
/**
* 授权(路径全)
*
* @param pathAndName
* @param time
* @return
*/
public abstract String authorizeAllName(String pathAndName, long time);
/**
* 临时上传文件授权
*
* @param dir
* @return
*/
public abstract Map<String, Object> tokens(String dir);
/**
* 删除文件
*
* @param pathAndName
*/
public abstract void deleteFile(String pathAndName);
}
四、COS实现类
package cn.xhh.xhh.core.objectstorage;
import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import com.google.common.collect.Maps;
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.auth.COSSigner;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.region.Region;
import lombok.Data;
@Component
public class CosObjectStorage extends BaseObjectStorage {
@Data
@Component
@ConfigurationProperties(prefix = "oss")
public static class CosInfo {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private String region;
private String rootDirectory;
}
@Autowired
private CosInfo cosInfo;
private COSClient init() {
COSCredentials cred = new BasicCOSCredentials(cosInfo.accessKeyId, cosInfo.accessKeySecret);
Region region = new Region(cosInfo.getRegion());
ClientConfig clientConfig = new ClientConfig(region);
COSClient cosClient = new COSClient(cred, clientConfig);
return cosClient;
}
@Override
public void upload(String pathAndName, File file) {
COSClient cosClient = init();
try {
cosClient.putObject(cosInfo.bucketName, cosInfo.rootDirectory + pathAndName, file);
} finally {
cosClient.shutdown();
}
}
@Override
public String authorize(String pathAndName, long time) {
COSClient cosClient = init();
try {
Date expiration = new Date(new Date().getTime() + time);
URL url = cosClient.generatePresignedUrl(cosInfo.bucketName,
cosInfo.rootDirectory + pathAndName, expiration);
return url.toString();
} finally {
cosClient.shutdown();
}
}
@Override
public String authorizeAllName(String pathAndName, long time) {
COSClient cosClient = init();
try {
Date expiration = new Date(new Date().getTime() + time);
URL url = cosClient.generatePresignedUrl(cosInfo.bucketName, pathAndName, expiration);
return url.toString();
} finally {
cosClient.shutdown();
}
}
@Override
public Map<String, Object> tokens(String dir) {
Map<String, Object> result = Maps.newHashMap();
COSClient cosClient = init();
try {
long expireTime = 60;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
COSCredentials cred = new BasicCOSCredentials(cosInfo.accessKeyId, cosInfo.accessKeySecret);
COSSigner signer = new COSSigner();
dir = "frontend/xhh/" + dir;
String sign = signer.buildAuthorizationStr(HttpMethodName.PUT, dir, cred, expiration);
result.put("storeType", "cos");
result.put("storageType", "cos");
result.put("accessId", cosInfo.accessKeyId);
result.put("signature", sign);
result.put("expire", String.valueOf(expireEndTime / 1000));
result.put("dir", dir);
result.put("host", cosInfo.endpoint.split("://")[0] + "://" + cosInfo.bucketName + "."
+ cosInfo.endpoint.split("://")[1]);
} catch (Exception e) {
e.printStackTrace();
} finally {
cosClient.shutdown();
}
return result;
}
@Override
public void deleteFile(String pathAndName) {
COSClient cosClient = init();
try {
cosClient.deleteObject(cosInfo.bucketName, cosInfo.rootDirectory + pathAndName);
} finally {
cosClient.shutdown();
}
}
}
五、application配置文件
objectstorage.type: cos
cos:
endpoint: https://oss-cn-xhh.aliyuncs.com
region: 1
access-key-id: LTAI4GKpXXXXXYuEzsyY63x
access-key-secret: vsVyXXX8XYWOWYjFMPXXXXjkw
bucket-name: data
root-directory: xhh/export/