use of com.qcloud.cos.auth.BasicCOSCredentials in project platform by elveahuang.
the class CosStorageServiceImpl method getClient.
/**
* @see StorageService#getClient()
*/
@Override
public COSClient getClient() {
COSCredentials credentials = new BasicCOSCredentials(this.config.getAccessKey(), this.config.getSecretKey());
Region region = new Region(this.config.getEndpoint());
ClientConfig clientConfig = new ClientConfig(region);
return new COSClient(credentials, clientConfig);
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project litemall by linlinjava.
the class TencentStorage method getCOSClient.
private COSClient getCOSClient() {
if (cosClient == null) {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region(region));
cosClient = new COSClient(cred, clientConfig);
}
return cosClient;
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project halo by ruibaby.
the class TencentCosFileHandler method delete.
@Override
public void delete(String key) {
Assert.notNull(key, "File key must not be blank");
// Get config
String region = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_REGION).toString();
String secretId = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_ID).toString();
String secretKey = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_KEY).toString();
String bucketName = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_BUCKET_NAME).toString();
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
Region regionConfig = new Region(region);
ClientConfig clientConfig = new ClientConfig(regionConfig);
// Init OSS client
COSClient cosClient = new COSClient(cred, clientConfig);
try {
cosClient.deleteObject(bucketName, key);
} catch (Exception e) {
throw new FileOperationException("附件 " + key + " 从腾讯云删除失败", e);
} finally {
cosClient.shutdown();
}
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project RuleApi by buxia97.
the class UploadController method cosUpload.
/**
* 上传cos
* @return
*/
@RequestMapping(value = "/cosUpload", method = RequestMethod.POST)
@ResponseBody
public Object cosUpload(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "token", required = false) String token) throws IOException {
Integer uStatus = UStatus.getStatus(token, this.dataprefix, redisTemplate);
if (uStatus == 0) {
return Result.getResultJson(0, "用户未登录或Token验证失败", null);
}
if (file == null) {
return new UploadMsg(0, "文件为空", null);
}
TypechoApiconfig apiconfig = apiconfigService.selectByKey(1);
String oldFileName = file.getOriginalFilename();
// String eName = oldFileName.substring(oldFileName.lastIndexOf("."));
String eName = "";
try {
eName = oldFileName.substring(oldFileName.lastIndexOf("."));
} catch (Exception e) {
oldFileName = oldFileName + ".png";
eName = oldFileName.substring(oldFileName.lastIndexOf("."));
}
// 检查是否是图片
BufferedImage bi = ImageIO.read(file.getInputStream());
if (bi == null) {
return Result.getResultJson(0, "请上传图片文件", null);
}
String newFileName = UUID.randomUUID() + eName;
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials(apiconfig.getCosAccessKey(), apiconfig.getCosSecretKey());
// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region(apiconfig.getCosBucket()));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
String bucketName = apiconfig.getCosBucketName();
// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
// 大文件上传请参照 API 文档高级 API 上传
File localFile = null;
try {
localFile = File.createTempFile("temp", null);
file.transferTo(localFile);
// 指定要上传到 COS 上的路径
String key = "/" + apiconfig.getCosPrefix() + "/" + year + "/" + month + "/" + day + "/" + newFileName;
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
// return new UploadMsg(1,"上传成功",this.path + putObjectRequest.getKey());
Map<String, String> info = new HashMap<String, String>();
info.put("url", apiconfig.getCosPath() + putObjectRequest.getKey());
return Result.getResultJson(1, "上传成功", info);
} catch (IOException e) {
return Result.getResultJson(0, "上传失败", null);
} finally {
// 关闭客户端(关闭后台线程)
cosclient.shutdown();
}
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project roof-im by madfroglx.
the class OCSTest method testUpload.
@Test
public void testUpload() {
// 用户基本信息
String appid = "1255710173";
String secret_id = "AKID3A9jOoqatjrh6k7UjyKfA5N6q4olSaB6";
String secret_key = "Qdr0efEb3NicSkED2UsKaQ8ANaPorDWr";
String sessionToken = "81ca8cc5fb84bd42ac36c40515917b308ad333d830001";
// 设置秘钥
COSCredentials cred = new BasicCOSCredentials(appid, secret_id, secret_key);
// 设置区域, 这里设置为华北
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
// 生成 cos 客户端对象
COSClient cosClient = new COSClient(cred, clientConfig);
// 创建 bucket
// bucket 数量上限 200 个, bucket 是重操作, 一般不建议创建如此多的 bucket
// 重复创建同名 bucket 会报错
String bucketName = "im";
// 上传 object, 建议 20M 以下的文件使用该接口
File localFile = new File("E:\\excel\\test.txt");
String key = "im/zlt/test4";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setSecurityToken(sessionToken);
putObjectRequest.setMetadata(objectMetadata);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(putObjectResult);
// 关闭客户端 (关闭后台线程)
cosClient.shutdown();
}
Aggregations