use of com.qcloud.cos.auth.COSCredentials in project halo by halo-dev.
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-plugin-experimental by guqing.
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 alluxio by Alluxio.
the class COSUnderFileSystem method createInstance.
/**
* Constructs a new instance of {@link COSUnderFileSystem}.
*
* @param uri the {@link AlluxioURI} for this UFS
* @param conf the configuration for this UFS
* @return the created {@link COSUnderFileSystem} instance
*/
public static COSUnderFileSystem createInstance(AlluxioURI uri, UnderFileSystemConfiguration conf) throws Exception {
String bucketName = UnderFileSystemUtils.getBucketName(uri);
Preconditions.checkArgument(conf.isSet(PropertyKey.COS_ACCESS_KEY), "Property %s is required to connect to COS", PropertyKey.COS_ACCESS_KEY);
Preconditions.checkArgument(conf.isSet(PropertyKey.COS_SECRET_KEY), "Property %s is required to connect to COS", PropertyKey.COS_SECRET_KEY);
Preconditions.checkArgument(conf.isSet(PropertyKey.COS_REGION), "Property %s is required to connect to COS", PropertyKey.COS_REGION);
Preconditions.checkArgument(conf.isSet(PropertyKey.COS_APP_ID), "Property %s is required to connect to COS", PropertyKey.COS_APP_ID);
String accessKey = conf.getString(PropertyKey.COS_ACCESS_KEY);
String secretKey = conf.getString(PropertyKey.COS_SECRET_KEY);
String regionName = conf.getString(PropertyKey.COS_REGION);
String appId = conf.getString(PropertyKey.COS_APP_ID);
COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
ClientConfig clientConfig = createCOSClientConfig(regionName, conf);
COSClient client = new COSClient(cred, clientConfig);
return new COSUnderFileSystem(uri, client, bucketName, appId, conf);
}
use of com.qcloud.cos.auth.COSCredentials 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();
}
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)));
}
Aggregations