use of com.qcloud.cos.auth.COSCredentials in project roof-im by madfroglx.
the class OCSTest method createAuthorization.
@Test
public void createAuthorization() throws UnsupportedEncodingException {
String appid = "1255710173";
String secret_id = "AKIDSpCPQFhNfmmcJ0Q0FKE7pNjuuiKlN73o";
String secret_key = "kKjOsEPOCoFUlwviLm27IJ9nFWcaNyOc";
// String sessionToken = "562af6ffcbe981dd21bd7434176dcbb2f883839030001";
// 设置秘钥
COSCredentials cred = new BasicCOSCredentials(appid, secret_id, secret_key);
COSSigner cosSigner = new COSSigner();
System.out.println(cosSigner.buildAuthorizationStr(HttpMethodName.POST, "/", cred, new Date(1520516362000L)));
}
use of com.qcloud.cos.auth.COSCredentials in project halo by ruibaby.
the class TencentCosFileHandler method upload.
@Override
public UploadResult upload(MultipartFile file) {
Assert.notNull(file, "Multipart file must not be null");
// Get config
String protocol = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_PROTOCOL).toString();
String domain = optionService.getByPropertyOrDefault(TencentCosProperties.COS_DOMAIN, String.class, "");
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();
String source = optionService.getByPropertyOrDefault(TencentCosProperties.COS_SOURCE, String.class, "");
String styleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_STYLE_RULE, String.class, "");
String thumbnailStyleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_THUMBNAIL_STYLE_RULE, String.class, "");
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);
StringBuilder basePath = new StringBuilder(protocol);
if (StringUtils.isNotEmpty(domain)) {
basePath.append(domain).append(URL_SEPARATOR);
} else {
basePath.append(bucketName).append(".cos.").append(region).append(".myqcloud.com").append(URL_SEPARATOR);
}
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder().setBasePath(basePath.toString()).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.TENCENTCOS) > 0).setOriginalName(file.getOriginalFilename()).build();
// Upload
ObjectMetadata objectMetadata = new ObjectMetadata();
// 提前告知输入流的长度, 否则可能导致 oom
objectMetadata.setContentLength(file.getSize());
// 设置 Content type, 默认是 application/octet-stream
objectMetadata.setContentType(file.getContentType());
PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(bucketName, pathDescriptor.getRelativePath(), file.getInputStream(), objectMetadata);
if (putObjectResponseFromInputStream == null) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到腾讯云失败 ");
}
String fullPath = pathDescriptor.getFullPath();
// Response result
UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(pathDescriptor.getName());
uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? fullPath : fullPath + styleRule);
uploadResult.setKey(pathDescriptor.getRelativePath());
uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
uploadResult.setSuffix(pathDescriptor.getExtension());
uploadResult.setSize(file.getSize());
// Handle thumbnail
handleImageMetadata(file, uploadResult, () -> {
if (ImageUtils.EXTENSION_ICO.equals(pathDescriptor.getExtension())) {
uploadResult.setThumbPath(fullPath);
return fullPath;
} else {
return StringUtils.isBlank(thumbnailStyleRule) ? fullPath : fullPath + thumbnailStyleRule;
}
});
return uploadResult;
} catch (Exception e) {
throw new FileOperationException("附件 " + file.getOriginalFilename() + " 上传失败(腾讯云)", e);
} finally {
cosClient.shutdown();
}
}
use of com.qcloud.cos.auth.COSCredentials in project halo by halo-dev.
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.COSCredentials in project cos-java-sdk-v5 by tencentyun.
the class COSClient method invoke.
private <X, Y extends CosServiceRequest> X invoke(CosHttpRequest<Y> request, HttpResponseHandler<CosServiceResponse<X>> responseHandler) throws CosClientException, CosServiceException {
COSSigner cosSigner = clientConfig.getCosSigner();
COSCredentials cosCredentials;
CosServiceRequest cosServiceRequest = request.getOriginalRequest();
if (cosServiceRequest != null && cosServiceRequest.getCosCredentials() != null) {
cosCredentials = cosServiceRequest.getCosCredentials();
} else {
cosCredentials = fetchCredential();
}
Date expiredTime = new Date(System.currentTimeMillis() + clientConfig.getSignExpired() * 1000);
boolean isCIWorkflowRequest = cosServiceRequest instanceof CIWorkflowServiceRequest;
cosSigner.setCIWorkflowRequest(isCIWorkflowRequest);
cosSigner.sign(request, cosCredentials, expiredTime);
return this.cosHttpClient.exeute(request, responseHandler);
}
use of com.qcloud.cos.auth.COSCredentials in project cos-java-sdk-v5 by tencentyun.
the class PutGetDelTest method testRequestSpecifiedTmpKeyInfoPutGetDel.
@Test
public void testRequestSpecifiedTmpKeyInfoPutGetDel() throws CosServiceException, IOException, InterruptedException {
COSClient cosclient = buildTemporyCredentialsCOSClient(1800L);
File localFile = buildTestFile(1024L);
COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey);
try {
String key = "ut/request-specified-key";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
putObjectRequest.setCosCredentials(cosCredentials);
cosclient.putObject(putObjectRequest);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
getObjectRequest.setCosCredentials(cosCredentials);
cosclient.getObject(getObjectRequest);
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket, key);
deleteObjectRequest.setCosCredentials(cosCredentials);
cosclient.deleteObject(deleteObjectRequest);
} finally {
localFile.delete();
cosclient.shutdown();
}
}
Aggregations