java对象存储工具类
package top.yuechenc.xingchen.common.utils;
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.exception.CosServiceException;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import com.tencent.cloud.CosStsClient;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;
@Data
@Component
@PropertySource("other.yml")
@ConfigurationProperties(prefix = "tencent")
public class CosClientTest implements InitializingBean {
@Value("${secretId}")
private String secretId;
@Value("${secretKey}")
private String secretKey;
@Value("${bucket}")
private String bucket;
@Value("${region}")
private String region;
@Value("${allowPrefix}")
private String allowPrefix;
@Value("${durationSeconds}")
private String durationSeconds;
@Value("${baseUrl}")
private String baseUrl;
@Override
public void afterPropertiesSet() {
System.out.println("================="+secretId);
}
public String keyUploadFile(String fullFileName, File file) {
JSONObject temp = getTempKey();
String tmpSecretId = temp.getJSONObject("credentials").getString("tmpSecretId");
String tmpSecretKey = temp.getJSONObject("credentials").getString("tmpSecretKey");
String sessionToken = temp.getJSONObject("credentials").getString("sessionToken");
COSCredentials cred = new BasicCOSCredentials(tmpSecretId, tmpSecretKey);
ClientConfig clientConfig = new ClientConfig(new Region(region));
COSClient cosclient = new COSClient(cred, clientConfig);
String bucketName = bucket;
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fullFileName, file);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setSecurityToken(sessionToken);
putObjectRequest.setMetadata(objectMetadata);
String rtValue = null;
try {
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
String etag = putObjectResult.getETag();
rtValue = baseUrl + fullFileName;
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} finally {
cosclient.shutdown();
return rtValue;
}
}
private void keyDownloadFile() {
JSONObject temp = getTempKey();
String tmpSecretId = temp.getJSONObject("credentials").getString("tmpSecretId");
String tmpSecretKey = temp.getJSONObject("credentials").getString("tmpSecretKey");
String sessionToken = temp.getJSONObject("credentials").getString("sessionToken");
COSCredentials cred = new BasicCOSCredentials(tmpSecretId, tmpSecretKey);
ClientConfig clientConfig = new ClientConfig(new Region(region));
COSClient cosclient = new COSClient(cred, clientConfig);
}
private JSONObject getTempKey() {
TreeMap<String, Object> config = new TreeMap<String, Object>();
try {
config.put("SecretId", secretId);
config.put("SecretKey", secretKey);
config.put("durationSeconds", Integer.parseInt(durationSeconds));
config.put("bucket", bucket);
config.put("region", region);
config.put("allowPrefix", allowPrefix);
JSONObject credential = CosStsClient.getCredential(config);
System.out.println(credential);
return credential;
} catch (Exception e) {
throw new IllegalArgumentException("no valid secret !");
}
}
public COSClient getCosClient() {
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
Region regiona = new Region(region);
ClientConfig clientConfig = new ClientConfig(regiona);
COSClient cosClient = new COSClient(cred, clientConfig);
return cosClient;
}
public String uploadFile(String fullFileName, File file) {
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, fullFileName, file);
putObjectRequest.setStorageClass(StorageClass.Standard_IA);
COSClient cc = getCosClient();
String etag = "";
String resutl;
try {
PutObjectResult putObjectResult = cc.putObject(putObjectRequest);
etag = putObjectResult.getETag();
resutl = baseUrl + fullFileName;
} catch (CosServiceException e) {
e.printStackTrace();
resutl=e.getMessage();
} catch (CosClientException e) {
e.printStackTrace();
resutl=e.getMessage();
}finally {
cc.shutdown();
}
return resutl;
}
public String downLoadFile(String bucketName, String path, String fullFileName) {
File downFile = new File(fullFileName);
COSClient cc = getCosClient();
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, path);
ObjectMetadata downObjectMeta = cc.getObject(getObjectRequest, downFile);
cc.shutdown();
String etag = downObjectMeta.getETag();
return etag;
}
public void deleteFile(String bucketName, String path) {
COSClient cc = getCosClient();
try {
cc.deleteObject(bucketName, path);
} catch (CosClientException e) {
e.printStackTrace();
} finally {
cc.shutdown();
}
}
public Bucket createBucket(String bucketName) throws CosClientException, CosServiceException {
COSClient cc = getCosClient();
Bucket bucket = null;
try {
bucket = cc.createBucket(bucketName);
} catch (CosClientException e) {
e.printStackTrace();
} finally {
}
return bucket;
}
;
public void deleteBucket(String bucketName) throws CosClientException, CosServiceException {
COSClient cc = getCosClient();
try {
cc.deleteBucket(bucketName);
} catch (CosClientException e) {
e.printStackTrace();
} finally {
}
}
;
public boolean doesBucketExist(String bucketName) throws CosClientException, CosServiceException {
COSClient cc = getCosClient();
boolean bucketExistFlag = cc.doesBucketExist(bucketName);
return bucketExistFlag;
}
;
public ObjectListing listObjects(String bucketName) throws CosClientException, CosServiceException {
COSClient cc = getCosClient();
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
listObjectsRequest.setBucketName(bucketName);
listObjectsRequest.setPrefix("");
listObjectsRequest.setDelimiter("/");
listObjectsRequest.setMarker("");
listObjectsRequest.setMaxKeys(100);
ObjectListing objectListing = cc.listObjects(listObjectsRequest);
String nextMarker = objectListing.getNextMarker();
boolean isTruncated = objectListing.isTruncated();
List<COSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
for (COSObjectSummary cosObjectSummary : objectSummaries) {
String key = cosObjectSummary.getKey();
long fileSize = cosObjectSummary.getSize();
String eTag = cosObjectSummary.getETag();
Date lastModified = cosObjectSummary.getLastModified();
String StorageClassStr = cosObjectSummary.getStorageClass();
}
return objectListing;
}
public void main(String[] args) {
}
}
springboot配置文件
tencent:
# 腾讯云的SecretId(永久的,可在控制台开启或关闭)
secretId: ***
# 腾讯云的SecretKey(永久的,可在控制台开启或关闭)
secretKey: ***
# 腾讯云的allowPrefix(允许上传的路径)
allowPrefix: '*'
# 腾讯云的访问基础链接:
baseUrl: https://xingchen-***.cos.ap-chengdu.myqcloud.com/
# 腾讯云的bucket (存储桶)
bucket: xingchen-***
# 腾讯云的临时密钥时长(单位秒)
durationSeconds: 1800
# 腾讯云的region(bucket所在地区)
region: ap-chengdu