Search in sources :

Example 56 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials 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)));
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) COSSigner(com.qcloud.cos.auth.COSSigner) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Date(java.util.Date) Test(org.junit.Test)

Example 57 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials 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();
    }
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) FileOperationException(run.halo.app.exception.FileOperationException) FileOperationException(run.halo.app.exception.FileOperationException) COSClient(com.qcloud.cos.COSClient) Region(com.qcloud.cos.region.Region) UploadResult(run.halo.app.model.support.UploadResult) ClientConfig(com.qcloud.cos.ClientConfig) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 58 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials in project cloud-sdk by mizhousoft.

the class COSObjectStorageServiceImpl method init.

public void init(COSProfile config) throws CloudSDKException {
    validate(config);
    COSCredentials cred = null;
    if (StringUtils.isBlank(config.getSessionToken())) {
        cred = new BasicCOSCredentials(config.getAccessKey(), config.getSecretKey());
    } else {
        cred = new BasicSessionCredentials(config.getAccessKey(), config.getSecretKey(), config.getSessionToken());
    }
    RegionEnum region = RegionEnum.get(config.getRegion());
    ClientConfig clientConfig = new ClientConfig(new Region(region.getValue()));
    clientConfig.setHttpProtocol(HttpProtocol.https);
    COSClient cosClient = new COSClient(cred, clientConfig);
    this.profile = config;
    this.cosClient = cosClient;
    LOG.info("Init cos client successfully.");
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicSessionCredentials(com.qcloud.cos.auth.BasicSessionCredentials) RegionEnum(com.mizhousoft.tencent.RegionEnum) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 59 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials in project yili-music by programmer-yili.

the class CosStorageServiceImpl method createCOSClient.

// Todo: 改造client获取方法
COSClient createCOSClient() {
    // 这里需要已经获取到临时密钥的结果。
    // 临时密钥的生成参考 https://cloud.tencent.com/document/product/436/14048#cos-sts-sdk
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    // ClientConfig 中包含了后续请求 COS 的客户端设置:
    ClientConfig clientConfig = new ClientConfig();
    // 设置 bucket 的地域
    // COS_REGION 请参照 https://cloud.tencent.com/document/product/436/6224
    clientConfig.setRegion(new Region(region));
    // 设置请求协议, http 或者 https
    // 5.6.53 及更低的版本,建议设置使用 https 协议
    // 5.6.54 及更高版本,默认使用了 https
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 以下的设置,是可选的:
    // 设置 socket 读取超时,默认 30s
    clientConfig.setSocketTimeout(30 * 1000);
    // 设置建立连接超时,默认 30s
    clientConfig.setConnectionTimeout(30 * 1000);
    return new COSClient(cred, clientConfig);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 60 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials 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();
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) FileOperationException(run.halo.app.exception.FileOperationException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) FileOperationException(run.halo.app.exception.FileOperationException)

Aggregations

BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)103 COSCredentials (com.qcloud.cos.auth.COSCredentials)103 Region (com.qcloud.cos.region.Region)99 ClientConfig (com.qcloud.cos.ClientConfig)94 COSClient (com.qcloud.cos.COSClient)91 CosClientException (com.qcloud.cos.exception.CosClientException)40 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)21 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)11 Copy (com.qcloud.cos.transfer.Copy)8 CopyResult (com.qcloud.cos.model.CopyResult)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)7 LinkedList (java.util.LinkedList)7 FileOperationException (run.halo.app.exception.FileOperationException)6 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)5